forked from P3HPC/code-base-investigator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebasin.py
More file actions
executable file
·171 lines (153 loc) · 4.81 KB
/
codebasin.py
File metadata and controls
executable file
·171 lines (153 loc) · 4.81 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# Copyright (C) 2019-2024 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
"""
This script is the main executable of Code Base Investigator.
"""
import argparse
import logging
import os
import sys
from codebasin import config, finder, report, util
from codebasin.walkers.platform_mapper import PlatformMapper
version = "1.1.1"
def report_enabled(name):
"""
Return true if the report with the specified name is enabled.
"""
if "all" in args.reports:
return True
return name in args.reports
def guess_project_name(config_path):
"""
Guess a useful name from the given path so that we can pick
meaningful filenames for output.
"""
fullpath = os.path.realpath(config_path)
(thedir, thename) = os.path.split(fullpath)
if config_path == "config.yaml":
(base, end) = os.path.split(thedir)
res = end.strip()
else:
(base, end) = os.path.splitext(thename)
res = base.strip()
if not res:
logging.getLogger("codebasin").warning(
"Can't guess meaningful output name from input",
)
res = "unknown"
return res
if __name__ == "__main__":
# Read command-line arguments
parser = argparse.ArgumentParser(
description="Code Base Investigator v" + str(version),
)
parser.add_argument(
"-r",
"--rootdir",
dest="rootdir",
metavar="DIR",
default=os.getcwd(),
type=str,
help="Set working root directory (default .)",
)
parser.add_argument(
"-c",
"--config",
dest="config_file",
metavar="FILE",
action="store",
help="configuration file (default: <DIR>/config.yaml)",
)
parser.add_argument(
"-v",
"--verbose",
dest="verbose",
action="count",
default=0,
help="increase verbosity level",
)
parser.add_argument(
"-q",
"--quiet",
dest="quiet",
action="count",
default=0,
help="decrease verbosity level",
)
parser.add_argument(
"-R",
"--report",
dest="reports",
metavar="REPORT",
default=["all"],
choices=["all", "summary", "clustering"],
nargs="+",
help="desired output reports (default: all)",
)
parser.add_argument(
"-d",
"--dump",
dest="dump",
metavar="<file.json>",
action="store",
help="dump out annotated platform/parsing tree to <file.json>",
)
parser.add_argument(
"--batchmode",
dest="batchmode",
action="store_true",
default=False,
help="Set batch mode (additional output for bulk operation.)",
)
args = parser.parse_args()
stdout_log = logging.StreamHandler(sys.stdout)
stdout_log.setFormatter(logging.Formatter("[%(levelname)-8s] %(message)s"))
logging.getLogger("codebasin").addHandler(stdout_log)
logging.getLogger("codebasin").setLevel(
max(1, logging.WARNING - 10 * (args.verbose - args.quiet)),
)
rootdir = os.path.realpath(args.rootdir)
if args.config_file is None:
config_file = os.path.join(rootdir, "config.yaml")
else:
config_file = args.config_file
# Load the configuration file into a dict
if not util.ensure_yaml(config_file):
logging.getLogger("codebasin").error(
"Configuration file does not have YAML file extension.",
)
sys.exit(1)
codebase, configuration = config.load(config_file, rootdir)
# Parse the source tree, and determine source line associations.
# The trees and associations are housed in state.
state = finder.find(rootdir, codebase, configuration)
# Count lines for platforms
platform_mapper = PlatformMapper(codebase)
setmap = platform_mapper.walk(state)
if args.dump:
if util.ensure_json(args.dump):
report.annotated_dump(args.dump, state)
else:
logging.getLogger("codebasin").warning(
"Output path for annotation dump does not end with .json: "
f"'{args.dump}'. Skipping dump.",
)
if args.batchmode and (
report_enabled("summary") or report_enabled("clustering")
):
print(f"Config file: {config_file}")
print(f"Root: {rootdir}")
# Print summary report
if report_enabled("summary"):
summary = report.summary(setmap)
if summary is not None:
print(summary)
# Print clustering report
if report_enabled("clustering"):
output_prefix = os.path.realpath(guess_project_name(config_file))
clustering_output_name = output_prefix + "-dendrogram.png"
clustering = report.clustering(clustering_output_name, setmap)
if clustering is not None:
print(clustering)
sys.exit(0)