-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
61 lines (41 loc) · 1.19 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
#!/usr/bin/env python3
import fcntl
import os
import sys
from typing import List
def instance_already_running(label="default"):
lock_file_pointer = os.open(f"/tmp/instance_{label}.lock", os.O_WRONLY | os.O_CREAT)
try:
fcntl.lockf(lock_file_pointer, fcntl.LOCK_EX | fcntl.LOCK_NB)
already_running = False
except IOError:
already_running = True
return already_running
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def ethrow(*args, **kwargs):
eprint(*args, **kwargs)
raise Exception(*args)
def remove_if_exists(path, *args, **kwargs):
if os.path.exists(path):
os.remove(path, *args, **kwargs)
def flatten(xs):
result = []
if isinstance(xs, (list, tuple)):
for x in xs:
result.extend(flatten(x))
else:
result.append(xs)
return result
def quote(x: str) -> str:
return f'"{x}"'
def unquote(x: str, quote='"') -> str:
if x.startswith(quote) and x.endswith(quote):
return x[1:-1]
return x
def unquote_all(x: str, quotes: List[str]) -> str:
for quote in quotes:
x = unquote(x, quote)
return x
def nullif(value: str, null_value: str) -> str:
return None if value == null_value else value