Debian Codegraph @

scripts/common/__init__.py

raw symbols SCIP
1 # Copyright ⓒ 2016 Daniel Keep.
2 #
3 # Licensed under the MIT license (see LICENSE or <http://opensource.org
4 # /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
5 # <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
6 # files in the project carrying such notice may not be copied, modified,
7 # or distributed except according to those terms.
8
9 __all__ = [
10 "RUSTUP",
11 "USE_ANSI",
12 "load_globals_from_metadata",
13 "load_metadata_from_manifest",
14 "msg",
15 "msg_trace",
16 "set_toolbox_trace",
17 "sh",
18 "sh_eval",
19 "which",
20 ]
21
22 import os
23 import os.path
24 import subprocess
25 import sys
26 import toml
27
28 from itertools import chain
29
30 TRACE = os.environ.get('TRACE_TOOLBOX', '') != ''
31 USE_ANSI = True if sys.platform != 'win32' else os.environ.get('FORCE_ANSI', '') != '' or os.environ.get('ConEmuANSI', 'OFF') == 'ON'
32
33 def which(programname, all=False):
34 path_exts = os.environ.get('PATHEXT', '').split(os.path.pathsep)
35 path_exts = [e for e in path_exts if e.lstrip() != '']
36
37 def matches():
38 for path in os.environ['PATH'].split(os.path.pathsep):
39 base_path = os.path.join(path, programname)
40 for ext in chain(('',), path_exts):
41 ext_path = base_path+ext
42 if os.path.exists(os.path.normcase(ext_path)):
43 yield ext_path
44
45 if all:
46 return matches()
47 else:
48 return next(matches(), None)
49
50 RUSTUP = "rustup" if which("rustup") is not None else "multirust"
51
52 def load_metadata_from_manifest(section):
53 with open('Cargo.toml', 'rt') as manifest_file:
54 manifest = toml.loads(manifest_file.read())
55 return (manifest
56 .get('package', {})
57 .get('metadata', {})
58 .get(section, {})
59 )
60
61 def load_globals_from_metadata(section, globals, names):
62 metadata = load_metadata_from_manifest(section)
63 for (k, v) in metadata.items():
64 k_ss = k.upper().replace('-', '_')
65 if k_ss not in names:
66 continue
67 globals[k_ss] = v
68
69 def msg(*args):
70 if USE_ANSI: sys.stdout.write('\x1b[1;34m')
71 sys.stdout.write('> ')
72 if USE_ANSI: sys.stdout.write('\x1b[1;32m')
73 for arg in args:
74 sys.stdout.write(str(arg))
75 if USE_ANSI: sys.stdout.write('\x1b[0m')
76 sys.stdout.write('\n')
77 sys.stdout.flush()
78
79 def msg_trace(*args):
80 if TRACE:
81 if USE_ANSI: sys.stderr.write('\x1b[1;31m')
82 sys.stderr.write('$ ')
83 if USE_ANSI: sys.stderr.write('\x1b[0m')
84 for arg in args:
85 sys.stderr.write(str(arg))
86 sys.stderr.write('\n')
87 sys.stderr.flush()
88
89 def set_toolbox_trace(env_var):
90 global TRACE
91 TRACE = TRACE or os.environ.get(env_var, '') != ''
92
93 def sh(cmd, env=None, stdout=None, stderr=None, checked=True):
94 msg_trace('sh(%r, env=%r)' % (cmd, env))
95 try:
96 subprocess.check_call(cmd, env=env, stdout=stdout, stderr=stderr, shell=True)
97 except Exception as e:
98 msg_trace('FAILED: %s' % e)
99 if checked:
100 raise
101 else:
102 return False
103 if not checked:
104 return True
105
106 def sh_eval(cmd, codec='utf-8', dont_strip=False):
107 msg_trace('sh_eval(%r)' % cmd)
108 result = None
109 try:
110 result = subprocess.check_output(cmd, shell=True).decode(codec)
111 if not dont_strip:
112 result = result.strip()
113 except:
114 msg_trace('FAILED!')
115 raise
116 return result