| #!/usr/bin/env python3 |
| # Copyright 2024 The Chromium Authors |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| '''Run `cargo vet` against `third_party/rust/chromium_crates_io`. |
| |
| Arguments are passed through to `cargo vet`. |
| ''' |
| |
| # TODO(https://crbug.com/405980483): Evaluate whether to keep supporting |
| # `tools/crates/run_cargo_vet.py` (see similar note in |
| # `tools/rust/build_vet.py`). Note that we have removed `cargo vet` presubmits |
| # (as tracked in https://crbug.com/405980483). |
| |
| import argparse |
| import os |
| import platform |
| import subprocess |
| import sys |
| |
| from run_cargo import (RunCargo, DEFAULT_SYSROOT) |
| |
| _SCRIPT_NAME = os.path.basename(__file__) |
| _THIS_DIR = os.path.realpath(os.path.dirname(__file__)) |
| _CHROMIUM_ROOT_DIR = os.path.join(_THIS_DIR, '..', '..') |
| _MANIFEST_DIR = os.path.join(_CHROMIUM_ROOT_DIR, 'third_party', 'rust', |
| 'chromium_crates_io') |
| _VET_DATA_DIR = os.path.join(_MANIFEST_DIR, 'supply-chain') |
| _CONFIG_TOML_PATH = os.path.join(_VET_DATA_DIR, 'config.toml') |
| _IMPORTS_LOCK_PATH = os.path.join(_VET_DATA_DIR, 'imports.lock') |
| |
| |
| def AreOnlyCommentsChanged(old_contents, new_contents): |
| |
| def NonCommentLines(contents): |
| lines = contents.splitlines() |
| lines = [line for line in lines if line and not line.startswith('#')] |
| return lines |
| |
| return NonCommentLines(old_contents) == NonCommentLines(new_contents) |
| |
| |
| def main(): |
| parser = argparse.ArgumentParser( |
| description= |
| 'run `cargo vet` against `//third_party/rust/chromium_crates_io`') |
| parser.add_argument('--rust-sysroot', |
| default=DEFAULT_SYSROOT, |
| help='use cargo and rustc from here') |
| (args, unrecognized_args) = parser.parse_known_args() |
| |
| # Avoid clobbering `config.toml` - see |
| # https://github.com/mozilla/cargo-vet/issues/589 and note that `gnrt |
| # vendor` generates this file from the `vet_config.toml.hbs` template. |
| with open(_CONFIG_TOML_PATH, 'r') as f: |
| old_config_toml = f.read() |
| |
| _CARGO_ARGS = ['-Zunstable-options', '-C', _MANIFEST_DIR] |
| _EXTRA_VET_ARGS = [ |
| # See the `[dependencies.cxxbridge-cmd]` section in |
| # `third_party/rust/chromium_crates_io/Cargo.toml` for explanation why |
| # `-Zbindeps` flag is needed. |
| '--cargo-arg=-Zbindeps', |
| '--no-registry-suggestions' |
| ] |
| success = RunCargo( |
| args.rust_sysroot, None, |
| _CARGO_ARGS + ['vet'] + unrecognized_args + _EXTRA_VET_ARGS) |
| |
| # Unclober `config.toml` changes if desirable. |
| with open(_CONFIG_TOML_PATH, 'r') as f: |
| new_config_toml = f.read() |
| if new_config_toml != old_config_toml: |
| if AreOnlyCommentsChanged(old_config_toml, new_config_toml): |
| print(f"{_SCRIPT_NAME}: NOTE: Restoring `config.toml` " \ |
| "(comment-only changes detected)") |
| with open(_CONFIG_TOML_PATH, 'w') as f: |
| f.write(old_config_toml) |
| else: |
| print(f"{_SCRIPT_NAME}: WARNING: Detected non-trivial " \ |
| "`config.toml` changes. " \ |
| "Check if `vet_config.toml.hbs` needs to be updated.") |
| |
| if not success: |
| is_presubmit = '--locked' in unrecognized_args and \ |
| '--frozen' in unrecognized_args |
| assert not is_presubmit |
| |
| return 0 if success else 1 |
| |
| |
| if __name__ == '__main__': |
| sys.exit(main()) |