1*60517a1eSAndroid Build Coastguard Worker# Copyright 2023 The Bazel Authors. All rights reserved. 2*60517a1eSAndroid Build Coastguard Worker# 3*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*60517a1eSAndroid Build Coastguard Worker# 7*60517a1eSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*60517a1eSAndroid Build Coastguard Worker# 9*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*60517a1eSAndroid Build Coastguard Worker# limitations under the License. 14*60517a1eSAndroid Build Coastguard Worker 15*60517a1eSAndroid Build Coastguard Worker"""Public entry point for py_import rule.""" 16*60517a1eSAndroid Build Coastguard Worker 17*60517a1eSAndroid Build Coastguard Workerload(":py_info.bzl", "PyInfo") 18*60517a1eSAndroid Build Coastguard Worker 19*60517a1eSAndroid Build Coastguard Workerdef _py_import_impl(ctx): 20*60517a1eSAndroid Build Coastguard Worker # See https://github.com/bazelbuild/bazel/blob/0.24.0/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java#L104 . 21*60517a1eSAndroid Build Coastguard Worker import_paths = [ 22*60517a1eSAndroid Build Coastguard Worker "/".join([ctx.workspace_name, x.short_path]) 23*60517a1eSAndroid Build Coastguard Worker for x in ctx.files.srcs 24*60517a1eSAndroid Build Coastguard Worker ] 25*60517a1eSAndroid Build Coastguard Worker 26*60517a1eSAndroid Build Coastguard Worker return [ 27*60517a1eSAndroid Build Coastguard Worker DefaultInfo( 28*60517a1eSAndroid Build Coastguard Worker default_runfiles = ctx.runfiles(ctx.files.srcs, collect_default = True), 29*60517a1eSAndroid Build Coastguard Worker ), 30*60517a1eSAndroid Build Coastguard Worker PyInfo( 31*60517a1eSAndroid Build Coastguard Worker transitive_sources = depset(transitive = [ 32*60517a1eSAndroid Build Coastguard Worker dep[PyInfo].transitive_sources 33*60517a1eSAndroid Build Coastguard Worker for dep in ctx.attr.deps 34*60517a1eSAndroid Build Coastguard Worker ]), 35*60517a1eSAndroid Build Coastguard Worker imports = depset(direct = import_paths, transitive = [ 36*60517a1eSAndroid Build Coastguard Worker dep[PyInfo].imports 37*60517a1eSAndroid Build Coastguard Worker for dep in ctx.attr.deps 38*60517a1eSAndroid Build Coastguard Worker ]), 39*60517a1eSAndroid Build Coastguard Worker ), 40*60517a1eSAndroid Build Coastguard Worker ] 41*60517a1eSAndroid Build Coastguard Worker 42*60517a1eSAndroid Build Coastguard Workerpy_import = rule( 43*60517a1eSAndroid Build Coastguard Worker doc = """This rule allows the use of Python packages as dependencies. 44*60517a1eSAndroid Build Coastguard Worker 45*60517a1eSAndroid Build Coastguard Worker It imports the given `.egg` file(s), which might be checked in source files, 46*60517a1eSAndroid Build Coastguard Worker fetched externally as with `http_file`, or produced as outputs of other rules. 47*60517a1eSAndroid Build Coastguard Worker 48*60517a1eSAndroid Build Coastguard Worker It may be used like a `py_library`, in the `deps` of other Python rules. 49*60517a1eSAndroid Build Coastguard Worker 50*60517a1eSAndroid Build Coastguard Worker This is similar to [java_import](https://docs.bazel.build/versions/master/be/java.html#java_import). 51*60517a1eSAndroid Build Coastguard Worker """, 52*60517a1eSAndroid Build Coastguard Worker implementation = _py_import_impl, 53*60517a1eSAndroid Build Coastguard Worker attrs = { 54*60517a1eSAndroid Build Coastguard Worker "deps": attr.label_list( 55*60517a1eSAndroid Build Coastguard Worker doc = "The list of other libraries to be linked in to the " + 56*60517a1eSAndroid Build Coastguard Worker "binary target.", 57*60517a1eSAndroid Build Coastguard Worker providers = [PyInfo], 58*60517a1eSAndroid Build Coastguard Worker ), 59*60517a1eSAndroid Build Coastguard Worker "srcs": attr.label_list( 60*60517a1eSAndroid Build Coastguard Worker doc = "The list of Python package files provided to Python targets " + 61*60517a1eSAndroid Build Coastguard Worker "that depend on this target. Note that currently only the .egg " + 62*60517a1eSAndroid Build Coastguard Worker "format is accepted. For .whl files, try the whl_library rule. " + 63*60517a1eSAndroid Build Coastguard Worker "We accept contributions to extend py_import to handle .whl.", 64*60517a1eSAndroid Build Coastguard Worker allow_files = [".egg"], 65*60517a1eSAndroid Build Coastguard Worker ), 66*60517a1eSAndroid Build Coastguard Worker }, 67*60517a1eSAndroid Build Coastguard Worker) 68