forked from saltstack/relenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
323 lines (260 loc) · 8.16 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# Copyright 2022 VMware, Inc.
# SPDX-License-Identifier: Apache-2
"""
Common classes and values used around relenv.
"""
import os
import pathlib
import platform
import subprocess
import sys
import tarfile
import time
import urllib.error
import urllib.request
# relenv package version
__version__ = "0.4.3"
MODULE_DIR = pathlib.Path(__file__).resolve().parent
LINUX = "linux"
WIN32 = "win32"
DARWIN = "darwin"
arches = {
LINUX: (
"x86_64",
"aarch64",
),
DARWIN: ("x86_64",),
WIN32: (
"amd64",
"x86",
# "arm64", # Python 11 should support arm.
),
}
if sys.platform == "win32":
DEFAULT_DATA_DIR = pathlib.Path.home() / "AppData" / "Local" / "relenv"
else:
DEFAULT_DATA_DIR = pathlib.Path.home() / ".local" / "relenv"
DATA_DIR = pathlib.Path(os.environ.get("RELENV_DATA", DEFAULT_DATA_DIR)).resolve()
class RelenvException(Exception):
"""
Base class for exeptions generated from relenv.
"""
def build_arch():
"""
Return the current machine.
"""
machine = platform.machine()
return machine.lower()
def work_root(root=None):
"""
Get the root directory that all other relenv working directories should be based on.
:param root: An explicitly requested root directory
:type root: str
:return: An absolute path to the relenv root working directory
:rtype: ``pathlib.Path``
"""
if root is not None:
base = pathlib.Path(root).resolve()
else:
base = MODULE_DIR
return base
def work_dir(name, root=None):
"""
Get the absolute path to the relenv working directory of the given name.
:param name: The name of the directory
:type name: str
:param root: The root directory that this working directory will be relative to
:type root: str
:return: An absolute path to the requested relenv working directory
:rtype: ``pathlib.Path``
"""
root = work_root(root)
if root == MODULE_DIR:
base = root / "_{}".format(name)
else:
base = root / name
return base
class WorkDirs:
"""
Simple class used to hold references to working directories relenv uses relative to a given root.
:param root: The root of the working directories tree
:type root: str
"""
def __init__(self, root):
self.root = root
self.toolchain_config = work_dir("toolchain", self.root)
self.toolchain = work_dir("toolchain", DATA_DIR)
self.build = work_dir("build", DATA_DIR)
self.src = work_dir("src", DATA_DIR)
self.logs = work_dir("logs", DATA_DIR)
self.download = work_dir("download", DATA_DIR)
def __getstate__(self):
"""
Return an object used for pickling.
:return: The picklable state
"""
return {
"root": self.root,
"toolchain_config": self.toolchain_config,
"toolchain": self.toolchain,
"build": self.build,
"src": self.src,
"logs": self.logs,
"download": self.download,
}
def __setstate__(self, state):
"""
Unwrap the object returned from unpickling.
:param state: The state to unpickle
:type state: dict
"""
self.root = state["root"]
self.toolchain_config = state["toolchain_config"]
self.toolchain = state["toolchain"]
self.build = state["build"]
self.src = state["src"]
self.logs = state["logs"]
self.download = state["download"]
def work_dirs(root=None):
"""
Returns a WorkDirs instance based on the given root.
:param root: The desired root of relenv's working directories
:type root: str
:return: A WorkDirs instance based on the given root
:rtype: ``relenv.common.WorkDirs``
"""
return WorkDirs(work_root(root))
def get_toolchain(arch=None, root=None):
"""
Get a the toolchain directory, specific to the arch if supplied.
:param arch: The architecture to get the toolchain for
:type arch: str
:param root: The root of the relenv working directories to search in
:type root: str
:return: The directory holding the toolchain
:rtype: ``pathlib.Path``
"""
dirs = work_dirs(root)
if arch:
return dirs.toolchain / "{}-linux-gnu".format(arch)
return dirs.toolchain
def get_triplet(machine=None, plat=None):
"""
Get the target triplet for the specified machine and platform.
If any of the args are None, it will try to deduce what they should be.
:param machine: The machine for the triplet
:type machine: str
:param plat: The platform for the triplet
:type plat: str
:raises RelenvException: If the platform is unknown
:return: The target triplet
:rtype: str
"""
if not plat:
plat = sys.platform
if not machine:
machine = build_arch()
if plat == "darwin":
return f"{machine}-macos"
elif plat == "win32":
return f"{machine}-win"
elif plat == "linux":
return f"{machine}-linux-gnu"
else:
raise RelenvException(f"Unknown platform {plat}")
def archived_build(triplet=None):
"""
Finds a the location of an archived build.
:param triplet: The build triplet to find
:type triplet: str
:return: The location of the archived build
:rtype: ``pathlib.Path``
"""
if not triplet:
triplet = get_triplet()
dirs = work_dirs(DATA_DIR)
return (dirs.build / triplet).with_suffix(".tar.xz")
def extract_archive(to_dir, archive):
"""
Extract an archive to a specific location.
:param to_dir: The directory to extract to
:type to_dir: str
:param archive: The archive to extract
:type archive: str
"""
if archive.endswith("tgz"):
read_type = "r:gz"
elif archive.endswith("xz"):
read_type = "r:xz"
elif archive.endswith("bz2"):
read_type = "r:bz2"
else:
read_type = "r"
with tarfile.open(archive, read_type) as t:
t.extractall(to_dir)
def get_download_location(url, dest):
"""
Get the full path to where the url will be downloaded to.
:param url: The url to donwload
:type url: str
:param dest: Where to download the url to
:type dest: str
:return: The path to where the url will be downloaded to
:rtype: str
"""
return os.path.join(dest, os.path.basename(url))
def download_url(url, dest, verbose=True):
"""
Download the url to the provided destination.
This method assumes the last part of the url is a filename. (https://foo.com/bar/myfile.tar.xz)
:param url: The url to download
:type url: str
:param dest: Where to download the url to
:type dest: str
:param verbose: Print download url and destination to stdout
:type verbose: bool
:raises urllib.error.HTTPError: If the url was unable to be downloaded
:return: The path to the downloaded content
:rtype: str
"""
local = get_download_location(url, dest)
if verbose:
print(f"Downloading {url} -> {local}")
n = 0
while n < 3:
n += 1
try:
fin = urllib.request.urlopen(url)
except (urllib.error.HTTPError, urllib.error.URLError) as exc:
if n == 3:
print(f"Unable to download: {url} {exc}", file=sys.stderr, flush=True)
raise
time.sleep(n * 10)
fout = open(local, "wb")
block = fin.read(10240)
try:
while block:
fout.write(block)
block = fin.read(10240)
fin.close()
fout.close()
except Exception:
try:
os.unlink(local)
except OSError:
pass
raise
return local
def runcmd(*args, **kwargs):
"""
Run a command.
Run the provided command, raising an Exception when the command finishes
with a non zero exit code. Arguments are passed through to ``subprocess.run``
:return: The process result
:rtype: ``subprocess.CompletedProcess``
:raises RelenvException: If the command finishes with a non zero exit code
"""
proc = subprocess.run(*args, **kwargs)
if proc.returncode != 0:
raise RelenvException("Build cmd '{}' failed".format(" ".join(args[0])))
return proc