Skip to content

Commit 0474798

Browse files
committed
Improve 7z error handling and windows tests
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 13abfbd commit 0474798

16 files changed

Lines changed: 2119 additions & 162 deletions

src/extractcode/sevenzip.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ def get_7z_errors(stdout, stderr):
110110

111111
file_errors = find_7z_errors(stderr)
112112
if file_errors:
113-
return ' '.join(file_errors.strip('"\' ')).strip()
113+
return ' '.join(fe.strip('"\' ') for fe in file_errors).strip()
114114

115115
file_errors = find_7z_errors(stdout)
116116
if file_errors:
117-
return ' '.join(file_errors.strip('"\' ')).strip()
117+
return ' '.join(fe.strip('"\' ') for fe in file_errors).strip()
118118

119119

120120
def get_7z_warnings(stdout):
@@ -196,12 +196,20 @@ def extract(location, target_dir, arch_type='*', file_by_file=on_mac, log=on_mac
196196
None.
197197
"""
198198
assert location
199-
assert target_dir
200199
abs_location = os.path.abspath(os.path.expanduser(location))
201-
abs_target_dir = os.path.abspath(os.path.expanduser(target_dir))
202-
200+
if not os.path.exists(abs_location):
201+
raise ExtractErrorFailedToExtract(
202+
'The system cannot find the path specified: {}'.format(repr(abs_location)))
203+
203204
if is_rar(location):
204-
raise ExtractErrorFailedToExtract('RAR extraction disactivated')
205+
raise ExtractErrorFailedToExtract(
206+
'RAR extraction disactivated: {}'.format(repr(location)))
207+
208+
assert target_dir
209+
abs_target_dir = os.path.abspath(os.path.expanduser(target_dir))
210+
if not os.path.exists(abs_target_dir):
211+
raise ExtractErrorFailedToExtract(
212+
'The system cannot find the target path specified: {}'.format(repr(target_dir)))
205213

206214
extractor = extract_file_by_file if file_by_file else extract_all_files_at_once
207215
return extractor(
@@ -633,7 +641,7 @@ def parse_7z_listing(location, utf=False):
633641
print(text)
634642
print('--------------------------------------')
635643

636-
header_tail = re.split(header_sep, text, flags=MULTILINE)
644+
header_tail = re.split(header_sep, text, flags=re.MULTILINE)
637645
if len(header_tail) != 2:
638646
# we more than one a header, confusion entails.
639647
raise ExtractWarningIncorrectEntry(
@@ -645,7 +653,7 @@ def parse_7z_listing(location, utf=False):
645653

646654
# FIXME: do something with header and footer?
647655
_header, body = header_tail
648-
body_and_footer = re.split(body_sep, body, flags=MULTILINE)
656+
body_and_footer = re.split(body_sep, body, flags=re.MULTILINE)
649657
no_footer = len(body_and_footer) == 1
650658
multiple_footers = len(body_and_footer) > 2
651659
_footer = empty
@@ -665,7 +673,7 @@ def parse_7z_listing(location, utf=False):
665673
print(body)
666674

667675
path_blocks = [pb.strip() for pb in
668-
re.split(path_block_sep, body, flags=MULTILINE) if pb and pb.strip()]
676+
re.split(path_block_sep, body, flags=re.MULTILINE) if pb and pb.strip()]
669677

670678
if TRACE_DEEP:
671679
logger.debug('parse_7z_listing: path_blocks:')
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
[
2+
{
3+
"path":"0-REGTYPE",
4+
"size":"3765",
5+
"date":"2004-07-21 15:09:21",
6+
"is_file":true,
7+
"is_dir":false,
8+
"is_special":false,
9+
"is_hardlink":false,
10+
"is_symlink":false,
11+
"is_broken_link":false,
12+
"link_target":null
13+
},
14+
{
15+
"path":"0-REGTYPE-TEXT",
16+
"size":"19941",
17+
"date":"2004-07-21 15:09:21",
18+
"is_file":true,
19+
"is_dir":false,
20+
"is_special":false,
21+
"is_hardlink":false,
22+
"is_symlink":false,
23+
"is_broken_link":false,
24+
"link_target":null
25+
},
26+
{
27+
"path":"0-REGTYPE-VEEEERY_LONG_NAME_____________________________________________________________________________________________________________________155",
28+
"size":"3765",
29+
"date":"2004-07-21 15:09:21",
30+
"is_file":true,
31+
"is_dir":false,
32+
"is_special":false,
33+
"is_hardlink":false,
34+
"is_symlink":false,
35+
"is_broken_link":false,
36+
"link_target":null
37+
},
38+
{
39+
"path":"1-LNKTYPE",
40+
"size":"0",
41+
"date":"2004-07-21 15:09:21",
42+
"is_file":true,
43+
"is_dir":false,
44+
"is_special":false,
45+
"is_hardlink":true,
46+
"is_symlink":false,
47+
"is_broken_link":false,
48+
"link_target":"0-REGTYPE"
49+
},
50+
{
51+
"path":"2-SYMTYPE",
52+
"size":"17",
53+
"date":"2004-07-21 15:09:21",
54+
"is_file":true,
55+
"is_dir":false,
56+
"is_special":false,
57+
"is_hardlink":false,
58+
"is_symlink":true,
59+
"is_broken_link":false,
60+
"link_target":"testtar/0-REGTYPE"
61+
},
62+
{
63+
"path":"3-CHRTYPE",
64+
"size":"0",
65+
"date":"2004-07-21 15:09:21",
66+
"is_file":true,
67+
"is_dir":false,
68+
"is_special":false,
69+
"is_hardlink":false,
70+
"is_symlink":false,
71+
"is_broken_link":false,
72+
"link_target":null
73+
},
74+
{
75+
"path":"5-DIRTYPE",
76+
"size":"0",
77+
"date":"2004-07-21 15:09:21",
78+
"is_file":false,
79+
"is_dir":true,
80+
"is_special":false,
81+
"is_hardlink":false,
82+
"is_symlink":false,
83+
"is_broken_link":false,
84+
"link_target":null
85+
},
86+
{
87+
"path":"6-FIFOTYPE",
88+
"size":"0",
89+
"date":"2004-07-21 15:09:21",
90+
"is_file":true,
91+
"is_dir":false,
92+
"is_special":false,
93+
"is_hardlink":false,
94+
"is_symlink":false,
95+
"is_broken_link":false,
96+
"link_target":null
97+
},
98+
{
99+
"path":"S-SPARSE",
100+
"size":"49152",
101+
"date":"2004-07-21 15:09:21",
102+
"is_file":true,
103+
"is_dir":false,
104+
"is_special":false,
105+
"is_hardlink":false,
106+
"is_symlink":false,
107+
"is_broken_link":false,
108+
"link_target":null
109+
},
110+
{
111+
"path":"S-SPARSE-WITH-NULLS",
112+
"size":"49152",
113+
"date":"2004-07-21 15:09:21",
114+
"is_file":true,
115+
"is_dir":false,
116+
"is_special":false,
117+
"is_hardlink":false,
118+
"is_symlink":false,
119+
"is_broken_link":false,
120+
"link_target":null
121+
}
122+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
"/0-REGTYPE",
3+
"/0-REGTYPE-TEXT",
4+
"/0-REGTYPE-VEEEERY_LONG_NAME_____________________________________________________________________________________________________________________155",
5+
"/1-LNKTYPE",
6+
"/2-SYMTYPE",
7+
"/3-CHRTYPE",
8+
"/5-DIRTYPE/",
9+
"/6-FIFOTYPE",
10+
"/S-SPARSE",
11+
"/S-SPARSE-WITH-NULLS"
12+
]

0 commit comments

Comments
 (0)