Skip to content

Commit 53e15fd

Browse files
committed
Filter event errors in replace_originals block
This is cleaner than to do tis in the main loop. Also refine and format the documentation. Reported-by: Bryan Sutula @sutula Reference: #31 Reference: aboutcode-org/scancode-toolkit#2723 Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 8c86536 commit 53e15fd

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/extractcode/extract.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,32 @@ def extract(
8888
ignore_pattern=(),
8989
):
9090
"""
91-
Walk and extract any archives found at `location` (either a file or
92-
directory). Extract only archives of a kind listed in the `kinds` kind tuple.
91+
Walk and extract any archives found at ``location`` (either a file or
92+
directory). Extract only archives of a kind listed in the ``kinds`` kind
93+
tuple.
9394
94-
Return an iterable of ExtractEvent tuples for each extracted archive. This
95-
can be used to track extraction progress:
95+
Return an iterable of ExtractEvent for each extracted archive. This can be
96+
used to track extraction progress:
9697
9798
- one event is emitted just before extracting an archive. The ExtractEvent
98-
warnings and errors are empty. The `done` flag is False.
99+
warnings and errors are empty. The "done" flag is False.
99100
100101
- one event is emitted right after extracting an archive. The ExtractEvent
101-
warnings and errors contains warnings and errors if any. The `done` flag
102+
warnings and errors contains warnings and errors if any. The "done" flag
102103
is True.
103104
104-
If `recurse` is True, extract recursively archives nested inside other
105-
archives. If `recurse` is false, then do not extract further an already
105+
If ``recurse`` is True, extract recursively archives nested inside other
106+
archives. If ``recurse`` is false, then do not extract further an already
106107
extracted archive identified by the corresponding extract suffix location.
107108
108-
If `replace_originals` is True, the extracted archives are replaced by the
109-
extracted content.
109+
If ``replace_originals`` is True, the extracted archives are replaced by the
110+
extracted content, only if the extraction was successful.
110111
111112
``ignore_pattern`` is a list of glob patterns to ignore.
112113
113114
Note that while the original filesystem is walked top-down, breadth-first,
114-
if ``recurse`` and a nested archive is found, it is extracted at full depth
115-
first before resuming the filesystem walk.
115+
if ``recurse`` and a nested archive is found, it is extracted first
116+
recursively and at full depth-first before resuming the filesystem walk.
116117
"""
117118

118119
extract_events = extract_files(
@@ -126,27 +127,19 @@ def extract(
126127
processed_events_append = processed_events.append
127128
for event in extract_events:
128129
yield event
129-
if event.warnings or event.errors:
130-
if TRACE:
131-
logger.debug(
132-
f'extract:replace_originals: {event} has errors. '
133-
'not replacing originals'
134-
)
135-
continue
136130
if replace_originals:
137131
processed_events_append(event)
138132

139-
140-
# move files around when done
133+
# move files around when done, unless there are errors
141134
if replace_originals:
142135
for xevent in reversed(processed_events):
143-
if xevent.done:
136+
if xevent.done and not (event.warnings or event.errors):
144137
source = xevent.source
145138
target = xevent.target
146139
if TRACE:
147140
logger.debug(
148-
'extract:replace_originals: replace '
149-
'%(source)r by %(target)r' % locals()
141+
f'extract:replace_originals: replacing '
142+
f'{source!r} by {target!r}'
150143
)
151144
fileutils.delete(source)
152145
fileutils.copytree(target, source)

0 commit comments

Comments
 (0)