forked from canonical/cloud-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_utils.py
64 lines (49 loc) · 1.73 KB
/
setup_utils.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
import os
import subprocess
import sys
from typing import List
def is_f(p: str) -> bool:
return os.path.isfile(p)
def is_generator(p: str) -> bool:
return "-generator" in p
def pkg_config_read(library: str, var: str) -> str:
fallbacks = {
"systemd": {
"systemdsystemconfdir": "/etc/systemd/system",
"systemdsystemunitdir": "/lib/systemd/system",
"systemdsystemgeneratordir": "/lib/systemd/system-generators",
},
"udev": {
"udevdir": "/lib/udev",
},
}
cmd = ["pkg-config", f"--variable={var}", library]
try:
path = subprocess.check_output(cmd).decode("utf-8") # nosec B603
path = path.strip()
except Exception:
path = fallbacks[library][var]
if path.startswith("/"):
path = path[1:]
return path
def in_virtualenv() -> bool:
# TODO: sys.real_prefix doesn't exist on any currently supported
# version of python. This function can never return True
try:
return sys.real_prefix != sys.prefix
except AttributeError:
return False
def version_to_pep440(version: str) -> str:
# read-version can spit out something like 22.4-15-g7f97aee24
# which is invalid under PEP 440. If we replace the first - with a +
# that should give us a valid version.
return version.replace("-", "+", 1)
def get_version() -> str:
cmd = [sys.executable, "tools/read-version"]
ver = subprocess.check_output(cmd) # B603
version = ver.decode("utf-8").strip()
return version_to_pep440(version)
def read_requires() -> List[str]:
cmd = [sys.executable, "tools/read-dependencies"]
deps = subprocess.check_output(cmd) # nosec B603
return deps.decode("utf-8").splitlines()