1# Copyright 2023 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Definitions for the modules_mapping.json generation. 16 17The modules_mapping.json file is a mapping from Python modules to the wheel 18names that provide those modules. It is used for determining which wheel 19distribution should be used in the `deps` attribute of `py_*` targets. 20 21This mapping is necessary when reading Python import statements and determining 22if they are provided by third-party dependencies. Most importantly, when the 23module name doesn't match the wheel distribution name. 24""" 25 26def _modules_mapping_impl(ctx): 27 modules_mapping = ctx.actions.declare_file(ctx.attr.modules_mapping_name) 28 args = ctx.actions.args() 29 all_wheels = depset( 30 [whl for whl in ctx.files.wheels], 31 transitive = [dep[DefaultInfo].files for dep in ctx.attr.wheels] + [dep[DefaultInfo].data_runfiles.files for dep in ctx.attr.wheels], 32 ) 33 args.add("--output_file", modules_mapping.path) 34 args.add_all("--exclude_patterns", ctx.attr.exclude_patterns) 35 args.add_all("--wheels", [whl.path for whl in all_wheels.to_list()]) 36 ctx.actions.run( 37 inputs = all_wheels.to_list(), 38 outputs = [modules_mapping], 39 executable = ctx.executable._generator, 40 arguments = [args], 41 use_default_shell_env = False, 42 ) 43 return [DefaultInfo(files = depset([modules_mapping]))] 44 45modules_mapping = rule( 46 _modules_mapping_impl, 47 attrs = { 48 "exclude_patterns": attr.string_list( 49 default = ["^_|(\\._)+"], 50 doc = "A set of regex patterns to match against each calculated module path. By default, exclude the modules starting with underscores.", 51 mandatory = False, 52 ), 53 "modules_mapping_name": attr.string( 54 default = "modules_mapping.json", 55 doc = "The name for the output JSON file.", 56 mandatory = False, 57 ), 58 "wheels": attr.label_list( 59 allow_files = True, 60 doc = "The list of wheels, usually the 'all_whl_requirements' from @<pip_repository>//:requirements.bzl", 61 mandatory = True, 62 ), 63 "_generator": attr.label( 64 cfg = "exec", 65 default = "//modules_mapping:generator", 66 executable = True, 67 ), 68 }, 69 doc = "Creates a modules_mapping.json file for mapping module names to wheel distribution names.", 70) 71