Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mike Frysinger | 3a446f2 | 2022-09-08 07:37:14 | [diff] [blame] | 2 | # Copyright 2021 The ChromiumOS Authors |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Linter for *.mojom files.""" |
| 7 | |
| 8 | import json |
| 9 | import os |
Mike Frysinger | 3f6a412 | 2023-09-08 21:02:49 | [diff] [blame] | 10 | from pathlib import Path |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 11 | |
Qijiang Fan | b493dd5 | 2022-06-24 04:55:15 | [diff] [blame] | 12 | |
Mike Frysinger | 3f6a412 | 2023-09-08 21:02:49 | [diff] [blame] | 13 | _HACK_VAR_TO_DISABLE_ISORT = "hack" |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 14 | # pylint: disable=wrong-import-position |
Mike Frysinger | 3f6a412 | 2023-09-08 21:02:49 | [diff] [blame] | 15 | import chromite_init # pylint: disable=unused-import |
| 16 | |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 17 | from chromite.lib import commandline |
| 18 | from chromite.lib import constants |
| 19 | from chromite.lib import cros_build_lib |
| 20 | from chromite.lib import git |
Qijiang Fan | b493dd5 | 2022-06-24 04:55:15 | [diff] [blame] | 21 | |
| 22 | |
Mike Frysinger | 3f6a412 | 2023-09-08 21:02:49 | [diff] [blame] | 23 | TOP_DIR = Path(__file__).resolve().parent.parent |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 24 | |
| 25 | CHECK_STABLE_MOJOM_COMPATIBILITY = os.path.join( |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 26 | constants.SOURCE_ROOT, |
| 27 | "src", |
| 28 | "platform", |
| 29 | "libchrome", |
| 30 | "mojo", |
| 31 | "public", |
| 32 | "tools", |
| 33 | "mojom", |
| 34 | "check_stable_mojom_compatibility.py", |
| 35 | ) |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 36 | |
| 37 | |
| 38 | def GetDiffFiles(from_commit, to_commit): |
| 39 | """Returns the changed file list.""" |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 40 | cmd = ["diff", "--name-only", from_commit, to_commit] |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 41 | res = git.RunGit(TOP_DIR, cmd) |
| 42 | return res.stdout.splitlines() |
| 43 | |
| 44 | |
| 45 | def GetRenameMap(from_commit, to_commit): |
| 46 | """Returns a new name to old name mapping for renamed files.""" |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 47 | cmd = ["diff", "--name-status", "--diff-filter=R", from_commit, to_commit] |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 48 | res = git.RunGit(TOP_DIR, cmd) |
| 49 | rename_map = {} |
| 50 | for line in res.stdout.splitlines(): |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 51 | tokens = line.split("\t") |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 52 | rename_map[tokens[2]] = tokens[1] |
| 53 | return rename_map |
| 54 | |
| 55 | |
| 56 | def GetFileContent(commit, file): |
| 57 | """Returns the content of a file in a commit.""" |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 58 | cmd = ["show", f"{commit}:{file}"] |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 59 | res = git.RunGit(TOP_DIR, cmd, check=False) |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 60 | return "" if res.returncode else res.stdout |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 61 | |
| 62 | |
| 63 | def CheckMojoStable(commit): |
| 64 | """Checks if the mojom files follow the stable rules.""" |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 65 | last_commit = f"{commit}^" |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 66 | rename_map = GetRenameMap(last_commit, commit) |
| 67 | delta = [] |
| 68 | for file in GetDiffFiles(last_commit, commit): |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 69 | if not file.endswith(".mojom"): |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 70 | continue |
| 71 | old_file = rename_map.get(file, file) |
Chung-Sheng Wu | 5bb50e4 | 2023-01-07 03:05:15 | [diff] [blame] | 72 | old_file_content = GetFileContent(last_commit, old_file) |
| 73 | new_file_content = GetFileContent(commit, file) |
| 74 | if old_file != file: |
| 75 | delta += [ |
| 76 | { |
| 77 | "filename": old_file, |
| 78 | "old": old_file_content, |
| 79 | "new": None, |
| 80 | }, |
| 81 | { |
| 82 | "filename": file, |
| 83 | "old": None, |
| 84 | "new": new_file_content, |
| 85 | }, |
| 86 | ] |
| 87 | else: |
| 88 | delta.append( |
| 89 | { |
| 90 | "filename": file, |
| 91 | "old": old_file_content, |
| 92 | "new": new_file_content, |
| 93 | } |
| 94 | ) |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 95 | cmd = [CHECK_STABLE_MOJOM_COMPATIBILITY, "--src-root", TOP_DIR] |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 96 | res = cros_build_lib.run(cmd, input=json.dumps(delta), check=False) |
| 97 | if res.returncode: |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 98 | cros_build_lib.Die("Please fix the mojom error.") |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 99 | |
| 100 | |
| 101 | def GetParser(): |
| 102 | """Returns an argument parser.""" |
| 103 | parser = commandline.ArgumentParser(description=__doc__) |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 104 | parser.add_argument( |
| 105 | "--commit", |
| 106 | required=True, |
| 107 | help="The git commit to be checked. " |
| 108 | 'Pass "pre-submit" for the pre-submit check.', |
| 109 | ) |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 110 | return parser |
| 111 | |
| 112 | |
| 113 | def main(argv): |
| 114 | parser = GetParser() |
| 115 | opts = parser.parse_args(argv) |
| 116 | opts.Freeze() |
| 117 | |
| 118 | # If we are running a pre-submit check, check the HEAD. |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 119 | commit = opts.commit if opts.commit != "pre-submit" else "HEAD" |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 120 | return CheckMojoStable(commit) |
| 121 | |
| 122 | |
Jack Rosenthal | 2886631 | 2022-07-19 17:21:30 | [diff] [blame] | 123 | if __name__ == "__main__": |
Chung-Sheng Wu | 3c06e4e | 2021-11-09 04:56:33 | [diff] [blame] | 124 | commandline.ScriptWrapperMain(lambda _: main) |