This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
79 lines (64 loc) · 2.32 KB
/
functions.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import re
import subprocess
from typing import List, Tuple
from file import File
from meta import commit, compile_time, version
def replace_variables(text: str, var: dict = {}):
for i in range(0, 1000):
replaced = False
for k in var.keys():
new = text.replace(f'${k}', var[k])
if new != text:
replaced = True
text = new
if not replaced:
break
return text
def run_subprocess(command: str, cwd: str, encoding: str, check_return_code: bool) -> str:
outputs = ''
with subprocess.Popen(args=command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd) as process:
with process.stdout as stdout:
while True:
data = stdout.read()
if len(data) == 0:
break
outputs += data.decode(encoding)
retcode = process.poll()
if retcode is None:
process.kill()
retcode = process.poll()
if check_return_code and retcode:
raise subprocess.CalledProcessError(retcode, process.args, output=outputs, stderr=None)
return outputs
def calculate_dir_structure(dir: File):
structure = []
for f in dir:
if f.isFile:
structure.append({
'name': f.name,
'length': f.length,
'hash': f.sha1,
'modified': f.modified
})
if f.isDirectory:
structure.append({
'name': f.name,
'tree': calculate_dir_structure(f)
})
return structure
def filter_files(pattern: str, ls: List[str]) -> Tuple[str]:
return [e for e in filter(lambda e: pattern == '' or re.fullmatch(pattern, e), ls)]
def with_progress(list: List[str]) -> Tuple[int, int, str]:
total = len(list)
result = []
index = 0
for el in list:
result += [(index, total, el)]
index += 1
return result
def print_metadata():
commit_sha = commit[:8] if len(commit) > 16 else commit
commit_sha_in_short = commit_sha if len(commit) > 0 else 'dev'
print(f'AppVersion: {version} ({commit_sha_in_short}), CompileTime: {compile_time}')
def filter_not_none(obj: dict):
return { k: v for k, v in obj.items() if v is not None }