Package _emerge :: Module BinpkgVerifier
[hide private]

Source Code for Module _emerge.BinpkgVerifier

  1  # Copyright 1999-2013 Gentoo Foundation 
  2  # Distributed under the terms of the GNU General Public License v2 
  3   
  4  import errno 
  5  import io 
  6  import sys 
  7   
  8  from _emerge.CompositeTask import CompositeTask 
  9  import portage 
 10  from portage import os 
 11  from portage.checksum import (_apply_hash_filter, 
 12          _filter_unaccelarated_hashes, _hash_filter) 
 13  from portage.output import EOutput 
 14  from portage.util._async.FileDigester import FileDigester 
 15  from portage.package.ebuild.fetch import _checksum_failure_temp_file 
 16   
17 -class BinpkgVerifier(CompositeTask):
18 __slots__ = ("logfile", "pkg", "_digests", "_pkg_path") 19
20 - def _start(self):
21 22 bintree = self.pkg.root_config.trees["bintree"] 23 digests = bintree._get_digests(self.pkg) 24 if "size" not in digests: 25 self.returncode = os.EX_OK 26 self._async_wait() 27 return 28 29 digests = _filter_unaccelarated_hashes(digests) 30 hash_filter = _hash_filter( 31 bintree.settings.get("PORTAGE_CHECKSUM_FILTER", "")) 32 if not hash_filter.transparent: 33 digests = _apply_hash_filter(digests, hash_filter) 34 35 self._digests = digests 36 self._pkg_path = bintree.getname(self.pkg.cpv) 37 38 try: 39 size = os.stat(self._pkg_path).st_size 40 except OSError as e: 41 if e.errno not in (errno.ENOENT, errno.ESTALE): 42 raise 43 self.scheduler.output(("!!! Fetching Binary failed " 44 "for '%s'\n") % self.pkg.cpv, log_path=self.logfile, 45 background=self.background) 46 self.returncode = 1 47 self._async_wait() 48 return 49 else: 50 if size != digests["size"]: 51 self._digest_exception("size", size, digests["size"]) 52 self.returncode = 1 53 self._async_wait() 54 return 55 56 self._start_task(FileDigester(file_path=self._pkg_path, 57 hash_names=(k for k in digests if k != "size"), 58 background=self.background, logfile=self.logfile, 59 scheduler=self.scheduler), 60 self._digester_exit)
61
62 - def _digester_exit(self, digester):
63 64 if self._default_exit(digester) != os.EX_OK: 65 self.wait() 66 return 67 68 for hash_name in digester.hash_names: 69 if digester.digests[hash_name] != self._digests[hash_name]: 70 self._digest_exception(hash_name, 71 digester.digests[hash_name], self._digests[hash_name]) 72 self.returncode = 1 73 self.wait() 74 return 75 76 if self.pkg.root_config.settings.get("PORTAGE_QUIET") != "1": 77 self._display_success() 78 79 self.returncode = os.EX_OK 80 self.wait()
81
82 - def _display_success(self):
83 stdout_orig = sys.stdout 84 stderr_orig = sys.stderr 85 global_havecolor = portage.output.havecolor 86 out = io.StringIO() 87 try: 88 sys.stdout = out 89 sys.stderr = out 90 if portage.output.havecolor: 91 portage.output.havecolor = not self.background 92 93 eout = EOutput() 94 eout.ebegin("%s %s ;-)" % (os.path.basename(self._pkg_path), 95 " ".join(sorted(self._digests)))) 96 eout.eend(0) 97 98 finally: 99 sys.stdout = stdout_orig 100 sys.stderr = stderr_orig 101 portage.output.havecolor = global_havecolor 102 103 self.scheduler.output(out.getvalue(), log_path=self.logfile, 104 background=self.background)
105
106 - def _digest_exception(self, name, value, expected):
107 108 head, tail = os.path.split(self._pkg_path) 109 temp_filename = _checksum_failure_temp_file(head, tail) 110 111 self.scheduler.output(( 112 "\n!!! Digest verification failed:\n" 113 "!!! %s\n" 114 "!!! Reason: Failed on %s verification\n" 115 "!!! Got: %s\n" 116 "!!! Expected: %s\n" 117 "File renamed to '%s'\n") % 118 (self._pkg_path, name, value, expected, temp_filename), 119 log_path=self.logfile, 120 background=self.background)
121