-
Notifications
You must be signed in to change notification settings - Fork 1
/
pkgs.py
417 lines (314 loc) · 12 KB
/
pkgs.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import os
import logging
import functools
import subprocess
import contextlib
from collections import OrderedDict
from rez.config import config as rezconfig
from rez.system import system
from rez.package_maker import PackageMaker
from rez.vendor.version.version import Version
from rez.packages import iter_package_families
from rez.exceptions import PackageNotFoundError
from rez.utils.formatting import PackageRequest
from rez.resolved_context import ResolvedContext
from rez.developer_package import DeveloperPackage
from rez.utils.logging_ import logger as rez_logger
from rez.packages import get_latest_package_from_string
from rez.package_repository import package_repository_manager
from . import git
# silencing rez logger, e.g. on package preprocessing
rez_logger.setLevel(logging.WARNING)
deliverconfig = rezconfig.plugins.command.deliver
def expand_path(path):
path = functools.reduce(
lambda _p, f: f(_p),
[path,
os.path.expanduser,
os.path.expandvars,
os.path.normpath]
)
return path
"""
FileSystem Repo(s) BindPackageRepo
| |
| |
| *------------*
| |
V V
MainMemoryRepo
"""
class Repo(object):
def __init__(self, root):
self._root = root
def __contains__(self, pkg):
uid = "@".join(pkg.parent.repository.uid[:2])
return uid == self.mem_uid
@property
def mem_uid(self):
return "memory@" + self._root
@property
def mem_repo(self):
return package_repository_manager.get_repository(self.mem_uid)
@property
def root(self):
return self._root
class BindPkgRepo(Repo):
@property
def root(self):
return self.mem_uid
@property
def bindings(self):
return {
"os": pkg_os,
"arch": pkg_arch,
"platform": pkg_platform,
}
def iter_bind_packages(self):
for name, maker in self.bindings.items():
data = maker().data.copy()
data["_DEV_SRC"] = self._root
yield name, {data["version"]: data}
def load(self):
self.mem_repo.data = {
name: versions for name, versions
in self.iter_bind_packages()
}
class DevPkgRepo(Repo):
def load_dev_packages(self, family):
for package in family.iter_packages():
pkg_path = os.path.dirname(package.uri)
github_repo = package.data.get("github_repo")
if github_repo:
for ver_tag in git.get_released_tags(github_repo):
os.environ["REZ_DELIVER_PKG_PAYLOAD_VER"] = ver_tag
yield DeveloperPackage.from_path(pkg_path)
else:
yield DeveloperPackage.from_path(pkg_path)
def iter_dev_packages(self):
for family in iter_package_families(paths=[self._root]):
name = family.name # package dir name
versions = dict()
for dev_package in self.load_dev_packages(family):
data = dev_package.data.copy()
if data.get("_DEV_SRC") != "_REZ_BIND":
data["_DEV_SRC"] = dev_package.filepath
version = data.get("version", "_NO_VERSION")
versions[version] = data
yield name, versions
def load(self):
"""Load dev-packages from filesystem into memory repository"""
self.mem_repo.data = {
name: versions for name, versions
in self.iter_dev_packages()
}
class DevRepoManager(object):
def __init__(self):
self._dev_repos = [
DevPkgRepo(root=expand_path(root))
for root in deliverconfig.dev_repository_roots
]
self._bind_repo = BindPkgRepo(root="_REZ_BIND")
@property
def binds(self):
return self._bind_repo
@property
def paths(self):
mem_paths = [
repo.mem_uid
for repo in self._dev_repos
]
mem_paths.append(self._bind_repo.mem_uid)
return mem_paths
def load(self):
self._bind_repo.load()
dev_paths = [
repo.root
for repo in self._dev_repos
]
dev_paths.append(self._bind_repo.root)
with override_config({
# Append `dev_paths` into `config.packages_path` so the requires
# can be expanded properly with other pre-installed packages.
# If we don't do this, requirements like "os-*" or "python-2.*"
# may raise error like schema validation fail (which is also
# confusing) due to package not found.
"packages_path": rezconfig.packages_path[:] + dev_paths,
# Ensure unversioned package is allowed, so we can iter dev
# packages.
"allow_unversioned_packages": True,
}):
for repo in self._dev_repos:
repo.load()
def find(self, name):
return get_latest_package_from_string(name, paths=self.paths)
def iter_package_families(self):
for family in iter_package_families(paths=self.paths):
yield family
class PackageInstaller(object):
def __init__(self, dev_repo, rezsrc=None):
rezsrc = expand_path(rezsrc or deliverconfig.rez_source_path)
self.release = False
self.dev_repo = dev_repo
self.rezsrc_path = rezsrc
self._requirements = OrderedDict()
@property
def installed_packages_path(self):
c, r = rezconfig, self.release
return c.nonlocal_packages_path if r else c.packages_path
@property
def deploy_path(self):
c, r = rezconfig, self.release
return c.release_packages_path if r else c.local_packages_path
def target(self, path):
"""
Only set to 'release' when the `path` is release_packages_path.
"""
path = expand_path(path)
release = path == expand_path(rezconfig.release_packages_path)
print("Mode: %s" % ("release" if release else "install"))
self.release = release
self.reset()
def reset(self):
self._requirements.clear()
def manifest(self):
return self._requirements.copy()
def run(self):
for _ in self.run_iter():
pass
def run_iter(self):
for (q_name, v_index), (exists, src) in self._requirements.items():
if exists:
continue
name = q_name.split("-", 1)[0]
if name == "rez":
self._install_rez_as_package()
elif name in self.dev_repo.binds.bindings:
self._bind(name)
else:
self._build(os.path.dirname(src), variant=v_index)
yield q_name, v_index
def find_installed(self, name):
paths = self.installed_packages_path
return get_latest_package_from_string(name, paths=paths)
def resolve(self, request, variant_index=None):
""""""
develop = self.dev_repo.find(request)
package = self.find_installed(request)
if develop is None and package is None:
raise PackageNotFoundError("%s not found in develop repository "
"nor in installed package paths."
% request)
if develop is None:
name = package.qualified_name
variants = package.iter_variants()
source = package.uri
else:
name = develop.qualified_name
variants = develop.iter_variants()
source = develop.data["_DEV_SRC"]
if package is None:
pkg_variants_req = []
else:
pkg_variants_req = [v.variant_requires
for v in package.iter_variants()]
for variant in variants:
if variant_index is not None and variant_index != variant.index:
continue
exists = variant.variant_requires in pkg_variants_req
context = self._build_context(variant)
for pkg in context.resolved_packages:
self.resolve(request=pkg.qualified_package_name,
variant_index=pkg.index)
self._requirements[(name, variant.index)] = (exists, source)
def _install_rez_as_package(self):
"""Use Rez's install script to deploy rez as package
"""
rezsrc = self.rezsrc_path
deploy_path = self.deploy_path
rez_install = os.path.join(os.path.abspath(rezsrc), "install.py")
dev_pkg = self.dev_repo.find("rez")
print("Installing Rez as package..")
clear_repo_cache(deploy_path)
for variant in dev_pkg.iter_variants():
print("Variant: ", variant)
context = self._build_context(variant)
context.execute_shell(
command=["python", rez_install, "-v", "-p", deploy_path],
block=True,
cwd=rezsrc,
)
def _build_context(self, variant):
paths = self.installed_packages_path + self.dev_repo.paths
implicit_pkgs = list(map(PackageRequest, rezconfig.implicit_packages))
pkg_requests = variant.get_requires(build_requires=True,
private_build_requires=True)
return ResolvedContext(pkg_requests + implicit_pkgs,
building=True,
package_paths=paths)
def _bind(self, name):
pkg = self.find_installed(name)
if pkg is not None:
# installed
return
deploy_path = self.deploy_path
env = os.environ.copy()
if not os.path.isdir(deploy_path):
os.makedirs(deploy_path)
if self.release:
env["REZ_RELEASE_PACKAGES_PATH"] = deploy_path
self._run_command(["rez-bind", "--release", name], env=env)
else:
env["REZ_LOCAL_PACKAGES_PATH"] = deploy_path
self._run_command(["rez-bind", name])
clear_repo_cache(deploy_path)
def _build(self, src_dir, variant=None):
variant_cmd = [] if variant is None else ["--variants", str(variant)]
deploy_path = self.deploy_path
env = os.environ.copy()
if not os.path.isdir(deploy_path):
os.makedirs(deploy_path)
if self.release:
env["REZ_RELEASE_PACKAGES_PATH"] = deploy_path
args = ["rez-release"] + variant_cmd
self._run_command(args, cwd=src_dir, env=env)
else:
env["REZ_LOCAL_PACKAGES_PATH"] = deploy_path
args = ["rez-build", "--install"] + variant_cmd
self._run_command(args, cwd=src_dir)
clear_repo_cache(deploy_path)
def _run_command(self, cmd_args, **kwargs):
print("Running command:\n %s\n" % cmd_args)
subprocess.check_call(cmd_args, **kwargs)
@contextlib.contextmanager
def override_config(entries):
try:
for key, value in entries.items():
rezconfig.override(key, value)
yield
finally:
for key in entries.keys():
rezconfig.remove_override(key)
def clear_repo_cache(path):
"""Clear filesystem repo family cache after pkg bind/install
Current use case: Clear cache after rez-bind and before iter dev
packages into memory. Without this, variants like os-* may not be
expanded, due to filesystem repo doesn't know 'os' has been bind since
the family list is cached in this session.
"""
fs_repo = package_repository_manager.get_repository(path)
fs_repo.get_family.cache_clear()
def make_package(name, data):
maker = PackageMaker(name, data=data, package_cls=DeveloperPackage)
return maker.get_package()
def pkg_os():
data = {"version": Version(system.os),
"requires": ["platform-%s" % system.platform,
"arch-%s" % system.arch]}
return make_package("os", data=data)
def pkg_arch():
data = {"version": Version(system.arch)}
return make_package("arch", data=data)
def pkg_platform():
data = {"version": Version(system.platform)}
return make_package("platform", data=data)