Skip to content

Commit 8eca070

Browse files
authored
Merge pull request #1946 from JRavi2/add-ignore-flag
Add --ignore flag to extractcode
2 parents b176aae + 8527e10 commit 8eca070

File tree

6 files changed

+66
-7
lines changed

6 files changed

+66
-7
lines changed

src/extractcode/archive.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from commoncode import fileutils
3535
from commoncode import filetype
3636
from commoncode import functional
37+
from commoncode.ignore import is_ignored
3738
from commoncode.system import on_linux
3839
from commoncode.system import py2
3940

@@ -103,13 +104,15 @@ def can_extract(location):
103104
return True
104105

105106

106-
def should_extract(location, kinds):
107+
def should_extract(location, kinds, ignore_pattern=()):
107108
"""
108109
Return True if this location should be extracted based on the provided
109110
kinds
110111
"""
111112
location = os.path.abspath(os.path.expanduser(location))
112-
if get_extractor(location, kinds):
113+
ignore_pattern = {extension : 'User ignore: Supplied by --ignore' for extension in ignore_pattern}
114+
should_ignore = is_ignored(location, ignore_pattern)
115+
if get_extractor(location, kinds) and not should_ignore:
113116
return True
114117

115118

src/extractcode/extract.py

Lines changed: 5 additions & 5 deletions
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, replace_originals=False):
100+
def extract(location, kinds=extractcode.default_kinds, recurse=False, replace_originals=False, ignore_pattern=()):
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.
@@ -125,7 +125,7 @@ def extract(location, kinds=extractcode.default_kinds, recurse=False, replace_or
125125
"""
126126
processed_events = []
127127
processed_events_append = processed_events.append
128-
for event in extract_files(location, kinds, recurse):
128+
for event in extract_files(location, kinds, recurse, ignore_pattern):
129129
yield event
130130
if replace_originals:
131131
processed_events_append(event)
@@ -143,7 +143,7 @@ def extract(location, kinds=extractcode.default_kinds, recurse=False, replace_or
143143
fileutils.delete(target)
144144

145145

146-
def extract_files(location, kinds=extractcode.default_kinds, recurse=False):
146+
def extract_files(location, kinds=extractcode.default_kinds, recurse=False, ignore_pattern=()):
147147
"""
148148
Extract the files found at `location`.
149149
@@ -177,7 +177,7 @@ def extract_files(location, kinds=extractcode.default_kinds, recurse=False):
177177
logger.debug('extract:walk not recurse: skipped file: %(loc)r' % locals())
178178
continue
179179

180-
if not archive.should_extract(loc, kinds):
180+
if not archive.should_extract(loc, kinds, ignore_pattern):
181181
if TRACE:
182182
logger.debug('extract:walk: skipped file: not should_extract: %(loc)r' % locals())
183183
continue
@@ -195,7 +195,7 @@ def extract_files(location, kinds=extractcode.default_kinds, recurse=False):
195195
if recurse:
196196
if TRACE:
197197
logger.debug('extract:walk: recursing on target: %(target)r' % locals())
198-
for xevent in extract(target, kinds, recurse):
198+
for xevent in extract(location=target, kinds=kinds, recurse=recurse, ignore_pattern=ignore_pattern):
199199
if TRACE:
200200
logger.debug('extract:walk:recurse:extraction event: %(xevent)r' % locals())
201201
yield xevent
716 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

tests/extractcode/test_extract.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,3 +856,59 @@ def test_extract_always_returns_a_generator_and_not_a_list(self):
856856
test_dir = self.get_test_loc('extract/generator', copy=True)
857857
result = extract.extract(test_dir)
858858
assert isinstance(result, GeneratorType)
859+
860+
def test_extract_ignore_file(self):
861+
test_dir = self.get_test_loc('extract/ignore', copy=True)
862+
expected = [
863+
'alpha.zip',
864+
'beta.tar',
865+
'beta.tar-extract/a.txt',
866+
'beta.tar-extract/b.txt',
867+
'beta.tar-extract/c.txt',
868+
'gamma/gamma.zip',
869+
'gamma/gamma.zip-extract/c.txt'
870+
]
871+
from extractcode import default_kinds
872+
result = list(extract.extract(test_dir, recurse=True, ignore_pattern=('alpha.zip',)))
873+
check_no_error(result)
874+
check_files(test_dir, expected)
875+
876+
def test_extract_ignore_directory(self):
877+
test_dir = self.get_test_loc('extract/ignore', copy=True)
878+
expected = [
879+
'alpha.zip',
880+
'alpha.zip-extract/a.txt',
881+
'alpha.zip-extract/beta.zip',
882+
'alpha.zip-extract/beta.zip-extract/b.txt',
883+
'alpha.zip-extract/gamma.tar',
884+
'alpha.zip-extract/gamma.tar-extract/c.txt',
885+
'beta.tar',
886+
'beta.tar-extract/a.txt',
887+
'beta.tar-extract/b.txt',
888+
'beta.tar-extract/c.txt',
889+
'gamma/gamma.zip',
890+
]
891+
from extractcode import default_kinds
892+
result = list(extract.extract(test_dir, recurse=True, ignore_pattern=('gamma',)))
893+
check_no_error(result)
894+
check_files(test_dir, expected)
895+
896+
def test_extract_ignore_pattern(self):
897+
test_dir = self.get_test_loc('extract/ignore', copy=True)
898+
expected = [
899+
'alpha.zip',
900+
'alpha.zip-extract/a.txt',
901+
'alpha.zip-extract/beta.zip',
902+
'alpha.zip-extract/gamma.tar',
903+
'alpha.zip-extract/gamma.tar-extract/c.txt',
904+
'beta.tar',
905+
'beta.tar-extract/a.txt',
906+
'beta.tar-extract/b.txt',
907+
'beta.tar-extract/c.txt',
908+
'gamma/gamma.zip',
909+
'gamma/gamma.zip-extract/c.txt'
910+
]
911+
from extractcode import default_kinds
912+
result = list(extract.extract(test_dir, recurse=True, ignore_pattern=('b*.zip',)))
913+
check_no_error(result)
914+
check_files(test_dir, expected)

0 commit comments

Comments
 (0)