blob: 462cbcdd7145663d962ec9c0c5e4306b3de2e587 [file] [log] [blame]
Chung-Sheng Wu3c06e4e2021-11-09 04:56:331#!/usr/bin/env python3
Mike Frysinger3a446f22022-09-08 07:37:142# Copyright 2021 The ChromiumOS Authors
Chung-Sheng Wu3c06e4e2021-11-09 04:56:333# 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
8import json
9import os
Mike Frysinger3f6a4122023-09-08 21:02:4910from pathlib import Path
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3311
Qijiang Fanb493dd52022-06-24 04:55:1512
Mike Frysinger3f6a4122023-09-08 21:02:4913_HACK_VAR_TO_DISABLE_ISORT = "hack"
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3314# pylint: disable=wrong-import-position
Mike Frysinger3f6a4122023-09-08 21:02:4915import chromite_init # pylint: disable=unused-import
16
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3317from chromite.lib import commandline
18from chromite.lib import constants
19from chromite.lib import cros_build_lib
20from chromite.lib import git
Qijiang Fanb493dd52022-06-24 04:55:1521
22
Mike Frysinger3f6a4122023-09-08 21:02:4923TOP_DIR = Path(__file__).resolve().parent.parent
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3324
25CHECK_STABLE_MOJOM_COMPATIBILITY = os.path.join(
Jack Rosenthal28866312022-07-19 17:21:3026 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 Wu3c06e4e2021-11-09 04:56:3336
37
38def GetDiffFiles(from_commit, to_commit):
39 """Returns the changed file list."""
Jack Rosenthal28866312022-07-19 17:21:3040 cmd = ["diff", "--name-only", from_commit, to_commit]
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3341 res = git.RunGit(TOP_DIR, cmd)
42 return res.stdout.splitlines()
43
44
45def GetRenameMap(from_commit, to_commit):
46 """Returns a new name to old name mapping for renamed files."""
Jack Rosenthal28866312022-07-19 17:21:3047 cmd = ["diff", "--name-status", "--diff-filter=R", from_commit, to_commit]
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3348 res = git.RunGit(TOP_DIR, cmd)
49 rename_map = {}
50 for line in res.stdout.splitlines():
Jack Rosenthal28866312022-07-19 17:21:3051 tokens = line.split("\t")
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3352 rename_map[tokens[2]] = tokens[1]
53 return rename_map
54
55
56def GetFileContent(commit, file):
57 """Returns the content of a file in a commit."""
Jack Rosenthal28866312022-07-19 17:21:3058 cmd = ["show", f"{commit}:{file}"]
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3359 res = git.RunGit(TOP_DIR, cmd, check=False)
Jack Rosenthal28866312022-07-19 17:21:3060 return "" if res.returncode else res.stdout
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3361
62
63def CheckMojoStable(commit):
64 """Checks if the mojom files follow the stable rules."""
Jack Rosenthal28866312022-07-19 17:21:3065 last_commit = f"{commit}^"
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3366 rename_map = GetRenameMap(last_commit, commit)
67 delta = []
68 for file in GetDiffFiles(last_commit, commit):
Jack Rosenthal28866312022-07-19 17:21:3069 if not file.endswith(".mojom"):
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3370 continue
71 old_file = rename_map.get(file, file)
Chung-Sheng Wu5bb50e42023-01-07 03:05:1572 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 Rosenthal28866312022-07-19 17:21:3095 cmd = [CHECK_STABLE_MOJOM_COMPATIBILITY, "--src-root", TOP_DIR]
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3396 res = cros_build_lib.run(cmd, input=json.dumps(delta), check=False)
97 if res.returncode:
Jack Rosenthal28866312022-07-19 17:21:3098 cros_build_lib.Die("Please fix the mojom error.")
Chung-Sheng Wu3c06e4e2021-11-09 04:56:3399
100
101def GetParser():
102 """Returns an argument parser."""
103 parser = commandline.ArgumentParser(description=__doc__)
Jack Rosenthal28866312022-07-19 17:21:30104 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 Wu3c06e4e2021-11-09 04:56:33110 return parser
111
112
113def 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 Rosenthal28866312022-07-19 17:21:30119 commit = opts.commit if opts.commit != "pre-submit" else "HEAD"
Chung-Sheng Wu3c06e4e2021-11-09 04:56:33120 return CheckMojoStable(commit)
121
122
Jack Rosenthal28866312022-07-19 17:21:30123if __name__ == "__main__":
Chung-Sheng Wu3c06e4e2021-11-09 04:56:33124 commandline.ScriptWrapperMain(lambda _: main)