Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / bin / update / tools.py
blob8cd786635f0dd0cf8437f2d79e64df4b9ca1f9c7
1 import os
2 import hashlib
3 import zipfile
4 import tarfile
6 def uncompress_file_to_dir(compressed_file, uncompress_dir):
7 command = None
8 extension = os.path.splitext(compressed_file)[1]
10 try:
11 os.mkdir(uncompress_dir)
12 except FileExistsError as e:
13 pass
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()
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)
32 return os.path.join(uncompress_dir, os.listdir(uncompress_dir)[0])
34 BUF_SIZE = 1048576
36 def 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()
46 def 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)}
53 return data
55 def 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)
60 return new_string
62 def make_complete_mar_name(target_dir, filename_prefix):
63 filename = filename_prefix + "_complete.mar"
64 return os.path.join(target_dir, filename)