|
| 1 | +#!{PythonPath} |
| 2 | + |
| 3 | +# Copyright 2023 Ericsson AB |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | + |
| 18 | +import os |
| 19 | +import re |
| 20 | +import shutil |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +from typing import Optional |
| 24 | + |
| 25 | +# The output directory for CodeChecker |
| 26 | +DATA_DIR: Optional[str] = None |
| 27 | +# The file to be analyzed |
| 28 | +FILE_PATH: Optional[str] = None |
| 29 | +# List of pairs of analyzers and their plist files |
| 30 | +ANALYZER_PLIST_PATHS: Optional[list[list[str]]] = None |
| 31 | +LOG_FILE: Optional[str] = None |
| 32 | +COMPILE_COMMANDS_JSON: str = "{compile_commands_json}" |
| 33 | +COMPILE_COMMANDS_ABSOLUTE: str = f"{COMPILE_COMMANDS_JSON}.abs" |
| 34 | +CODECHECKER_ARGS: str = "{codechecker_args}" |
| 35 | + |
| 36 | + |
| 37 | +def parse_args(): |
| 38 | + """ |
| 39 | + Parse arguments that may change from file-to-file. |
| 40 | + """ |
| 41 | + if len(sys.argv) != 5: |
| 42 | + print("Wrong amount of arguments") |
| 43 | + sys.exit(1) |
| 44 | + |
| 45 | + global DATA_DIR |
| 46 | + global FILE_PATH |
| 47 | + global LOG_FILE |
| 48 | + global ANALYZER_PLIST_PATHS |
| 49 | + |
| 50 | + DATA_DIR = sys.argv[1] |
| 51 | + FILE_PATH = sys.argv[2] |
| 52 | + LOG_FILE = sys.argv[3] |
| 53 | + ANALYZER_PLIST_PATHS = [item.split(",") for item in sys.argv[4].split(";")] |
| 54 | + |
| 55 | + |
| 56 | +def log(msg: str) -> None: |
| 57 | + """ |
| 58 | + Append message to the log file |
| 59 | + """ |
| 60 | + with open(LOG_FILE, "a") as log_file: # type: ignore |
| 61 | + log_file.write(msg) |
| 62 | + |
| 63 | + |
| 64 | +def _create_compile_commands_json_with_absolute_paths(): |
| 65 | + """ |
| 66 | + Modifies the paths in compile_commands.json to contain the absolute path |
| 67 | + of the files. |
| 68 | + """ |
| 69 | + with open(COMPILE_COMMANDS_JSON, "r") as original_file, open( |
| 70 | + COMPILE_COMMANDS_ABSOLUTE, "w" |
| 71 | + ) as new_file: |
| 72 | + content = original_file.read() |
| 73 | + # Replace "directory":"." with the absolute path |
| 74 | + # of the current working directory |
| 75 | + new_content = content.replace( |
| 76 | + '"directory":".', f'"directory":"{os.getcwd()}' |
| 77 | + ) |
| 78 | + new_file.write(new_content) |
| 79 | + |
| 80 | + |
| 81 | +def _run_codechecker() -> None: |
| 82 | + """ |
| 83 | + Runs CodeChecker analyze |
| 84 | + """ |
| 85 | + log( |
| 86 | + f"CodeChecker command: CodeChecker analyze {CODECHECKER_ARGS} \ |
| 87 | +{COMPILE_COMMANDS_ABSOLUTE} --output={DATA_DIR} --file=*/{FILE_PATH}\n" |
| 88 | + ) |
| 89 | + log("===-----------------------------------------------------===\n") |
| 90 | + log(" CodeChecker error log \n") |
| 91 | + log("===-----------------------------------------------------===\n") |
| 92 | + |
| 93 | + result = subprocess.run( |
| 94 | + ["echo", "$PATH"], |
| 95 | + shell=True, |
| 96 | + env=os.environ, |
| 97 | + capture_output=True, |
| 98 | + text=True, |
| 99 | + ) |
| 100 | + log(result.stdout) |
| 101 | + |
| 102 | + codechecker_cmd: list[str] = ( |
| 103 | + ["CodeChecker", "analyze"] |
| 104 | + + CODECHECKER_ARGS.split() |
| 105 | + + ["--output=" + DATA_DIR] # type: ignore |
| 106 | + + ["--file=*/" + FILE_PATH] # type: ignore |
| 107 | + + [COMPILE_COMMANDS_ABSOLUTE] |
| 108 | + ) |
| 109 | + |
| 110 | + try: |
| 111 | + with open(LOG_FILE, "a") as log_file: # type: ignore |
| 112 | + subprocess.run( |
| 113 | + codechecker_cmd, |
| 114 | + env=os.environ, |
| 115 | + stdout=log_file, |
| 116 | + stderr=log_file, |
| 117 | + check=True, |
| 118 | + ) |
| 119 | + except subprocess.CalledProcessError as e: |
| 120 | + log(e.output.decode() if e.output else "") |
| 121 | + if e.returncode == 1 or e.returncode >= 128: |
| 122 | + _display_error(e.returncode) |
| 123 | + |
| 124 | + |
| 125 | +def _display_error(ret_code: int) -> None: |
| 126 | + """ |
| 127 | + Display the log file, and exit with 1 |
| 128 | + """ |
| 129 | + # Log and exit on error |
| 130 | + print("===-----------------------------------------------------===") |
| 131 | + print(f"[ERROR]: CodeChecker returned with {ret_code}!") |
| 132 | + with open(LOG_FILE, "r") as log_file: # type: ignore |
| 133 | + print(log_file.read()) |
| 134 | + sys.exit(1) |
| 135 | + |
| 136 | + |
| 137 | +def _move_plist_files(): |
| 138 | + """ |
| 139 | + Move the plist files from the temporary directory to their final destination |
| 140 | + """ |
| 141 | + # NOTE: the following we do to get rid of md5 hash in plist file names |
| 142 | + # Copy the plist files to the specified destinations |
| 143 | + for file in os.listdir(DATA_DIR): |
| 144 | + for analyzer_info in ANALYZER_PLIST_PATHS: # type: ignore |
| 145 | + if re.search( |
| 146 | + rf"_{analyzer_info[0]}_.*\.plist$", file |
| 147 | + ) and os.path.isfile( |
| 148 | + os.path.join(DATA_DIR, file) |
| 149 | + ): # type: ignore |
| 150 | + shutil.move(os.path.join(DATA_DIR, file), analyzer_info[1]) # type: ignore |
| 151 | + |
| 152 | + |
| 153 | +def main(): |
| 154 | + parse_args() |
| 155 | + _create_compile_commands_json_with_absolute_paths() |
| 156 | + _run_codechecker() |
| 157 | + _move_plist_files() |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + main() |
| 162 | + |
| 163 | + |
| 164 | +# I have conserved this comment from the original bash script |
| 165 | +# The sed commands are commented out, so we won't implement them |
| 166 | +# # sed -i -e "s|<string>.*execroot/bazel_codechecker/|<string>|g" $CLANG_TIDY_PLIST |
| 167 | +# # sed -i -e "s|<string>.*execroot/bazel_codechecker/|<string>|g" $CLANGSA_PLIST |
0 commit comments