@@ -27,6 +27,7 @@ behavior.
2727""" 
2828
2929load ("//python/private:normalize_name.bzl" , "normalize_name" )
30+ load ("//python/private:repo_utils.bzl" , "repo_utils" )
3031load (":index_sources.bzl" , "index_sources" )
3132load (":parse_requirements_txt.bzl" , "parse_requirements_txt" )
3233load (":whl_target_platforms.bzl" , "select_whls" )
@@ -208,6 +209,56 @@ def parse_requirements(
208209
209210    return  ret 
210211
212+ def  select_requirement (requirements , * , platform ):
213+     """A simple function to get a requirement for a particular platform. 
214+ 
215+     Only used in WORKSPACE. 
216+ 
217+     Args: 
218+         requirements (list[struct]): The list of requirements as returned by 
219+             the `parse_requirements` function above. 
220+         platform (str or None): The host platform. Usually an output of the 
221+             `host_platform` function. If None, then this function will return 
222+             the first requirement it finds. 
223+ 
224+     Returns: 
225+         None if not found or a struct returned as one of the values in the 
226+         parse_requirements function. The requirement that should be downloaded 
227+         by the host platform will be returned. 
228+     """ 
229+     maybe_requirement  =  [
230+         req 
231+         for  req  in  requirements 
232+         if  not  platform  or  [p  for  p  in  req .target_platforms  if  p .endswith (platform )]
233+     ]
234+     if  not  maybe_requirement :
235+         # Sometimes the package is not present for host platform if there 
236+         # are whls specified only in particular requirements files, in that 
237+         # case just continue, however, if the download_only flag is set up, 
238+         # then the user can also specify the target platform of the wheel 
239+         # packages they want to download, in that case there will be always 
240+         # a requirement here, so we will not be in this code branch. 
241+         return  None 
242+ 
243+     return  maybe_requirement [0 ]
244+ 
245+ def  host_platform (ctx ):
246+     """Return a string representation of the repository OS. 
247+ 
248+     Only used in WORKSPACE. 
249+ 
250+     Args: 
251+         ctx (struct): The `module_ctx` or `repository_ctx` attribute. 
252+ 
253+     Returns: 
254+         The string representation of the platform that we can later used in the `pip` 
255+         machinery. 
256+     """ 
257+     return  "{}_{}" .format (
258+         repo_utils .get_platforms_os_name (ctx ),
259+         repo_utils .get_platforms_cpu_name (ctx ),
260+     )
261+ 
211262def  _add_dists (* , requirement , index_urls , logger  =  None ):
212263    """Populate dists based on the information from the PyPI index. 
213264
0 commit comments