-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
executable file
·191 lines (155 loc) · 4.95 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
import datetime
import os
import shutil
import subprocess
import sys
import zlib
from collections import namedtuple
def chdir_wrap(dir_name, no_run=False):
if not no_run:
os.chdir(dir_name)
def run_cmd_and_log(dir_name, name, command, log_path=None):
if not log_path:
log_path = dir_name
chdir_wrap(dir_name)
file_append(log_path + 'log', name + '\n')
file_append(log_path + 'log', command + '\n')
ret_val = run_cmd(command + " &> " + log_path + name + '.log')
file_append(log_path + 'log', 'Exit code: ' + str(ret_val) + '\n\n')
return ret_val
def recursively_remove_dir(dir_name):
if os.path.isdir(dir_name):
shutil.rmtree(dir_name)
def get_dir_list(dir_name):
try:
lst = os.listdir(dir_name)
except:
return []
lst = [elem for elem in lst if elem != ".svn" and elem != "c_version"]
return lst
def create_unique_folder(basename):
count = 1
while True:
name = get_unique_dir_name(basename)
count = count + 1
if count > 25:
raise NameError('Unable to create a directory ' + name)
no_error = True
try:
os.makedirs(name)
except os.error as e:
no_error = False
if e.errno != errno.EEXIST:
raise
if no_error:
break
return name
def file_append(filename, data):
with open(filename, 'a') as f:
f.write(data)
def get_unique_dir_name(basename):
i = 1
if not os.path.isdir(basename):
return basename
while os.path.isdir(basename + "_" + str(i)):
i = i + 1
return basename + "_" + str(i)
def get_unique_file_name(basename):
i = 1
if not os.path.isfile(basename):
return basename
while os.path.isfile(basename + "_" + str(i)):
i = i + 1
return basename + "_" + str(i)
def run_cmd(cmd_line, no_run = False, print_cmd = True):
fullcommand = "bash -c \"" + cmd_line + "\""
if print_cmd:
print(fullcommand)
if not no_run:
return os.system(fullcommand)
return None
def get_nb_cpus():
nb = subprocess.getoutput('cat /proc/cpuinfo | grep processor | wc -l')
# TODO: Some error checking here
return int(nb)
def get_arch():
return subprocess.getoutput('uname -m')
def remove_suffix(var, suffix):
if var.endswith(suffix):
var = var[:-len(suffix)]
return var
def get_hostname():
hostname = os.uname()[1]
return remove_suffix(hostname, '.localdomain')
def get_svn_revision(path='.'):
(status, cur_svn_rev) = subprocess.getstatusoutput('svnversion ' + path)
if status == 0:
return cur_svn_rev
else:
return "unknow"
def get_monitor_pid():
(status, data) = subprocess.getstatusoutput('ps ax | grep tile-monitor | grep -v grep')
if data.strip(' ').split(' ')[0]:
return int(data.strip(' ').split(' ')[0])
else:
return 0
def compute_file_crc32(filename, noCreationDate=True):
try:
file = open(filename, 'rb')
except:
return None
data = file.read()
if noCreationDate:
lst = data.split(b'\n')
lst = [e for e in lst if e.find(b'CreationDate') == -1]
data = b'\n'.join(lst)
crc32 = zlib.crc32(data)
file.close()
return "%08x" % (crc32)
def pad_align(s, align=20, extra=3):
l = len(s)
p = align - l % align + extra
pad = ' ' * p
return s + pad
def format_table(table, sep=' | ', file=sys.stdout):
nb_cols = len(table[0])
col_sizes = [0] * nb_cols
for line in table:
for i in range(0, nb_cols):
cs = col_sizes[i]
ds = len(line[i])
if ds > cs:
col_sizes[i] = ds
for line in table:
for i in range(0, nb_cols - 1):
ds = len(line[i])
print(line[i], end='', file=file)
print(' ' * (col_sizes[i] - ds), end='', file=file)
print(sep, end='', file=file)
print(line[nb_cols - 1], file=file)
_ntuple_diskusage = namedtuple('usage', 'total used free')
def disk_usage(path):
"""Return disk usage statistics about the given path.
Returned valus is a named tuple with attributes 'total', 'used' and
'free', which are the amount of total, used and free space, in bytes.
"""
st = os.statvfs(path)
free = st.f_bavail * st.f_frsize
total = st.f_blocks * st.f_frsize
used = (st.f_blocks - st.f_bfree) * st.f_frsize
return _ntuple_diskusage(total, used, free)
def test():
print("Nb CPUs : ", get_nb_cpus())
print("Arch : ", get_arch())
print("Hostname : ", get_hostname())
print("SVN revision : ", get_svn_revision())
print("CRC32('util.py'):", compute_file_crc32('util.py'))
print("CRC32('whateve'):", compute_file_crc32('whateve'))
print(disk_usage('/'))
def get_log_time():
now = datetime.datetime.now()
now_str = now.strftime('%Y-%m-%d_%H:%M:%S')
return now_str
if __name__ == "__main__":
test()