-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
32 lines (26 loc) · 822 Bytes
/
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
class util:
@staticmethod
def cmd(*args, **kwargs):
import subprocess
kwargs |= {'stdout': subprocess.PIPE}
p = subprocess.run(args, **kwargs)
assert p.returncode == 0, f'{repr(p)}'
return p.stdout.decode('utf8')
@staticmethod
def sort_mtime(files, cwd='./'):
return [p[0] for p in sorted([(f, os.stat(os.path.join(cwd, f)).st_mtime) for f in files], key=lambda p: p[1])]
@staticmethod
def basename(filename):
return filename.rsplit("/", 1)[1]
@classmethod
def read_file(R, filepath):
if not os.path.isfile(filepath):
LOG(f"ERROR: reading file that does not exist: '{filepath}'")
return ""
acc = list()
with open(filepath) as f:
c = f.read()
if not c.endswith("\n"):
acc.append("\n")
acc.append(c)
return "".join(acc)