tdf#156260 sw floattable: avoid moving text from the last anchor to its precede
[LibreOffice.git] / bin / update / tools.py
blob35e635cf8336859036e2f54660e60cb03b16efad
1 import os
2 import hashlib
3 import zipfile
4 import tarfile
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(" ", "_")))
24 else:
25 print("Error: unknown extension " + extension)
27 return os.path.join(uncompress_dir, os.listdir(uncompress_dir)[0])
30 BUF_SIZE = 1048576
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):
37 sha512.update(data)
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',
45 'size': filesize,
46 'url': url + os.path.basename(mar_file)}
48 return data
51 def replace_variables_in_string(string, **kwargs):
52 new_string = string
53 for key, val in kwargs.items():
54 new_string = new_string.replace('$(%s)' % key, val)
56 return new_string
59 def make_complete_mar_name(target_dir, filename_prefix):
60 filename = filename_prefix + "_complete.mar"
61 return os.path.join(target_dir, filename)