Skip to content

Commit

Permalink
Fix handling of special characters in filenames in RECORD file
Browse files Browse the repository at this point in the history
  • Loading branch information
adang1345 committed Sep 14, 2023
1 parent 006ed6e commit 2c9b752
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
9 changes: 5 additions & 4 deletions delvewheel/_wheel_repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ast
import base64
import csv
import hashlib
import os
import pathlib
Expand Down Expand Up @@ -951,13 +952,13 @@ def repair(self, target: str, no_mangles: set, no_mangle_all: bool, strip: bool,
for file in files:
filepath_list.append(os.path.join(root, file))
with open(record_filepath, 'w', newline='\n') as record_file:
writer = csv.writer(record_file, lineterminator='\n')
for file_path in filepath_list:
if file_path == record_filepath:
record_file.write(os.path.relpath(record_filepath, self._extract_dir).replace('\\', '/'))
record_file.write(',,\n')
writer.writerow((os.path.relpath(record_filepath, self._extract_dir).replace('\\', '/'), '', ''))
else:
record_line = '{},sha256={},{}\n'.format(os.path.relpath(file_path, self._extract_dir).replace('\\', '/'), *self._rehash(file_path))
record_file.write(record_line)
hash, size = self._rehash(file_path)
writer.writerow((os.path.relpath(file_path, self._extract_dir).replace('\\', '/'), f'sha256={hash}', size))

# repackage wheel
print('repackaging wheel')
Expand Down
11 changes: 6 additions & 5 deletions tests/fix_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
is modified manually for testing purposes."""

import base64
import csv
import hashlib
import os
import sys
Expand Down Expand Up @@ -35,14 +36,14 @@ def _rehash(file_path: str) -> typing.Tuple[str, int]:
for root, _, files in os.walk(extract_dir):
for file in files:
filepath_list.append(os.path.join(root, file))
with open(record_filepath, 'w') as record_file:
with open(record_filepath, 'w', newline='\n') as record_file:
writer = csv.writer(record_file, lineterminator='\n')
for file_path in filepath_list:
if file_path == record_filepath:
record_file.write(os.path.relpath(record_filepath, extract_dir).replace('\\', '/'))
record_file.write(',,\n')
writer.writerow((os.path.relpath(record_filepath, extract_dir).replace('\\', '/'), '', ''))
else:
record_line = '{},sha256={},{}\n'.format(os.path.relpath(file_path, extract_dir).replace('\\', '/'), *_rehash(file_path))
record_file.write(record_line)
hash, size = _rehash(file_path)
writer.writerow((os.path.relpath(file_path, extract_dir).replace('\\', '/'), f'sha256={hash}', size))

# repackage wheel
with zipfile.ZipFile(whl_path, 'w', zipfile.ZIP_DEFLATED) as whl_file:
Expand Down
10 changes: 10 additions & 0 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,16 @@ def test_include_symbols1(self):
self.assertTrue(os.path.exists(os.path.join(tempdir, 'simpleext.libs/simpledll.pdb')))
self.assertTrue(os.path.exists(os.path.join(tempdir, 'ns/simpledll.pdb')))

def test_filename_special_character(self):
"""RECORD is fixed correctly when filename contains the ',' special
character."""
check_call(['delvewheel', 'repair', '--add-path', 'simpleext/x64', 'simpleext/simpleext-0.0.1-0record-cp310-cp310-win_amd64.whl'])
with tempfile.TemporaryDirectory() as tempdir:
with zipfile.ZipFile('wheelhouse/simpleext-0.0.1-0record-cp310-cp310-win_amd64.whl') as whl_file:
whl_file.extractall(tempdir)
with open(os.path.join(tempdir, 'simpleext-0.0.1.dist-info/RECORD')) as file:
self.assertTrue(any(line.startswith('"simpleext-0.0.1.data/data/a,b.txt"') for line in file))


class NeededTestCase(unittest.TestCase):
"""Tests for delvewheel needed"""
Expand Down
Binary file not shown.

0 comments on commit 2c9b752

Please sign in to comment.