def _write_update_to_disk(self): # Writes updated binary to disk log.debug('Writing update to disk') filename_key = '{}*{}*{}*{}*{}'.format(u'updates', self.name, self.highest_version, self.plat, u'filename') filename = self.star_access_update_data.get(filename_key) if filename is None: raise PatcherError('Filename missing in version file') with ChDir(self.update_folder): try: with open(filename, u'wb') as f: f.write(self.new_binary) except IOError: # Removes file is it somehow got created if os.path.exists(filename): os.remove(filename) log.error(u'Failed to open file for writing') raise PatcherError(u'Failed to open file for writing') else: file_info = self._current_file_info(self.name, self.highest_version) new_file_hash = file_info['file_hash'] log.debug(u'checking file hash match') if new_file_hash != get_package_hashes(filename): log.error(u'File hash does not match') os.remove(filename) raise PatcherError(u'Patched file hash bad checksum') log.debug('Wrote update file')
def extract_info(self, package): """Gets version number, platform & hash for package. Args: package (str): filename """ if os.path.splitext(package)[1].lower() not in \ self.supported_extensions: msg = u'Not a supported archive format: {}'.format(package) self.info['reason'] = msg log.error(msg) return try: self.version = get_version_number(package) except UtilsError: msg = u'Package version not formatted correctly' self.info[u'reason'] = msg log.error(msg) return try: self.platform = parse_platform(package) except UtilsError: msg = u'Package platform not formatted correctly' self.info[u'reason'] = msg log.error(msg) return # No need to get any more info if above failed self.name = self._get_package_name(package) self.file_hash = get_package_hashes(package) self.info[u'status'] = True
def _verify_installed_binary(self): # Verifies currently installed binary against known hash log.debug(u'Checking for current installed binary to patch') # I just really like using this ChDir context # manager. Even sent the developer a cup of coffee with ChDir(self.update_folder): if not os.path.exists(self.current_filename): log.debug(u'Cannot find binary to patch') return False installed_file_hash = get_package_hashes(self.current_filename) if self.current_file_hash != installed_file_hash: log.debug(u'Binary hash mismatch') return False with open(self.current_filename, u'rb') as f: self.og_binary = f.read() os.remove(self.current_filename) log.debug(u'Binary found and verified') return True
def test_package_hash(): digest = 'cb44ec613a594f3b20e46b768c5ee780e0a9b66ac6d5ac1468ca4d3635c4aa9b' assert digest == get_package_hashes('hash-test.txt')