-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
78 lines (57 loc) · 1.66 KB
/
util.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
import platform
import sys
import multiprocessing
import shutil
import os
from os.path import dirname, isfile, isdir, exists
import hashlib
def system_info():
def gb(value):
return value / (1024**3)
def format_gb(number):
return '{:.2f} GB'.format(gb(number))
if platform.system() == 'Linux':
total_ram = format_gb(os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES'))
else:
total_ram = 'unknown'
loc_ds = dirname(__file__)
total_ds, used_ds, free_ds = [format_gb(i) for i in shutil.disk_usage(loc_ds)]
return [
['platform', platform.platform()],
['python version', sys.version],
['cores', str(multiprocessing.cpu_count())],
['total ram', total_ram],
['free disk space (' + loc_ds + ')', free_ds],
['total disk space (' + loc_ds + ')', total_ds],
['used disk space (' + loc_ds + ')', used_ds],
]
def error(msg):
print('ERROR: ' + str(msg), flush=True)
sys.exit(1)
def info(msg):
print('INFO: ' + str(msg), flush=True)
def warning(msg):
print('WARNING: ' + str(msg), flush=True)
def clear_dir(dirpath):
shutil.rmtree(dirpath, True)
os.makedirs(dirpath)
return dirpath
def make_key(s):
sha1 = hashlib.sha1()
sha1.update(s.encode('utf-8'))
return sha1.hexdigest()
def read_file(fpath):
with open(fpath, 'r') as f:
return f.read()
def write_file(fpath, contents):
with open(fpath, 'w') as f:
f.write(contents)
# copy a file and make sure that the destination
# path exists (all directories)
def copy(src, dest):
if exists(src):
os.makedirs(dirname(dest), exist_ok=True)
if isfile(src):
shutil.copyfile(src, dest)
elif isdir(src):
shutil.copytree(src, dest)