-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths
executable file
·76 lines (57 loc) · 1.85 KB
/
paths
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
#!/usr/bin/env -S python3 -I
import argparse
import logging
import os
import sys
import subprocess
from ctypes.util import find_library
def python_site():
for p in sys.path:
if os.path.isdir(p):
logger.info(f"chosen site: {p}")
return p
else:
logger.debug(f"disregarding site: {p}")
def lib(l):
logger.debug(f"resolving library: {l}")
resolved = find_library(l)
logger.debug(f"resolving library: {l} -> {resolved}")
lib = os.path.join(os.path.dirname(os.path.realpath(__file__)), "lib")
p = subprocess.run([lib, resolved], check=True, capture_output=True, text=True)
path = p.stdout.splitlines()[0]
logger.debug(f"resolving library: {l} -> {resolved} -> {path}")
return path
def parse_args():
parser = argparse.ArgumentParser(description="Path-resolution tool")
parser.add_argument("--log", default=os.environ.get("LOG_LEVEL", "WARN"), help="set log level")
parser.add_argument("-o", "--output")
parser.add_argument("-l", "--lib", nargs="*")
parser.add_argument("-p", "--python-site", action="store_true")
return parser.parse_args()
logger = None
def setup_logger(level):
l = logging.getLogger("paths")
l.setLevel(level)
ch = logging.StreamHandler()
ch.setLevel(level)
f = logging.Formatter(
fmt="%(asctime)s:%(name)s:%(levelname)s %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S%z")
ch.setFormatter(f)
l.addHandler(ch)
return l
if __name__ == "__main__":
args = parse_args()
logger = setup_logger(args.log.upper())
logger.debug(f"args: {args}")
if args.output is not None:
f = open(args.output, "w")
def out(s):
f.write(s)
f.write("\n")
else:
out = print
if args.python_site:
out(python_site())
for l in args.lib or []:
out(lib(l))