9797ExtractEvent = 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.
@@ -151,21 +151,21 @@ def extract(location, kinds=extractcode.default_kinds, recurse=False):
151151 target = join (abspath (top ), extractcode .get_extraction_path (loc ))
152152 if TRACE :
153153 logger .debug ('extract:target: %(target)r' % locals ())
154- for xevent in extract_file (loc , target , kinds ):
154+ for xevent in extract_file (loc , target , kinds , replace_originals = replace_originals ):
155155 if TRACE :
156156 logger .debug ('extract:walk:extraction event: %(xevent)r' % locals ())
157157 yield xevent
158158
159159 if recurse :
160160 if TRACE :
161161 logger .debug ('extract:walk: recursing on target: %(target)r' % locals ())
162- for xevent in extract (target , kinds , recurse ):
162+ for xevent in extract (target if replace_originals else localtion , kinds , recurse , replace_originals = replace_originals ):
163163 if TRACE :
164164 logger .debug ('extract:walk:recurse:extraction event: %(xevent)r' % locals ())
165165 yield xevent
166166
167167
168- def extract_file (location , target , kinds = extractcode .default_kinds , verbose = False ):
168+ def extract_file (location , target , kinds = extractcode .default_kinds , verbose = False , replace_originals = False ):
169169 """
170170 Extract a single archive at `location` in the `target` directory if it is
171171 of a kind supported in the `kinds` kind tuple.
@@ -178,16 +178,22 @@ def extract_file(location, target, kinds=extractcode.default_kinds, verbose=Fals
178178 + getattr (extractor , '__module__' , '' )
179179 + '.' + getattr (extractor , '__name__' , '' ))
180180 if extractor :
181- yield ExtractEvent (location , target , done = False , warnings = [], errors = [])
181+ yield ExtractEvent (location , target if not replace_originals else location , done = False , warnings = [], errors = [])
182182 try :
183183 # extract first to a temp directory: if there is an error, the
184184 # extracted files will not be moved to target
185185 tmp_tgt = fileutils .get_temp_dir (prefix = 'scancode-extract-' )
186186 abs_location = abspath (expanduser (location ))
187187 warns = extractor (abs_location , tmp_tgt ) or []
188188 warnings .extend (warns )
189- fileutils .copytree (tmp_tgt , target )
190- fileutils .delete (tmp_tgt )
189+ if not replace_originals :
190+ fileutils .copytree (tmp_tgt , target )
191+ fileutils .delete (tmp_tgt )
192+ else :
193+ if TRACE :
194+ logger .debug ('extract_file: replace original with extracted content: %(location)r' % locals ())
195+ fileutils .delete (location )
196+ fileutils .copytree (tmp_tgt , location )
191197 except Exception as e :
192198 errors = [str (e ).strip (' \' "' )]
193199 if verbose :
0 commit comments