@@ -27,105 +27,18 @@ load(
2727 _py_runtime_pair = "py_runtime_pair" ,
2828 _py_test = "py_test" ,
2929)
30+ load (":current_py_toolchain.bzl" , _current_py_toolchain = "current_py_toolchain" )
31+ load (":py_import.bzl" , _py_import = "py_import" )
3032
3133# Exports of native-defined providers.
3234
3335PyInfo = internal_PyInfo
3436
3537PyRuntimeInfo = internal_PyRuntimeInfo
3638
37- def _current_py_toolchain_impl (ctx ):
38- toolchain = ctx .toolchains [ctx .attr ._toolchain ]
39+ current_py_toolchain = _current_py_toolchain
3940
40- direct = []
41- transitive = []
42- vars = {}
43-
44- if toolchain .py3_runtime and toolchain .py3_runtime .interpreter :
45- direct .append (toolchain .py3_runtime .interpreter )
46- transitive .append (toolchain .py3_runtime .files )
47- vars ["PYTHON3" ] = toolchain .py3_runtime .interpreter .path
48-
49- if toolchain .py2_runtime and toolchain .py2_runtime .interpreter :
50- direct .append (toolchain .py2_runtime .interpreter )
51- transitive .append (toolchain .py2_runtime .files )
52- vars ["PYTHON2" ] = toolchain .py2_runtime .interpreter .path
53-
54- files = depset (direct , transitive = transitive )
55- return [
56- toolchain ,
57- platform_common .TemplateVariableInfo (vars ),
58- DefaultInfo (
59- runfiles = ctx .runfiles (transitive_files = files ),
60- files = files ,
61- ),
62- ]
63-
64- current_py_toolchain = rule (
65- doc = """
66- This rule exists so that the current python toolchain can be used in the `toolchains` attribute of
67- other rules, such as genrule. It allows exposing a python toolchain after toolchain resolution has
68- happened, to a rule which expects a concrete implementation of a toolchain, rather than a
69- toolchain_type which could be resolved to that toolchain.
70- """ ,
71- implementation = _current_py_toolchain_impl ,
72- attrs = {
73- "_toolchain" : attr .string (default = str (Label ("@bazel_tools//tools/python:toolchain_type" ))),
74- },
75- toolchains = [
76- str (Label ("@bazel_tools//tools/python:toolchain_type" )),
77- ],
78- )
79-
80- def _py_import_impl (ctx ):
81- # See https://github.com/bazelbuild/bazel/blob/0.24.0/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java#L104 .
82- import_paths = [
83- "/" .join ([ctx .workspace_name , x .short_path ])
84- for x in ctx .files .srcs
85- ]
86-
87- return [
88- DefaultInfo (
89- default_runfiles = ctx .runfiles (ctx .files .srcs , collect_default = True ),
90- ),
91- PyInfo (
92- transitive_sources = depset (transitive = [
93- dep [PyInfo ].transitive_sources
94- for dep in ctx .attr .deps
95- ]),
96- imports = depset (direct = import_paths , transitive = [
97- dep [PyInfo ].imports
98- for dep in ctx .attr .deps
99- ]),
100- ),
101- ]
102-
103- py_import = rule (
104- doc = """This rule allows the use of Python packages as dependencies.
105-
106- It imports the given `.egg` file(s), which might be checked in source files,
107- fetched externally as with `http_file`, or produced as outputs of other rules.
108-
109- It may be used like a `py_library`, in the `deps` of other Python rules.
110-
111- This is similar to [java_import](https://docs.bazel.build/versions/master/be/java.html#java_import).
112- """ ,
113- implementation = _py_import_impl ,
114- attrs = {
115- "deps" : attr .label_list (
116- doc = "The list of other libraries to be linked in to the " +
117- "binary target." ,
118- providers = [PyInfo ],
119- ),
120- "srcs" : attr .label_list (
121- doc = "The list of Python package files provided to Python targets " +
122- "that depend on this target. Note that currently only the .egg " +
123- "format is accepted. For .whl files, try the whl_library rule. " +
124- "We accept contributions to extend py_import to handle .whl." ,
125- allow_files = [".egg" ],
126- ),
127- },
128- )
41+ py_import = _py_import
12942
13043# Re-exports of Starlark-defined symbols in @bazel_tools//tools/python.
13144
0 commit comments