-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiledb-rebuild.py
More file actions
executable file
·92 lines (73 loc) · 3.06 KB
/
compiledb-rebuild.py
File metadata and controls
executable file
·92 lines (73 loc) · 3.06 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# SPDX-License-Identifier: GPL-2.0
#
# Schspa (C) 2021
#
# Author: Schspa Shi <schspa@gmail.com>
#
"""A tool for recompile files in compile_commands.json ."""
import argparse
import json
import logging
import os
import re
import subprocess
_DEFAULT_LOG_LEVEL = 'WARNING'
_VALID_LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
def parse_arguments():
"""Sets up and parses command-line arguments.
Returns:
log_level: A logging level to filter log output.
directory: The directory to search for .cmd files.
"""
usage = 'Recompile Single file from kernel .cmd files'
parser = argparse.ArgumentParser(description=usage)
compiledb_path_help = ('Path to the compile_commands.json file'
'(defaults to the ./compile_commands.json)')
parser.add_argument('-c', '--compiledb', type=str, help=compiledb_path_help)
file_pattern_help = ('Regular expression to filter "file" key compile_commands.json'
'(defaults to "*")')
parser.add_argument('-f', '--file', type=str, help=file_pattern_help)
log_level_help = ('The level of log messages to produce (one of ' +
', '.join(_VALID_LOG_LEVELS) + '; defaults to ' +
_DEFAULT_LOG_LEVEL + ')')
parser.add_argument(
'--log_level', type=str, default=_DEFAULT_LOG_LEVEL,
help=log_level_help)
args = parser.parse_args()
log_level = args.log_level
if log_level not in _VALID_LOG_LEVELS:
raise ValueError('%s is not a valid log level' % log_level)
compiledb_path = args.compiledb or os.path.join(os.getcwd(), 'compile_commands.json')
compiledb_path = os.path.abspath(compiledb_path)
file_pattern = args.file
return log_level, file_pattern, compiledb_path
def main():
"""Walks through the directory and finds and parses .cmd files."""
log_level, file_pattern, compiledb_path = parse_arguments()
level = getattr(logging, log_level)
logging.basicConfig(format='%(levelname)s: %(message)s', level=level)
logging.debug('log_level %s, file_pattern: %s, compiledb_path: %s',
log_level, file_pattern, compiledb_path)
filename_matcher = re.compile(file_pattern)
single_file_filter = None;
if not os.path.exists(compiledb_path):
logging.error('compiledb file %s not exists', compiledb_path)
return
with open(compiledb_path, 'r') as f:
compile_commands = json.load(f)
for compile_command in compile_commands:
if filename_matcher.match(compile_command['file']):
logging.info('compile file: %s', compile_command['file'])
output = subprocess.check_output(compile_command['arguments'],
cwd=compile_command['directory'],
stderr=subprocess.STDOUT).decode("utf-8")
if output == '':
continue
logging.info(output)
pass
pass
pass
return
if __name__ == '__main__':
main()