1# Copyright 2024 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"""A function to convert a dist name to a valid bazel repo name. 16""" 17 18load("//python/private:normalize_name.bzl", "normalize_name") 19load(":parse_whl_name.bzl", "parse_whl_name") 20 21def whl_repo_name(prefix, filename, sha256): 22 """Return a valid whl_library repo name given a distribution filename. 23 24 Args: 25 prefix: str, the prefix of the whl_library. 26 filename: str, the filename of the distribution. 27 sha256: str, the sha256 of the distribution. 28 29 Returns: 30 a string that can be used in `whl_library`. 31 """ 32 parts = [prefix] 33 34 if not filename.endswith(".whl"): 35 # Then the filename is basically foo-3.2.1.<ext> 36 parts.append(normalize_name(filename.rpartition("-")[0])) 37 parts.append("sdist") 38 else: 39 parsed = parse_whl_name(filename) 40 name = normalize_name(parsed.distribution) 41 python_tag, _, _ = parsed.python_tag.partition(".") 42 abi_tag, _, _ = parsed.abi_tag.partition(".") 43 platform_tag, _, _ = parsed.platform_tag.partition(".") 44 45 parts.append(name) 46 parts.append(python_tag) 47 parts.append(abi_tag) 48 parts.append(platform_tag) 49 50 parts.append(sha256[:8]) 51 52 return "_".join(parts) 53