summaryrefslogtreecommitdiffstats
path: root/bin/update/tools.py
blob: 6bc3f7971fc8b7994396eb9ea43c031a93fe779b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import hashlib
import subprocess

from path import convert_to_native


def uncompress_file_to_dir(compressed_file, uncompress_dir):
    os.makedirs(uncompress_dir, exist_ok=True)

    if subprocess.call([
            'msiexec', '/a', convert_to_native(compressed_file).replace('/', '\\'),
            '/quiet',
            'TARGETDIR=' + convert_to_native(uncompress_dir).replace('/', '\\')]) != 0:
        raise Exception(f'msiexec failed')

    return uncompress_dir


BUF_SIZE = 1048576


def get_hash(file_path):
    sha512 = hashlib.sha512()
    with open(file_path, 'rb') as f:
        while data := f.read(BUF_SIZE):
            sha512.update(data)
        return sha512.hexdigest()


def get_file_info(mar_file, url):
    filesize = os.path.getsize(mar_file)
    data = {'hash': get_hash(mar_file),
            'hash_function': 'sha512',
            'size': filesize,
            'url': url + os.path.basename(mar_file)}

    return data


def replace_variables_in_string(string, **kwargs):
    new_string = string
    for key, val in kwargs.items():
        new_string = new_string.replace('$(%s)' % key, val)

    return new_string


def make_complete_mar_name(target_dir, filename_prefix):
    filename = filename_prefix + "_complete.mar"
    return os.path.join(target_dir, filename)