|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +"""Check that no Cargo.toml files contain [patch.crates-io] entries. |
| 20 | +
|
| 21 | +Release builds must not depend on patched crates. During development it is |
| 22 | +common to temporarily patch crates-io dependencies, but those patches must |
| 23 | +be removed before creating a release. |
| 24 | +
|
| 25 | +An empty [patch.crates-io] section is allowed. |
| 26 | +""" |
| 27 | + |
| 28 | +import sys |
| 29 | +from pathlib import Path |
| 30 | + |
| 31 | +import tomllib |
| 32 | + |
| 33 | + |
| 34 | +def main() -> int: |
| 35 | + errors: list[str] = [] |
| 36 | + for cargo_toml in sorted(Path().rglob("Cargo.toml")): |
| 37 | + if "target" in cargo_toml.parts: |
| 38 | + continue |
| 39 | + with Path.open(cargo_toml, "rb") as f: |
| 40 | + data = tomllib.load(f) |
| 41 | + patch = data.get("patch", {}).get("crates-io", {}) |
| 42 | + if patch: |
| 43 | + errors.append(str(cargo_toml)) |
| 44 | + for name, spec in patch.items(): |
| 45 | + errors.append(f" {name} = {spec}") |
| 46 | + |
| 47 | + if errors: |
| 48 | + print("ERROR: Release builds must not contain [patch.crates-io] entries.") |
| 49 | + print() |
| 50 | + for line in errors: |
| 51 | + print(line) |
| 52 | + print() |
| 53 | + print("Remove all [patch.crates-io] entries before creating a release.") |
| 54 | + return 1 |
| 55 | + |
| 56 | + print("OK: No [patch.crates-io] entries found.") |
| 57 | + return 0 |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + sys.exit(main()) |
0 commit comments