forked from opensciencegrid/tarball-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
75 lines (54 loc) · 1.72 KB
/
common.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
from __future__ import print_function
import errno
import os
import subprocess
import sys
class Error(Exception): pass
def statusmsg(*args):
if sys.stdout.isatty():
print("\x1b[35;1m>>> ", " ".join(args), "\x1b[0m")
else:
print(">>> ".join(args))
def errormsg(*args):
if sys.stdout.isatty():
print("\x1b[31;1m*** ", " ".join(args), "\x1b[0m")
else:
print("*** ".join(args))
def safe_makedirs(path, mode=511):
try:
os.makedirs(path, mode)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def safe_symlink(src, dst):
try:
os.symlink(src, dst)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def to_str(strlike, encoding="latin-1", errors="backslashescape"):
if not isinstance(strlike, str):
if str is bytes:
return strlike.encode(encoding, errors)
else:
return strlike.decode(encoding, errors)
else:
return strlike
def to_bytes(strlike, encoding="latin-1", errors="backslashescape"):
if not isinstance(strlike, bytes):
return strlike.encode(encoding, errors)
else:
return strlike
class MountProcFS(object):
def __init__(self, root_dir):
self.root_dir = root_dir
self.proc_dir = os.path.join(root_dir, 'proc')
def __enter__(self):
safe_makedirs(self.proc_dir)
subprocess.check_call(['mount', '-t', 'proc', 'proc', self.proc_dir])
def __exit__(self, exc_type, exc_value, traceback):
subprocess.call(['umount', self.proc_dir])
VALID_DVERS = ["el6", "el7", "el8", "el9"]
VALID_BASEARCHES = ["x86_64"]
DEFAULT_BASEARCH = "x86_64"
assert DEFAULT_BASEARCH in VALID_BASEARCHES