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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# Copyright (c) 2020 Egor Tensin <egor@tensin.name>
# This file is part of the "cmake-common" project.
# For details, see https://github.com/egor-tensin/cmake-common
# Distributed under the MIT License.
from contextlib import contextmanager
import functools
import logging
import os.path
import shlex
import shutil
import subprocess
import sys
import tempfile
import time
import project.os
def read_file(path):
with open(path, encoding="utf-8") as fd:
return fd.read()
def normalize_path(s):
return os.path.abspath(os.path.normpath(s))
def mkdir_parent(path):
os.makedirs(path, exist_ok=True)
def full_exe_name(exe):
if not project.os.on_windows_like():
# There's no PATHEXT on Linux.
return exe
# b2 on Windows doesn't like it when the executable name doesn't include
# the extension.
dir_path = os.path.dirname(exe) or None
path = shutil.which(exe, path=dir_path)
if not path:
raise RuntimeError(f"executable '{exe}' could not be found")
if dir_path:
# If it was found in a specific directory, include the directory in the
# result. shutil.which returns the executable name prefixed with the
# path argument.
return path
# If it was found in PATH, just return the basename (which includes the
# extension).
return os.path.basename(path)
@contextmanager
def setup_logging(verbose=False):
level_names = {
logging.DEBUG: "DBG",
logging.INFO: "INFO",
logging.WARNING: "WARN",
logging.ERROR: "ERR",
logging.CRITICAL: "CRIT",
}
for lvl, name in level_names.items():
logging.addLevelName(lvl, name)
logging.basicConfig(
level=logging.DEBUG if verbose else logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S%z",
format="%(asctime)s | %(levelname)4s | %(message)s",
# Log to stdout, because that's where subprocess's output goes (so that
# they don't get interleaved).
stream=sys.stdout,
)
try:
yield
except Exception as e:
logging.exception(e)
sys.exit(1)
@contextmanager
def cd(path):
cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(cwd)
def run(cmd, **kwargs):
logging.info("Running: %s", shlex.join(cmd))
return subprocess.run(cmd, check=True, **kwargs)
def _run_log_output(level, output):
if not output:
logging.log(level, "... No output")
logging.log(level, "Output (%d) characters):", len(output))
for line in output.splitlines():
logging.log(level, " %s", line)
def run_capture(cmd, **kwargs):
stdout = subprocess.PIPE
stderr = subprocess.STDOUT
try:
result = run(cmd, **kwargs, stdout=stdout, stderr=stderr, encoding="utf-8")
except subprocess.CalledProcessError as e:
logging.error("... Exited with code %d", e.returncode)
_run_log_output(logging.ERROR, e.output)
raise
_run_log_output(logging.DEBUG, result.stdout)
return result.stdout
@contextmanager
def delete_on_error(path):
try:
yield
except:
logging.info("Removing temporary file: %s", path)
os.remove(path)
raise
@contextmanager
def delete(path):
try:
yield
finally:
logging.info("Removing temporary file: %s", path)
os.remove(path)
@contextmanager
def temp_file(**kwargs):
"""Make NamedTemporaryFile usable on Windows.
It can't be opened a second time on Windows, hence this silliness.
"""
tmp = tempfile.NamedTemporaryFile(delete=False, **kwargs)
with tmp as file, delete_on_error(file.name):
path = file.name
logging.info("Created temporary file: %s", path)
with delete(path):
yield path
def retry(exc_type, timeout=5, tries=3, backoff=2):
def wrapper(func):
@functools.wraps(func)
def func2(*args, **kwargs):
current_timeout = timeout
current_try = 0
while True:
try:
return func(*args, **kwargs)
except exc_type as e:
logging.exception(e)
current_try += 1
if current_try < tries:
logging.error("Retrying after %d seconds", current_timeout)
time.sleep(current_timeout)
current_timeout *= backoff
continue
raise
return func2
return wrapper
|