Skip to content

Commit bcb92a1

Browse files
authored
fix(get.py): Clear old files before extracting (espressif#10188)
1 parent 54c4b0c commit bcb92a1

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Diff for: tools/get.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,28 @@ def report_progress(count, blockSize, totalSize):
6969

7070
def unpack(filename, destination):
7171
dirname = ''
72-
print('Extracting {0} ...'.format(os.path.basename(filename)))
73-
sys.stdout.flush()
7472
if filename.endswith('tar.gz'):
75-
tfile = tarfile.open(filename, 'r:gz')
76-
tfile.extractall(destination)
77-
dirname = tfile.getnames()[0]
73+
cfile = tarfile.open(filename, 'r:gz')
74+
dirname = cfile.getnames()[0].split('/')[0]
7875
elif filename.endswith('zip'):
79-
zfile = zipfile.ZipFile(filename)
80-
zfile.extractall(destination)
81-
dirname = zfile.namelist()[0]
76+
cfile = zipfile.ZipFile(filename)
77+
dirname = cfile.namelist()[0].split('/')[0]
8278
else:
8379
raise NotImplementedError('Unsupported archive type')
8480

8581
# a little trick to rename tool directories so they don't contain version number
8682
rename_to = re.match(r'^([a-z][^\-]*\-*)+', dirname).group(0).strip('-')
83+
84+
if os.path.isdir(os.path.join(destination, rename_to)):
85+
print('Removing existing {0} ...'.format(rename_to))
86+
shutil.rmtree(os.path.join(destination, rename_to), ignore_errors=True)
87+
88+
print('Extracting {0} ...'.format(os.path.basename(filename)))
89+
sys.stdout.flush()
90+
cfile.extractall(destination)
91+
8792
if rename_to != dirname:
8893
print('Renaming {0} to {1} ...'.format(dirname, rename_to))
89-
if os.path.isdir(rename_to):
90-
shutil.rmtree(rename_to)
9194
shutil.move(dirname, rename_to)
9295

9396
def download_file_with_progress(url,filename):

0 commit comments

Comments
 (0)