Skip to content

Commit 5fc4f8f

Browse files
committed
Replace uses of code base dictionary with CodeBase
Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 9e6297f commit 5fc4f8f

File tree

4 files changed

+14
-57
lines changed

4 files changed

+14
-57
lines changed

bin/codebasin

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import logging
1010
import os
1111
import sys
1212

13-
from codebasin import config, finder, report, util
13+
from codebasin import CodeBase, config, finder, report, util
1414
from codebasin.walkers.platform_mapper import PlatformMapper
1515

1616
version = "1.2.0"
@@ -107,14 +107,7 @@ def main():
107107
# Determine the root directory based on where codebasin is run.
108108
rootdir = os.path.realpath(os.getcwd())
109109

110-
# Set up a default codebase and configuration object.
111-
codebase = {
112-
"files": [],
113-
"platforms": [],
114-
"exclude_files": set(),
115-
"exclude_patterns": args.excludes,
116-
"rootdir": rootdir,
117-
}
110+
# Set up a default configuration object.
118111
configuration = {}
119112

120113
# Load the analysis file if it exists.
@@ -132,8 +125,7 @@ def main():
132125

133126
if "codebase" in analysis_toml:
134127
if "exclude" in analysis_toml["codebase"]:
135-
excludes = analysis_toml["codebase"]["exclude"]
136-
codebase["exclude_patterns"] += excludes
128+
args.excludes += analysis_toml["codebase"]["exclude"]
137129

138130
for name in args.platforms:
139131
if name not in analysis_toml["platform"].keys():
@@ -142,16 +134,20 @@ def main():
142134
+ "does not exist in the configuration file.",
143135
)
144136

137+
cmd_platforms = args.platforms.copy()
145138
for name in analysis_toml["platform"].keys():
146-
if args.platforms and name not in args.platforms:
139+
if cmd_platforms and name not in cmd_platforms:
147140
continue
148141
if "commands" not in analysis_toml["platform"][name]:
149142
raise ValueError(f"Missing 'commands' for platform {name}")
150143
p = analysis_toml["platform"][name]["commands"]
151144
db = config.load_database(p, rootdir)
152-
codebase["platforms"].append(name)
145+
args.platforms.append(name)
153146
configuration.update({name: db})
154147

148+
# Construct a codebase object associated with the root directory.
149+
codebase = CodeBase(rootdir, exclude_patterns=args.excludes)
150+
155151
# Parse the source tree, and determine source line associations.
156152
# The trees and associations are housed in state.
157153
state = finder.find(
@@ -180,8 +176,7 @@ def main():
180176
if report_enabled("clustering"):
181177
basename = os.path.basename(args.analysis_file)
182178
filename = os.path.splitext(basename)[0]
183-
platform_names = [p for p in codebase["platforms"]]
184-
output_prefix = "-".join([filename] + platform_names)
179+
output_prefix = "-".join([filename] + args.platforms)
185180

186181
clustering_output_name = output_prefix + "-dendrogram.png"
187182
clustering = report.clustering(clustering_output_name, setmap)

codebasin/finder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ def find(
142142

143143
# Build a tree for each unique file for all platforms.
144144
state = ParserState(summarize_only)
145-
for f in codebase["files"]:
145+
for f in codebase:
146146
state.insert_file(f)
147147
for p in configuration:
148148
for e in configuration[p]:
149-
if e["file"] not in codebase["files"]:
149+
if e["file"] not in codebase:
150150
filename = e["file"]
151151
if legacy_warnings:
152152
log.warning(

codebasin/walkers/exporter.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from codebasin import util
88
from codebasin.preprocessor import CodeNode, FileNode
9-
from codebasin.walkers.platform_mapper import exclude
109
from codebasin.walkers.tree_walker import TreeWalker
1110

1211
log = logging.getLogger("codebasin")
@@ -38,10 +37,7 @@ def walk(self, state):
3837
def _export_node(self, _filename, _node, _map):
3938
# Do not export files that the user does not consider to be part of
4039
# the codebase
41-
if isinstance(_node, FileNode) and exclude(
42-
_node.filename,
43-
self.codebase,
44-
):
40+
if isinstance(_node, FileNode) and _node.filename not in self.codebase:
4541
return
4642

4743
if isinstance(_node, CodeNode):

codebasin/walkers/platform_mapper.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,13 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
import logging
5-
import os
6-
7-
import pathspec
85

96
from codebasin.preprocessor import CodeNode, FileNode
107
from codebasin.walkers.tree_mapper import TreeMapper
118

129
log = logging.getLogger("codebasin")
1310

1411

15-
def exclude(filename, cb):
16-
# Always exclude files that were explicitly listed as excluded.
17-
if filename in cb["exclude_files"]:
18-
log.info(f"Excluding {filename}; matches 'exclude_files'.")
19-
return True
20-
21-
# Only exclude files outside of the root directory if they weren't
22-
# explicitly listed as part of the codebase.
23-
path = os.path.realpath(filename)
24-
if not path.startswith(cb["rootdir"]):
25-
if filename in cb["files"]:
26-
return False
27-
log.info(f"Excluding {filename}; outside of root directory.")
28-
return True
29-
30-
# Exclude files matching an exclude pattern.
31-
#
32-
# Use GitIgnoreSpec to match git behavior in weird corner cases.
33-
# Convert relative paths to match .gitignore subdirectory behavior.
34-
spec = pathspec.GitIgnoreSpec.from_lines(cb["exclude_patterns"])
35-
rel = os.path.relpath(path, cb["rootdir"])
36-
if spec.match_file(rel):
37-
log.info(f"Excluding {filename}; matches exclude pattern.")
38-
return True
39-
40-
return False
41-
42-
4312
class PlatformMapper(TreeMapper):
4413
"""
4514
Specific TreeMapper that builds a mapping of nodes to platforms.
@@ -57,10 +26,7 @@ def _map_node(self, _node, _map):
5726
"""
5827
# Do not map files that the user does not consider to be part of
5928
# the codebase
60-
if isinstance(_node, FileNode) and exclude(
61-
_node.filename,
62-
self.codebase,
63-
):
29+
if isinstance(_node, FileNode) and _node.filename not in self.codebase:
6430
return
6531

6632
if isinstance(_node, CodeNode):

0 commit comments

Comments
 (0)