fix(get.py): Clear old files before extracting (#10188)

This commit is contained in:
Lucas Saavedra Vaz 2024-08-22 17:46:50 -03:00 committed by GitHub
parent 54c4b0c619
commit bcb92a14a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -69,25 +69,28 @@ def report_progress(count, blockSize, totalSize):
def unpack(filename, destination):
dirname = ''
print('Extracting {0} ...'.format(os.path.basename(filename)))
sys.stdout.flush()
if filename.endswith('tar.gz'):
tfile = tarfile.open(filename, 'r:gz')
tfile.extractall(destination)
dirname = tfile.getnames()[0]
cfile = tarfile.open(filename, 'r:gz')
dirname = cfile.getnames()[0].split('/')[0]
elif filename.endswith('zip'):
zfile = zipfile.ZipFile(filename)
zfile.extractall(destination)
dirname = zfile.namelist()[0]
cfile = zipfile.ZipFile(filename)
dirname = cfile.namelist()[0].split('/')[0]
else:
raise NotImplementedError('Unsupported archive type')
# a little trick to rename tool directories so they don't contain version number
rename_to = re.match(r'^([a-z][^\-]*\-*)+', dirname).group(0).strip('-')
if os.path.isdir(os.path.join(destination, rename_to)):
print('Removing existing {0} ...'.format(rename_to))
shutil.rmtree(os.path.join(destination, rename_to), ignore_errors=True)
print('Extracting {0} ...'.format(os.path.basename(filename)))
sys.stdout.flush()
cfile.extractall(destination)
if rename_to != dirname:
print('Renaming {0} to {1} ...'.format(dirname, rename_to))
if os.path.isdir(rename_to):
shutil.rmtree(rename_to)
shutil.move(dirname, rename_to)
def download_file_with_progress(url,filename):