-
Notifications
You must be signed in to change notification settings - Fork 3
Rewrite code_checker bash wrapper in python #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5bfb078
Add code_checker_script.py to replace the inline code_checker bash wr…
furtib 7de4e31
Expand template only once, pass variables as argument instead
furtib 4e9e24d
Beautify
furtib 05f62ee
Fix incorrect type
furtib 0847cd4
Rename code_checker_script to per_file_script
furtib bf07dfd
Change to move
furtib 6afcac8
make comments nicer
furtib f988dd4
Move argument parsing to a separate function
furtib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| #!{PythonPath} | ||
|
|
||
| # Copyright 2023 Ericsson AB | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| import os | ||
| import re | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| from typing import Optional | ||
|
|
||
| # The output directory for CodeChecker | ||
| DATA_DIR: Optional[str] = None | ||
| # The file to be analyzed | ||
| FILE_PATH: Optional[str] = None | ||
| # List of pairs of analyzers and their plist files | ||
| ANALYZER_PLIST_PATHS: Optional[list[list[str]]] = None | ||
| LOG_FILE: Optional[str] = None | ||
| COMPILE_COMMANDS_JSON: str = "{compile_commands_json}" | ||
| COMPILE_COMMANDS_ABSOLUTE: str = f"{COMPILE_COMMANDS_JSON}.abs" | ||
| CODECHECKER_ARGS: str = "{codechecker_args}" | ||
|
|
||
|
|
||
| def parse_args(): | ||
| """ | ||
| Parse arguments that may change from file-to-file. | ||
| """ | ||
| if len(sys.argv) != 5: | ||
| print("Wrong amount of arguments") | ||
| sys.exit(1) | ||
|
|
||
| global DATA_DIR | ||
| global FILE_PATH | ||
| global LOG_FILE | ||
| global ANALYZER_PLIST_PATHS | ||
|
|
||
| DATA_DIR = sys.argv[1] | ||
| FILE_PATH = sys.argv[2] | ||
| LOG_FILE = sys.argv[3] | ||
| ANALYZER_PLIST_PATHS = [item.split(",") for item in sys.argv[4].split(";")] | ||
|
|
||
|
|
||
| def log(msg: str) -> None: | ||
| """ | ||
| Append message to the log file | ||
| """ | ||
| with open(LOG_FILE, "a") as log_file: # type: ignore | ||
| log_file.write(msg) | ||
|
|
||
|
|
||
| def _create_compile_commands_json_with_absolute_paths(): | ||
| """ | ||
| Modifies the paths in compile_commands.json to contain the absolute path | ||
| of the files. | ||
| """ | ||
| with open(COMPILE_COMMANDS_JSON, "r") as original_file, open( | ||
| COMPILE_COMMANDS_ABSOLUTE, "w" | ||
| ) as new_file: | ||
| content = original_file.read() | ||
| # Replace "directory":"." with the absolute path | ||
| # of the current working directory | ||
| new_content = content.replace( | ||
| '"directory":".', f'"directory":"{os.getcwd()}' | ||
| ) | ||
| new_file.write(new_content) | ||
|
|
||
|
|
||
| def _run_codechecker() -> None: | ||
| """ | ||
| Runs CodeChecker analyze | ||
| """ | ||
| log( | ||
| f"CodeChecker command: CodeChecker analyze {CODECHECKER_ARGS} \ | ||
| {COMPILE_COMMANDS_ABSOLUTE} --output={DATA_DIR} --file=*/{FILE_PATH}\n" | ||
| ) | ||
| log("===-----------------------------------------------------===\n") | ||
| log(" CodeChecker error log \n") | ||
| log("===-----------------------------------------------------===\n") | ||
|
|
||
| result = subprocess.run( | ||
| ["echo", "$PATH"], | ||
| shell=True, | ||
| env=os.environ, | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
| log(result.stdout) | ||
|
|
||
| codechecker_cmd: list[str] = ( | ||
| ["CodeChecker", "analyze"] | ||
| + CODECHECKER_ARGS.split() | ||
| + ["--output=" + DATA_DIR] # type: ignore | ||
| + ["--file=*/" + FILE_PATH] # type: ignore | ||
| + [COMPILE_COMMANDS_ABSOLUTE] | ||
| ) | ||
|
|
||
| try: | ||
| with open(LOG_FILE, "a") as log_file: # type: ignore | ||
| subprocess.run( | ||
| codechecker_cmd, | ||
| env=os.environ, | ||
| stdout=log_file, | ||
| stderr=log_file, | ||
| check=True, | ||
| ) | ||
| except subprocess.CalledProcessError as e: | ||
| log(e.output.decode() if e.output else "") | ||
| if e.returncode == 1 or e.returncode >= 128: | ||
| _display_error(e.returncode) | ||
|
|
||
|
|
||
| def _display_error(ret_code: int) -> None: | ||
| """ | ||
| Display the log file, and exit with 1 | ||
| """ | ||
| # Log and exit on error | ||
| print("===-----------------------------------------------------===") | ||
| print(f"[ERROR]: CodeChecker returned with {ret_code}!") | ||
| with open(LOG_FILE, "r") as log_file: # type: ignore | ||
| print(log_file.read()) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def _move_plist_files(): | ||
| """ | ||
| Move the plist files from the temporary directory to their final destination | ||
| """ | ||
| # NOTE: the following we do to get rid of md5 hash in plist file names | ||
| # Copy the plist files to the specified destinations | ||
| for file in os.listdir(DATA_DIR): | ||
| for analyzer_info in ANALYZER_PLIST_PATHS: # type: ignore | ||
| if re.search( | ||
| rf"_{analyzer_info[0]}_.*\.plist$", file | ||
| ) and os.path.isfile( | ||
| os.path.join(DATA_DIR, file) | ||
| ): # type: ignore | ||
| shutil.move(os.path.join(DATA_DIR, file), analyzer_info[1]) # type: ignore | ||
|
|
||
|
|
||
| def main(): | ||
| parse_args() | ||
| _create_compile_commands_json_with_absolute_paths() | ||
| _run_codechecker() | ||
| _move_plist_files() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
|
||
|
|
||
| # I have conserved this comment from the original bash script | ||
| # The sed commands are commented out, so we won't implement them | ||
| # # sed -i -e "s|<string>.*execroot/bazel_codechecker/|<string>|g" $CLANG_TIDY_PLIST | ||
| # # sed -i -e "s|<string>.*execroot/bazel_codechecker/|<string>|g" $CLANGSA_PLIST | ||
furtib marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.