MCPcopy Create free account
hub / github.com/PacktPublishing/3D-Graphics-Rendering-Cookbook / extractFile

Function extractFile

deps/bootstrap.py:196–279  ·  view source on GitHub ↗
(filename, target_dir)

Source from the content-addressed store, hash-verified

194
195
196def extractFile(filename, target_dir):
197 if os.path.exists(target_dir):
198 shutil.rmtree(target_dir)
199
200 log("Extracting file " + filename)
201 stem, extension = os.path.splitext(os.path.basename(filename))
202
203 if extension == ".zip" or extension == "":
204 zfile = zipfile.ZipFile(filename)
205 extract_dir = os.path.commonprefix(zfile.namelist())
206 hasFolder = False
207 for fname in zfile.namelist():
208 if fname.find('/') != -1:
209 hasFolder = True
210 extract_dir_local = ""
211 if not hasFolder: # special case, there are no folders in the archive
212 extract_dir = ""
213 if extract_dir == "": # deal with stupid zip files that don't contain a base directory
214 extract_dir, extension2 = os.path.splitext(os.path.basename(filename))
215 extract_dir_local = extract_dir
216 extract_dir_abs = os.path.join(SRC_DIR, extract_dir_local)
217
218 try:
219 os.mkdirs(extract_dir_abs)
220 except:
221 pass
222
223 if not USE_UNZIP:
224 zfile.extractall(extract_dir_abs)
225 zfile.close()
226 else:
227 zfile.close()
228 dieIfNonZero(executeCommand(TOOL_COMMAND_UNZIP + " " + filename + " -d " + extract_dir_abs))
229
230 elif extension == ".tar" or extension == ".gz" or extension == ".bz2" or extension == ".xz":
231
232 if extension == ".xz":# and not lzma_available:
233 stem2, extension2 = os.path.splitext(os.path.basename(stem))
234 if extension2 == ".tar":
235 # we extract the .tar.xz file to a .tar file before we uncompress that
236 tar_filename = os.path.join(os.path.dirname(filename), stem)
237 decompressTarXZFile(filename, tar_filename)
238 filename = tar_filename
239 else:
240 raise RuntimeError("Unable to extract .xz file that is not a .tar.xz file.")
241
242 tfile = tarfile.open(filename)
243 extract_dir = os.path.commonprefix(tfile.getnames())
244 extract_dir_local = ""
245 if extract_dir == "": # deal with stupid tar files that don't contain a base directory
246 extract_dir, extension2 = os.path.splitext(os.path.basename(filename))
247 extract_dir_local = extract_dir
248 extract_dir_abs = os.path.join(SRC_DIR, extract_dir_local)
249
250 try:
251 os.mkdirs(extract_dir_abs)
252 except:
253 pass

Callers 1

downloadAndExtractFileFunction · 0.85

Calls 4

logFunction · 0.85
dieIfNonZeroFunction · 0.85
executeCommandFunction · 0.85
decompressTarXZFileFunction · 0.85

Tested by

no test coverage detected