@@ -69,25 +69,28 @@ def report_progress(count, blockSize, totalSize):
69
69
70
70
def unpack (filename , destination ):
71
71
dirname = ''
72
- print ('Extracting {0} ...' .format (os .path .basename (filename )))
73
- sys .stdout .flush ()
74
72
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 ]
78
75
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 ]
82
78
else :
83
79
raise NotImplementedError ('Unsupported archive type' )
84
80
85
81
# a little trick to rename tool directories so they don't contain version number
86
82
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
+
87
92
if rename_to != dirname :
88
93
print ('Renaming {0} to {1} ...' .format (dirname , rename_to ))
89
- if os .path .isdir (rename_to ):
90
- shutil .rmtree (rename_to )
91
94
shutil .move (dirname , rename_to )
92
95
93
96
def download_file_with_progress (url ,filename ):
0 commit comments