1import os 2import hashlib 3import zipfile 4import tarfile 5 6def uncompress_file_to_dir(compressed_file, uncompress_dir): 7 command = None 8 extension = os.path.splitext(compressed_file)[1] 9 10 try: 11 os.mkdir(uncompress_dir) 12 except FileExistsError as e: 13 pass 14 15 if extension == '.gz': 16 tar = tarfile.open(compressed_file) 17 tar.extractall(uncompress_dir) 18 tar.close() 19 elif extension == '.zip': 20 zip_file = zipfile.ZipFile(compressed_file) 21 zip_file.extractall(uncompress_dir) 22 zip_file.close() 23 24 uncompress_dir = os.path.join(uncompress_dir, os.listdir(uncompress_dir)[0]) 25 if " " in os.listdir(uncompress_dir)[0]: 26 print("replacing whitespace in directory name") 27 os.rename(os.path.join(uncompress_dir, os.listdir(uncompress_dir)[0]), 28 os.path.join(uncompress_dir, os.listdir(uncompress_dir)[0].replace(" ", "_"))) 29 else: 30 print("Error: unknown extension " + extension) 31 32 return os.path.join(uncompress_dir, os.listdir(uncompress_dir)[0]) 33 34BUF_SIZE = 1048576 35 36def get_hash(file_path): 37 sha512 = hashlib.sha512() 38 with open(file_path, 'rb') as f: 39 while True: 40 data = f.read(BUF_SIZE) 41 if not data: 42 break 43 sha512.update(data) 44 return sha512.hexdigest() 45 46def get_file_info(mar_file, url): 47 filesize = os.path.getsize(mar_file) 48 data = { 'hash' : get_hash(mar_file), 49 'hashFunction' : 'sha512', 50 'size' : filesize, 51 'url' : url + os.path.basename(mar_file)} 52 53 return data 54 55def replace_variables_in_string(string, **kwargs): 56 new_string = string 57 for key, val in kwargs.items(): 58 new_string = new_string.replace('$(%s)'%key, val) 59 60 return new_string 61 62def make_complete_mar_name(target_dir, filename_prefix): 63 filename = filename_prefix + "_complete.mar" 64 return os.path.join(target_dir, filename) 65