Skip to content

Commit 197268b

Browse files
committed
add --replace-originals flag to extractcode
Signed-off-by: Maximilian Huber <maximilian.huber@tngtech.com>
1 parent 83395b1 commit 197268b

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/extractcode/extract.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
ExtractEvent = namedtuple('ExtractEvent', 'source target done warnings errors')
9898

9999

100-
def extract(location, kinds=extractcode.default_kinds, recurse=False):
100+
def extract(location, kinds=extractcode.default_kinds, recurse=False, replace_originals=False):
101101
"""
102102
Walk and extract any archives found at `location` (either a file or
103103
directory). Extract only archives of a kind listed in the `kinds` kind tuple.
@@ -116,10 +116,26 @@ def extract(location, kinds=extractcode.default_kinds, recurse=False):
116116
archives If `recurse` is false, then do not extract further an already
117117
extracted archive identified by the corresponding extract suffix location.
118118
119+
If `replace_originals` is True, the archives will be extracted in place.
120+
119121
Note that while the original file system is walked top-down, breadth-first,
120122
if recurse and a nested archive is found, it is extracted to full depth
121123
first before resuming the file system walk.
122124
"""
125+
events = list(extract_files(location, kinds, recurse))
126+
if replace_originals:
127+
for xevent in reversed(events):
128+
if xevent.done:
129+
source = xevent.source
130+
target = xevent.target
131+
if TRACE:
132+
logger.debug('extract:replace_originals: replace %(source)r by %(target)r' % locals())
133+
fileutils.delete(source)
134+
fileutils.copytree(target, source)
135+
fileutils.delete(target)
136+
return events
137+
138+
def extract_files(location, kinds=extractcode.default_kinds, recurse=False):
123139
ignored = partial(ignore.is_ignored, ignores=ignore.default_ignores, unignores={})
124140
if TRACE:
125141
logger.debug('extract:start: %(location)r recurse: %(recurse)r\n' % locals())

tests/extractcode/test_extract.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,55 @@ def test_extract_tree_recursive(self):
238238
check_no_error(result)
239239
check_files(test_dir, expected)
240240

241+
def test_extract_tree_recursive_replace_originals(self):
242+
expected = (
243+
'a/a.txt',
244+
'a/a.tar.gz/a/b/a.txt',
245+
'a/a.tar.gz/a/b/b.txt',
246+
'a/a.tar.gz/a/c/c.txt',
247+
'b/a.txt',
248+
'b/b.tar.gz/b/.svn/all-wcprops',
249+
'b/b.tar.gz/b/.svn/entries',
250+
'b/b.tar.gz/b/.svn/format',
251+
'b/b.tar.gz/b/a/a.txt',
252+
'b/b.tar.gz/b/a/.svn/all-wcprops',
253+
'b/b.tar.gz/b/a/.svn/entries',
254+
'b/b.tar.gz/b/a/.svn/format',
255+
'b/b.tar.gz/b/a/.svn/prop-base/a.tar.gz.svn-base',
256+
'b/b.tar.gz/b/a/.svn/text-base/a.tar.gz.svn-base',
257+
'b/b.tar.gz/b/a/.svn/text-base/a.txt.svn-base',
258+
'b/b.tar.gz/b/a/a.tar.gz/a/b/a.txt',
259+
'b/b.tar.gz/b/a/a.tar.gz/a/b/b.txt',
260+
'b/b.tar.gz/b/a/a.tar.gz/a/c/c.txt',
261+
'b/b.tar.gz/b/b/a.txt',
262+
'b/b.tar.gz/b/b/.svn/all-wcprops',
263+
'b/b.tar.gz/b/b/.svn/entries',
264+
'b/b.tar.gz/b/b/.svn/format',
265+
'b/b.tar.gz/b/b/.svn/text-base/a.txt.svn-base',
266+
'b/b.tar.gz/b/c/a.txt',
267+
'b/b.tar.gz/b/c/.svn/all-wcprops',
268+
'b/b.tar.gz/b/c/.svn/entries',
269+
'b/b.tar.gz/b/c/.svn/format',
270+
'b/b.tar.gz/b/c/.svn/prop-base/a.tar.gz.svn-base',
271+
'b/b.tar.gz/b/c/.svn/text-base/a.tar.gz.svn-base',
272+
'b/b.tar.gz/b/c/.svn/text-base/a.txt.svn-base',
273+
'b/b.tar.gz/b/c/a.tar.gz/a/b/a.txt',
274+
'b/b.tar.gz/b/c/a.tar.gz/a/b/b.txt',
275+
'b/b.tar.gz/b/c/a.tar.gz/a/c/c.txt',
276+
'c/a.txt',
277+
'c/a.tar.gz/a/b/a.txt',
278+
'c/a.tar.gz/a/b/b.txt',
279+
'c/a.tar.gz/a/c/c.txt',
280+
)
281+
test_dir = self.get_test_loc('extract/tree', copy=True)
282+
result = list(extract.extract(test_dir, recurse=True, replace_originals=True))
283+
check_no_error(result)
284+
check_files(test_dir, expected)
285+
# again
286+
result = list(extract.extract(test_dir, recurse=True))
287+
check_no_error(result)
288+
check_files(test_dir, expected)
289+
241290
def test_extract_tree_shallow_then_recursive(self):
242291
shallow = (
243292
'a/a.tar.gz',

0 commit comments

Comments
 (0)