15
15
"""This module is for implementing PEP508 compliant METADATA deps parsing.
16
16
"""
17
17
18
- load ("@pythons_hub//:versions.bzl" , "DEFAULT_PYTHON_VERSION" , "MINOR_MAPPING" )
19
- load ("//python/private:full_version.bzl" , "full_version" )
20
18
load ("//python/private:normalize_name.bzl" , "normalize_name" )
21
- load (":pep508_env.bzl" , "env" )
22
19
load (":pep508_evaluate.bzl" , "evaluate" )
23
- load (":pep508_platform.bzl" , "platform" , "platform_from_str" )
24
20
load (":pep508_requirement.bzl" , "requirement" )
25
21
26
22
def deps (
27
23
name ,
28
24
* ,
29
25
requires_dist ,
30
- platforms = [],
31
26
extras = [],
32
27
excludes = [],
33
- default_python_version = None ,
34
- minor_mapping = MINOR_MAPPING ):
28
+ include = []):
35
29
"""Parse the RequiresDist from wheel METADATA
36
30
37
31
Args:
38
32
name: {type}`str` the name of the wheel.
39
33
requires_dist: {type}`list[str]` the list of RequiresDist lines from the
40
34
METADATA file.
41
35
excludes: {type}`list[str]` what packages should we exclude.
36
+ include: {type}`list[str]` what packages should we exclude. If it is not
37
+ specified, then we will include all deps from `requires_dist`.
42
38
extras: {type}`list[str]` the requested extras to generate targets for.
43
- platforms: {type}`list[str]` the list of target platform strings.
44
- default_python_version: {type}`str` the host python version.
45
- minor_mapping: {type}`type[str, str]` the minor mapping to use when
46
- resolving to the full python version as DEFAULT_PYTHON_VERSION can by
47
- of format `3.x`.
48
39
49
40
Returns:
50
41
A struct with attributes:
@@ -60,39 +51,20 @@ def deps(
60
51
deps_select = {}
61
52
name = normalize_name (name )
62
53
want_extras = _resolve_extras (name , reqs , extras )
54
+ include = [normalize_name (n ) for n in include ]
63
55
64
56
# drop self edges
65
57
excludes = [name ] + [normalize_name (x ) for x in excludes ]
66
58
67
- default_python_version = default_python_version or DEFAULT_PYTHON_VERSION
68
- if default_python_version :
69
- # if it is not bzlmod, then DEFAULT_PYTHON_VERSION may be unset
70
- default_python_version = full_version (
71
- version = default_python_version ,
72
- minor_mapping = minor_mapping ,
73
- )
74
- platforms = [
75
- platform_from_str (p , python_version = default_python_version )
76
- for p in platforms
77
- ]
78
-
79
- abis = sorted ({p .abi : True for p in platforms if p .abi })
80
- if default_python_version and len (abis ) > 1 :
81
- _ , _ , tail = default_python_version .partition ("." )
82
- default_abi = "cp3" + tail
83
- elif len (abis ) > 1 :
84
- fail (
85
- "all python versions need to be specified explicitly, got: {}" .format (platforms ),
86
- )
87
- else :
88
- default_abi = None
89
-
90
59
reqs_by_name = {}
91
60
92
61
for req in reqs :
93
62
if req .name_ in excludes :
94
63
continue
95
64
65
+ if include and req .name_ not in include :
66
+ continue
67
+
96
68
reqs_by_name .setdefault (req .name , []).append (req )
97
69
98
70
for name , reqs in reqs_by_name .items ():
@@ -102,55 +74,25 @@ def deps(
102
74
normalize_name (name ),
103
75
reqs ,
104
76
extras = want_extras ,
105
- platforms = platforms ,
106
- default_abi = default_abi ,
107
77
)
108
78
109
79
return struct (
110
80
deps = sorted (deps ),
111
81
deps_select = {
112
- _platform_str ( p ): sorted ( deps )
113
- for p , deps in deps_select .items ()
82
+ d : markers
83
+ for d , markers in sorted ( deps_select .items () )
114
84
},
115
85
)
116
86
117
- def _platform_str (self ):
118
- if self .abi == None :
119
- return "{}_{}" .format (self .os , self .arch )
120
-
121
- return "{}_{}_{}" .format (
122
- self .abi ,
123
- self .os or "anyos" ,
124
- self .arch or "anyarch" ,
125
- )
126
-
127
- def _add (deps , deps_select , dep , platform ):
87
+ def _add (deps , deps_select , dep , markers = None ):
128
88
dep = normalize_name (dep )
129
89
130
- if platform == None :
90
+ if not markers :
131
91
deps [dep ] = True
132
-
133
- # If the dep is in the platform-specific list, remove it from the select.
134
- pop_keys = []
135
- for p , _deps in deps_select .items ():
136
- if dep not in _deps :
137
- continue
138
-
139
- _deps .pop (dep )
140
- if not _deps :
141
- pop_keys .append (p )
142
-
143
- for p in pop_keys :
144
- deps_select .pop (p )
145
- return
146
-
147
- if dep in deps :
148
- # If the dep is already in the main dependency list, no need to add it in the
149
- # platform-specific dependency list.
150
- return
151
-
152
- # Add the platform-specific branch
153
- deps_select .setdefault (platform , {})[dep ] = True
92
+ elif len (markers ) == 1 :
93
+ deps_select [dep ] = markers [0 ]
94
+ else :
95
+ deps_select [dep ] = "({})" .format (") or (" .join (sorted (markers )))
154
96
155
97
def _resolve_extras (self_name , reqs , extras ):
156
98
"""Resolve extras which are due to depending on self[some_other_extra].
@@ -207,37 +149,24 @@ def _resolve_extras(self_name, reqs, extras):
207
149
# Poor mans set
208
150
return sorted ({x : None for x in extras })
209
151
210
- def _add_reqs (deps , deps_select , dep , reqs , * , extras , platforms , default_abi = None ):
152
+ def _add_reqs (deps , deps_select , dep , reqs , * , extras ):
211
153
for req in reqs :
212
154
if not req .marker :
213
- _add (deps , deps_select , dep , None )
155
+ _add (deps , deps_select , dep )
214
156
return
215
157
216
- platforms_to_add = {}
217
- for plat in platforms :
218
- if plat in platforms_to_add :
219
- # marker evaluation is more expensive than this check
220
- continue
221
-
222
- added = False
223
- for extra in extras :
224
- if added :
158
+ markers = {}
159
+ for req in reqs :
160
+ for x in extras :
161
+ m = evaluate (req .marker , env = {"extra" : x }, strict = False )
162
+ if m == False :
163
+ continue
164
+ elif m == True :
165
+ _add (deps , deps_select , dep )
225
166
break
167
+ else :
168
+ markers [m ] = None
169
+ continue
226
170
227
- for req in reqs :
228
- if evaluate (req .marker , env = env (target_platform = plat , extra = extra )):
229
- platforms_to_add [plat ] = True
230
- added = True
231
- break
232
-
233
- if len (platforms_to_add ) == len (platforms ):
234
- # the dep is in all target platforms, let's just add it to the regular
235
- # list
236
- _add (deps , deps_select , dep , None )
237
- return
238
-
239
- for plat in platforms_to_add :
240
- if default_abi :
241
- _add (deps , deps_select , dep , plat )
242
- if plat .abi == default_abi or not default_abi :
243
- _add (deps , deps_select , dep , platform (os = plat .os , arch = plat .arch ))
171
+ if markers :
172
+ _add (deps , deps_select , dep , sorted (markers ))
0 commit comments