File: run_cargo_vet.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (95 lines) | stat: -rwxr-xr-x 3,489 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/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 pathlib
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,
                        type=pathlib.Path,
                        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'
    ]
    retcode = 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 retcode


if __name__ == '__main__':
    sys.exit(main())