File: clean-up-not-fatal-until.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (63 lines) | stat: -rwxr-xr-x 2,294 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python3
# Copyright 2025 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import os
import re
import subprocess
import sys


def main(args):
    parser = argparse.ArgumentParser(
        description='Removes usage of obsolete base::NotFatalUntil::M<N> values'
    )
    parser.add_argument('-m', '--milestone', required=True)
    parsed_args = parser.parse_args(args=args)

    try:
        milestone = int(parsed_args.milestone)
    except ValueError:
        print(f'--milestone must be a number: {parsed_args.milestone}',
              file=sys.stderr)
        return -1

    pattern = f'NotFatalUntil::M{milestone}'
    print(f'Searching for files with {pattern}...', file=sys.stderr)
    files = subprocess.check_output(
        ('git.exe' if os.name == 'nt' else 'git', 'gs', pattern,
         '--name-only')).decode('utf-8').splitlines()

    print(f'Found {len(files)} {"file" if len(files) == 1 else "files"}',
          file=sys.stderr)

    # Intended to match base::NotFatalUntil as the last argument of CHECK(),
    # CHECK_EQ(), et cetera. While this could match more things than expected,
    # as it only looks for a comma + whitespace + a base::NotFatalUntil value,
    # in practice, it more or less works.
    replace_regex = re.compile(fr',\s+?(?:base::)?{pattern}')
    # Macros that optionally take a single base::NotFatalUntil argument need a
    # separate regex.
    macro_replace_regex = re.compile(
        fr'(?P<macro>CHECK_IS_(?:NOT_)?TEST|NOTREACHED)\((?:base::)?{pattern}\)'
    )
    for f in files:
        print(f'Updating {f}...', file=sys.stderr)
        with open(f, 'r+') as f:
            contents = f.read()
            new_contents = replace_regex.sub('', contents)
            new_contents = macro_replace_regex.sub(f'\g<macro>()',
                                                   new_contents)
            if 'NotFatalUntil' not in new_contents:
                new_contents = new_contents.replace(
                    '#include "base/not_fatal_until.h"\n', '')
            f.seek(0)
            f.truncate()
            f.write(new_contents)
    print(f'Done!', file=sys.stderr)


if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))