forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefs.bzl
More file actions
142 lines (118 loc) · 4.71 KB
/
Copy pathdefs.bzl
File metadata and controls
142 lines (118 loc) · 4.71 KB
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
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Core rules for building Python projects.
"""
load("@bazel_tools//tools/python:srcs_version.bzl", _find_requirements = "find_requirements")
load(
"//python/private:reexports.bzl",
"internal_PyInfo",
"internal_PyRuntimeInfo",
_py_binary = "py_binary",
_py_library = "py_library",
_py_runtime = "py_runtime",
_py_runtime_pair = "py_runtime_pair",
_py_test = "py_test",
)
# Exports of native-defined providers.
PyInfo = internal_PyInfo
PyRuntimeInfo = internal_PyRuntimeInfo
def _current_py_toolchain_impl(ctx):
toolchain = ctx.toolchains[ctx.attr._toolchain]
direct = []
transitive = []
vars = {}
if toolchain.py3_runtime and toolchain.py3_runtime.interpreter:
direct.append(toolchain.py3_runtime.interpreter)
transitive.append(toolchain.py3_runtime.files)
vars["PYTHON3"] = toolchain.py3_runtime.interpreter.path
if toolchain.py2_runtime and toolchain.py2_runtime.interpreter:
direct.append(toolchain.py2_runtime.interpreter)
transitive.append(toolchain.py2_runtime.files)
vars["PYTHON2"] = toolchain.py2_runtime.interpreter.path
files = depset(direct, transitive = transitive)
return [
toolchain,
platform_common.TemplateVariableInfo(vars),
DefaultInfo(
runfiles = ctx.runfiles(transitive_files = files),
files = files,
),
]
current_py_toolchain = rule(
doc = """
This rule exists so that the current python toolchain can be used in the `toolchains` attribute of
other rules, such as genrule. It allows exposing a python toolchain after toolchain resolution has
happened, to a rule which expects a concrete implementation of a toolchain, rather than a
toolchain_type which could be resolved to that toolchain.
""",
implementation = _current_py_toolchain_impl,
attrs = {
"_toolchain": attr.string(default = str(Label("@bazel_tools//tools/python:toolchain_type"))),
},
toolchains = [
str(Label("@bazel_tools//tools/python:toolchain_type")),
],
)
def _py_import_impl(ctx):
# See https://github.com/bazelbuild/bazel/blob/0.24.0/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java#L104 .
import_paths = [
"/".join([ctx.workspace_name, x.short_path])
for x in ctx.files.srcs
]
return [
DefaultInfo(
default_runfiles = ctx.runfiles(ctx.files.srcs, collect_default = True),
),
PyInfo(
transitive_sources = depset(transitive = [
dep[PyInfo].transitive_sources
for dep in ctx.attr.deps
]),
imports = depset(direct = import_paths, transitive = [
dep[PyInfo].imports
for dep in ctx.attr.deps
]),
),
]
py_import = rule(
doc = """This rule allows the use of Python packages as dependencies.
It imports the given `.egg` file(s), which might be checked in source files,
fetched externally as with `http_file`, or produced as outputs of other rules.
It may be used like a `py_library`, in the `deps` of other Python rules.
This is similar to [java_import](https://docs.bazel.build/versions/master/be/java.html#java_import).
""",
implementation = _py_import_impl,
attrs = {
"deps": attr.label_list(
doc = "The list of other libraries to be linked in to the " +
"binary target.",
providers = [PyInfo],
),
"srcs": attr.label_list(
doc = "The list of Python package files provided to Python targets " +
"that depend on this target. Note that currently only the .egg " +
"format is accepted. For .whl files, try the whl_library rule. " +
"We accept contributions to extend py_import to handle .whl.",
allow_files = [".egg"],
),
},
)
# Re-exports of Starlark-defined symbols in @bazel_tools//tools/python.
py_runtime_pair = _py_runtime_pair
find_requirements = _find_requirements
py_library = _py_library
py_binary = _py_binary
py_test = _py_test
py_runtime = _py_runtime