44
55- the special 'preprocess' function;
66- early bound functions that use the @early decorator.
7+ """
78
8- An example of use:
9+ # these imports just forward the symbols into this module's namespace
10+ from rez .utils .sourcecode import late
11+ from rez .exceptions import InvalidPackageError
912
10- # in package.py
11- name = 'mypackage'
1213
13- version = '1.2.3'
14+ def expand_requirement (request ):
15+ """Expands a requirement string like 'python-2.*'
1416
15- @early()
16- def requires():
17- from rez.package_py_utils import expand_requires
17+ Only trailing wildcards are supported; they will be replaced with the
18+ latest package version found within the range. If none are found, the
19+ wildcards will just be stripped.
1820
19- return expand_requires(
20- 'boost-1.*.*',
21- 'maya-2017.*'
22- )
23- """
21+ Example:
2422
25- # Here to allow 'from rez.package_utils import late' in package.py
26- from rez . utils . sourcecode import late
23+ >>> print expand_requirement('python-2.*')
24+ python-2.7
2725
28- # Here to allow 'from rez.package_utils import InvalidPackageError' in package.py
29- from rez . exceptions import InvalidPackageError
26+ Args:
27+ request (str): Request to expand, eg 'python-2.*'
3028
29+ Returns:
30+ str: Expanded request string.
31+ """
32+ from rez .vendor .version .requirement import VersionedObject , Requirement
33+ from rez .packages_ import get_latest_package
3134
32- def expand_requires (* requests ):
33- """Create an expanded requirements list.
35+ txt = request .replace ('*' , '_' )
36+ obj = VersionedObject (txt )
37+ rank = len (obj .version )
38+
39+ request_ = request
40+ while request_ .endswith ('*' ):
41+ request_ = request_ [:- 2 ] # strip sep + *
3442
35- Given a list of requirements with optional trailing wildcards, expand each
36- out to the latest package found within that range. This is useful when a
37- package is compatible with a version range of a package at build time, but
38- requires a stricter requirement at runtime. For example, a compiled library
39- may build with many versions of boost (boost-1.*.*), but once compiled, must
40- be used with the boost version that has then been linked against (1.55.0).
43+ req = Requirement (request_ )
44+ package = get_latest_package (name = req .name , range_ = req .range_ )
4145
42- Note:
43- If a package is not found in the given range, it is expanded to the
44- request as-is, with trailing wildcards removed.
46+ if package is None :
47+ return request_
48+
49+ obj .version_ = package .version .trim (rank )
50+ return str (obj )
51+
52+
53+ def expand_requires (* requests ):
54+ """Create an expanded requirements list.
4555
4656 Example:
4757
@@ -57,27 +67,4 @@ def expand_requires(*requests):
5767 Returns:
5868 List of str: Expanded requirements.
5969 """
60- from rez .vendor .version .requirement import VersionedObject
61- from rez .packages_ import get_latest_package
62-
63- result = []
64-
65- for request in requests :
66- txt = request .replace ('*' , '_' )
67- obj = VersionedObject (txt )
68- rank = len (obj .version )
69-
70- request_ = request
71- while request_ .endswith ('*' ):
72- request_ = request_ [:- 2 ] # consume sep + *
73-
74- package = get_latest_package (request_ )
75-
76- if package is None :
77- result .append (request_ )
78- continue
79-
80- obj .version_ = package .version .trim (rank )
81- result .append (str (obj ))
82-
83- return result
70+ return [expand_requirement (x ) for x in requests ]
0 commit comments