Skip to content

Commit d542cc5

Browse files
committed
Ensure extraction still works on Python 2.7
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 3c121c6 commit d542cc5

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/extractcode/sevenzip.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import pprint
3535
import re
3636
from re import MULTILINE # NOQA
37-
import shlex
3837

3938
import attr
4039

@@ -52,10 +51,17 @@
5251
from extractcode import ExtractErrorFailedToExtract
5352
from extractcode import ExtractWarningIncorrectEntry
5453

54+
if py3:
55+
from shlex import quote as shlex_quote
56+
else:
57+
from pipes import quote as shlex_quote
58+
59+
5560
"""
5661
Low level support for p/7zip-based archive extraction.
5762
"""
5863

64+
5965
logger = logging.getLogger(__name__)
6066

6167
TRACE = False
@@ -304,7 +310,7 @@ def build_7z_extract_command(
304310
]
305311

306312
if single_entry:
307-
args += [shlex.quote(single_entry.path)]
313+
args += [shlex_quote(single_entry.path)]
308314

309315
lib_dir, cmd_loc = get_bin_locations()
310316

@@ -675,7 +681,13 @@ def parse_7z_listing(location, utf=False):
675681

676682
lines = path_block.splitlines(False)
677683
# thfirst line is the Path line
678-
path = lines.pop(0).strip()
684+
path_line = lines.pop(0).strip()
685+
if 'Path =' in path_line:
686+
_, _, path= path_line.partition('Path =')
687+
path = path.lstrip()
688+
else:
689+
path = path_line
690+
679691
second = lines[0]
680692

681693
if equal_sep not in second:

tests/extractcode/test_sevenzip.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,18 @@ def check_results_with_expected_json(self, results, expected_loc, regen=False):
5050
with open(expected_loc, wmode) as ex:
5151
json.dump(results, ex, indent=2, separators=(',', ':'))
5252

53-
oph = OrderedDict if py2 else dict
53+
# oph = OrderedDict if py2 else dict
5454

5555
with open(expected_loc, 'rb') as ex:
56-
expected = json.load(ex, encoding='utf-8', object_pairs_hook=oph)
56+
expected = json.load(ex, encoding='utf-8', object_pairs_hook=dict)
5757

5858
try:
5959
assert expected == results
6060
except AssertionError:
61-
assert json.dumps(expected, indent=2) == json.dumps(results, indent=2)
61+
if isinstance(results, dict):
62+
expected = expected.items()
63+
results = results.items()
64+
assert json.dumps(sorted(expected), indent=2) == json.dumps(sorted(results), indent=2)
6265

6366
def test_get_7z_errors_password_protected(self):
6467
test = '''

0 commit comments

Comments
 (0)