def test_reset_timestamps(self): with util.temporary_directory(prefix='glyco-zipfix-') as tempdir: # Create an archive zipname = os.path.join(tempdir, 'testfile.zip') with zipfile.ZipFile(zipname, 'w') as f: f.write(os.path.join(DATA_DIR, 'zipfix_test', 'file1.txt')) f.write(os.path.join(DATA_DIR, 'zipfix_test', 'file2.txt')) # Read original state with zipfile.ZipFile(zipname, 'r') as f: dt_orig = [info.date_time for info in f.infolist()] namelist_orig = f.namelist() namelist_orig.sort() hashes_orig = [hashlib.sha1(f.read(filename)).hexdigest() for filename in namelist_orig] # Reset zipfix.reset_all_timestamps_in_zip(zipname) # Make sure only timestamps have changed. with zipfile.ZipFile(zipname, 'r') as f: dt_new = [info.date_time for info in f.infolist()] namelist_new = f.namelist() namelist_new.sort() hashes_new = [hashlib.sha1(f.read(filename)).hexdigest() for filename in namelist_new] self.assertEqual(namelist_orig, namelist_new) self.assertEqual(hashes_orig, hashes_new) self.assertNotEqual(dt_orig, dt_new) for dt in dt_new: self.assertEqual(dt, (1980, 0, 0, 0, 0, 0))
def grab_wheel(src, dst, build_num=0): """Move a single wheel file and fix its name. Args: src (str): directory containing one wheel file. dst (str): directory where to put the renamed wheel file. Returns: wheel_filename (str): path to the generated file, in its final location. """ items = os.listdir(src) assert len(items) == 1, ( 'Wrong number of files (%r) in wheel directory: %r' % (items, src)) wheelfile = items[0] wheel_info = util.WHEEL_FILE_RE.match(wheelfile) assert wheel_info is not None, ('Not a wheel file? %r' % os.path.join(src, wheelfile)) src_path = os.path.join(src, wheelfile) zipfix.reset_all_timestamps_in_zip(src_path) plat_tag = '' if not wheelfile.endswith('none-any.whl'): plat_tag = util.platform_tag() with open(src_path, 'rb') as f: digest = hashlib.sha1(f.read()) wheel_sha = digest.hexdigest() dest_path = os.path.join( dst, '{}-{}_{}{}{}'.format( wheel_info.group('namever'), build_num, wheel_sha, plat_tag, wheel_info.group(4), )) shutil.copyfile(src_path, dest_path) return dest_path
def grab_wheel(src, dst, build_num=0): """Move a single wheel file and fix its name. Args: src (str): directory containing one wheel file. dst (str): directory where to put the renamed wheel file. Returns: wheel_filename (str): path to the generated file, in its final location. """ items = os.listdir(src) assert len(items) == 1, ( 'Wrong number of files (%r) in wheel directory: %r' % (items, src)) wheelfile = items[0] wheel_info = util.WHEEL_FILE_RE.match(wheelfile) assert wheel_info is not None, ( 'Not a wheel file? %r' % os.path.join(src, wheelfile)) src_path = os.path.join(src, wheelfile) zipfix.reset_all_timestamps_in_zip(src_path) plat_tag = '' if not wheelfile.endswith('none-any.whl'): plat_tag = util.platform_tag() with open(src_path, 'rb') as f: digest = hashlib.sha1(f.read()) wheel_sha = digest.hexdigest() dest_path = os.path.join(dst, '{}-{}_{}{}{}'.format( wheel_info.group('namever'), build_num, wheel_sha, plat_tag, wheel_info.group(4), )) shutil.copyfile(src_path, dest_path) return dest_path
def test_reset_timestamps(self): with util.temporary_directory(prefix='glyco-zipfix-') as tempdir: # Create an archive zipname = os.path.join(tempdir, 'testfile.zip') with zipfile.ZipFile(zipname, 'w') as f: f.write(os.path.join(DATA_DIR, 'zipfix_test', 'file1.txt')) f.write(os.path.join(DATA_DIR, 'zipfix_test', 'file2.txt')) # Read original state with zipfile.ZipFile(zipname, 'r') as f: dt_orig = [info.date_time for info in f.infolist()] namelist_orig = f.namelist() namelist_orig.sort() hashes_orig = [ hashlib.sha1(f.read(filename)).hexdigest() for filename in namelist_orig ] # Reset zipfix.reset_all_timestamps_in_zip(zipname) # Make sure only timestamps have changed. with zipfile.ZipFile(zipname, 'r') as f: dt_new = [info.date_time for info in f.infolist()] namelist_new = f.namelist() namelist_new.sort() hashes_new = [ hashlib.sha1(f.read(filename)).hexdigest() for filename in namelist_new ] self.assertEqual(namelist_orig, namelist_new) self.assertEqual(hashes_orig, hashes_new) self.assertNotEqual(dt_orig, dt_new) for dt in dt_new: self.assertEqual(dt, (1980, 0, 0, 0, 0, 0))