7 def uncompress_file_to_dir(compressed_file
, uncompress_dir
):
8 extension
= os
.path
.splitext(compressed_file
)[1]
10 os
.makedirs(uncompress_dir
, exist_ok
=True)
12 if extension
== '.gz':
13 with tarfile
.open(compressed_file
) as tar
:
14 tar
.extractall(uncompress_dir
)
15 elif extension
== '.zip':
16 with zipfile
.ZipFile(compressed_file
) as zip_file
:
17 zip_file
.extractall(uncompress_dir
)
19 uncompress_dir
= os
.path
.join(uncompress_dir
, os
.listdir(uncompress_dir
)[0])
20 if " " in os
.listdir(uncompress_dir
)[0]:
21 print("replacing whitespace in directory name")
22 os
.rename(os
.path
.join(uncompress_dir
, os
.listdir(uncompress_dir
)[0]),
23 os
.path
.join(uncompress_dir
, os
.listdir(uncompress_dir
)[0].replace(" ", "_")))
25 print("Error: unknown extension " + extension
)
27 return os
.path
.join(uncompress_dir
, os
.listdir(uncompress_dir
)[0])
33 def get_hash(file_path
):
34 sha512
= hashlib
.sha512()
35 with
open(file_path
, 'rb') as f
:
36 while data
:= f
.read(BUF_SIZE
):
38 return sha512
.hexdigest()
41 def get_file_info(mar_file
, url
):
42 filesize
= os
.path
.getsize(mar_file
)
43 data
= {'hash': get_hash(mar_file
),
44 'hashFunction': 'sha512',
46 'url': url
+ os
.path
.basename(mar_file
)}
51 def replace_variables_in_string(string
, **kwargs
):
53 for key
, val
in kwargs
.items():
54 new_string
= new_string
.replace('$(%s)' % key
, val
)
59 def make_complete_mar_name(target_dir
, filename_prefix
):
60 filename
= filename_prefix
+ "_complete.mar"
61 return os
.path
.join(target_dir
, filename
)