Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_write_str(wheel_path):
with WheelFile(wheel_path, 'w') as wf:
wf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, world!")\n'))
wf.writestr(native('hello/h,ll,.py'), as_bytes('print("Héllö, world!")\n'))
with ZipFile(wheel_path, 'r') as zf:
infolist = zf.infolist()
assert len(infolist) == 3
assert infolist[0].filename == native('hello/héllö.py')
assert infolist[0].file_size == 25
assert infolist[1].filename == native('hello/h,ll,.py')
assert infolist[1].file_size == 25
assert infolist[2].filename == 'test-1.0.dist-info/RECORD'
record = zf.read('test-1.0.dist-info/RECORD')
assert record == as_bytes(
'hello/héllö.py,sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25\n'
'"hello/h,ll,.py",sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25\n'
def test_weak_hash_algorithm(wheel_path, algorithm, digest):
hash_string = '{}={}'.format(algorithm, digest)
with ZipFile(wheel_path, 'w') as zf:
zf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, w0rld!")\n'))
zf.writestr('test-1.0.dist-info/RECORD',
as_bytes('hello/héllö.py,{},25'.format(hash_string)))
exc = pytest.raises(WheelError, WheelFile, wheel_path)
exc.match(r"^Weak hash algorithm \({}\) is not permitted by PEP 427$".format(algorithm))
if eof is None:
eof = ef._eof
update_crc_orig(newdata)
else: # Python 2
update_crc_orig(newdata, eof)
running_hash.update(newdata)
if eof and running_hash.digest() != expected_hash:
raise WheelError("Hash mismatch for file '{}'".format(native(ef_name)))
ef = ZipFile.open(self, name_or_info, mode, pwd)
ef_name = as_unicode(name_or_info.filename if isinstance(name_or_info, ZipInfo)
else name_or_info)
if mode == 'r' and not ef_name.endswith('/'):
if ef_name not in self._file_hashes:
raise WheelError("No hash found for file '{}'".format(native(ef_name)))
algorithm, expected_hash = self._file_hashes[ef_name]
if expected_hash is not None:
# Monkey patch the _update_crc method to also check for the hash from RECORD
running_hash = hashlib.new(algorithm)
update_crc_orig, ef._update_crc = ef._update_crc, _update_crc
return ef
def iter_files(self):
files = os.path.join(self.path, 'info', 'files')
with open(files) as f:
return [native(line.strip()) for line in f.readlines()]
def load(self):
# XXX JSON is not a great database
for path in load_config_paths('wheel'):
conf = os.path.join(native(path), self.CONFIG_NAME)
if os.path.exists(conf):
with open(conf, 'r') as infile:
self.data = json.load(infile)
for x in ('signers', 'verifiers'):
if x not in self.data:
self.data[x] = []
if 'schema' not in self.data:
self.data['schema'] = self.SCHEMA
elif self.data['schema'] != self.SCHEMA:
raise ValueError(
"Bad wheel.json version {0}, expected {1}".format(
self.data['schema'], self.SCHEMA))
break
return self
def skip(path):
"""Wheel hashes every possible file."""
return (path == record_relpath)
with _open_for_csv(record_path, 'w+') as record_file:
writer = csv.writer(record_file)
for path in walk():
relative_path = relpath(path, bdist_dir)
if skip(relative_path):
hash = ''
size = ''
else:
with open(path, 'rb') as f:
data = f.read()
digest = hashlib.sha256(data).digest()
hash = 'sha256=' + native(urlsafe_b64encode(digest))
size = len(data)
path_for_record = relpath(
path, bdist_dir).replace(psep, '/')
writer.writerow((path_for_record, hash, size))