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 37 try: 38 size = os.stat(self._pkg_path).st_size 39 except OSError as e: 40 if e.errno not in (errno.ENOENT, errno.ESTALE): 41 raise 42 self.scheduler.output(("!!! Fetching Binary failed " 43 "for '%s'\n") % self.pkg.cpv, log_path=self.logfile, 44 background=self.background) 45 self.returncode = 1 46 self._async_wait() 47 return 48 else: 49 if size != digests["size"]: 50 self._digest_exception("size", size, digests["size"]) 51 self.returncode = 1 52 self._async_wait() 53 return 54 55 self._start_task(FileDigester(file_path=self._pkg_path, 56 hash_names=(k for k in digests if k != "size"), 57 background=self.background, logfile=self.logfile, 58 scheduler=self.scheduler), 59 self._digester_exit)
60
61 - def _digester_exit(self, digester):
62 63 if self._default_exit(digester) != os.EX_OK: 64 self.wait() 65 return 66 67 for hash_name in digester.hash_names: 68 if digester.digests[hash_name] != self._digests[hash_name]: 69 self._digest_exception(hash_name, 70 digester.digests[hash_name], self._digests[hash_name]) 71 self.returncode = 1 72 self.wait() 73 return 74 75 if self.pkg.root_config.settings.get("PORTAGE_QUIET") != "1": 76 self._display_success() 77 78 self.returncode = os.EX_OK 79 self.wait()
80
81 - def _display_success(self):
82 stdout_orig = sys.stdout 83 stderr_orig = sys.stderr 84 global_havecolor = portage.output.havecolor 85 out = io.StringIO() 86 try: 87 sys.stdout = out 88 sys.stderr = out 89 if portage.output.havecolor: 90 portage.output.havecolor = not self.background 91 92 path = self._pkg_path 93 if path.endswith(".partial"): 94 path = path[:-len(".partial")] 95 eout = EOutput() 96 eout.ebegin("%s %s ;-)" % (os.path.basename(path), 97 " ".join(sorted(self._digests)))) 98 eout.eend(0) 99 100 finally: 101 sys.stdout = stdout_orig 102 sys.stderr = stderr_orig 103 portage.output.havecolor = global_havecolor 104 105 self.scheduler.output(out.getvalue(), log_path=self.logfile, 106 background=self.background)
107
108 - def _digest_exception(self, name, value, expected):
109 110 head, tail = os.path.split(self._pkg_path) 111 temp_filename = _checksum_failure_temp_file(head, tail) 112 113 self.scheduler.output(( 114 "\n!!! Digest verification failed:\n" 115 "!!! %s\n" 116 "!!! Reason: Failed on %s verification\n" 117 "!!! Got: %s\n" 118 "!!! Expected: %s\n" 119 "File renamed to '%s'\n") % 120 (self._pkg_path, name, value, expected, temp_filename), 121 log_path=self.logfile, 122 background=self.background)
123