Skip to content

Commit a3e7be4

Browse files
authored
Merge pull request #76 from Pennycook/simplify-platform-option
Simplify platform option
2 parents 40c9346 + a09cbe6 commit a3e7be4

File tree

1 file changed

+15
-40
lines changed

1 file changed

+15
-40
lines changed

bin/codebasin

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,9 @@ def main():
118118
metavar="<platform>",
119119
action="append",
120120
default=[],
121-
help="Add the specified platform to the analysis. "
122-
+ "May be a name or a path to a compilation database. "
121+
help="Include the specified platform in the analysis. "
123122
+ "May be specified multiple times. "
124-
+ "If not specified, all known platforms will be included.",
123+
+ "If not specified, all platforms will be included.",
125124
)
126125
# The analysis-file argument is optional while we support the -c option.
127126
parser.add_argument(
@@ -175,33 +174,9 @@ def main():
175174
"Cannot use --config (-c) with TOML analysis files.",
176175
)
177176

178-
# Process the -p flag first to infer wider context.
179-
filtered_platforms = []
180-
additional_platforms = []
181-
for p in args.platforms:
182-
# If it's a path, it has to be a compilation database.
183-
if os.path.exists(p):
184-
if not os.path.splitext(p)[1] == ".json":
185-
raise RuntimeError(f"Platform file {p} must end in .json.")
186-
additional_platforms.append(p)
187-
continue
188-
189-
# Otherwise, treat it as a name in the configuration file.
190-
# Explain the logic above in cases that look suspiciously like paths.
191-
if "/" in p or os.path.splitext(p)[1] == ".json":
192-
logging.getLogger("codebasin").warning(
193-
f"{p} doesn't exist, so will be treated as a name.",
194-
)
195-
filtered_platforms.append(p)
196-
197-
# A legacy config file is required if:
198-
# - No additional platforms are specified; and
199-
# - No TOML analysis file is specified
177+
# If no file is specified, legacy behavior checks for config.yaml
200178
config_file = args.config_file
201-
config_required = (
202-
len(additional_platforms) == 0 and args.analysis_file is None
203-
)
204-
if config_file is None and config_required:
179+
if args.config_file is None and args.analysis_file is None:
205180
warnings.warn(
206181
"Implicitly defined configuration files are deprecated.",
207182
DeprecationWarning,
@@ -236,15 +211,15 @@ def main():
236211
config_file,
237212
rootdir,
238213
exclude_patterns=args.excludes,
239-
filtered_platforms=filtered_platforms,
214+
filtered_platforms=args.platforms,
240215
)
241216

242217
# Load the analysis file if it exists.
243218
if args.analysis_file is not None:
244219
path = os.path.realpath(args.analysis_file)
245220
if os.path.exists(path):
246221
if not os.path.splitext(path)[1] == ".toml":
247-
raise RuntimeError(f"Analysis file {p} must end in .toml.")
222+
raise RuntimeError(f"Analysis file {path} must end in .toml.")
248223

249224
with util.safe_open_read_nofollow(path, "rb") as f:
250225
try:
@@ -257,23 +232,23 @@ def main():
257232
excludes = analysis_toml["codebase"]["exclude"]
258233
codebase["exclude_patterns"] += excludes
259234

235+
for name in args.platforms:
236+
if name not in analysis_toml["platform"].keys():
237+
raise RuntimeError(
238+
f"Platform {name} requested on the command line "
239+
+ "does not exist in the configuration file.",
240+
)
241+
260242
for name in analysis_toml["platform"].keys():
261-
if filtered_platforms and name not in filtered_platforms:
243+
if args.platforms and name not in args.platforms:
262244
continue
263245
if "commands" not in analysis_toml["platform"][name]:
264246
raise ValueError(f"Missing 'commands' for platform {name}")
265247
p = analysis_toml["platform"][name]["commands"]
266248
db = config.load_database(p, rootdir)
249+
codebase["platforms"].append(name)
267250
configuration.update({name: db})
268251

269-
# Extend configuration with any additional platforms.
270-
for p in additional_platforms:
271-
name = os.path.splitext(os.path.basename(p))[0]
272-
if name in codebase["platforms"]:
273-
raise RuntimeError(f"Platform name {p} conflicts with {name}.")
274-
db = config.load_database(p, rootdir)
275-
configuration.update({name: db})
276-
277252
# Parse the source tree, and determine source line associations.
278253
# The trees and associations are housed in state.
279254
legacy_warnings = True if config_file else False

0 commit comments

Comments
 (0)