Skip to content

Commit

Permalink
Merge pull request #56 from Pennycook/cbiconfig
Browse files Browse the repository at this point in the history
Replace importcfg.json with config TOML file
  • Loading branch information
Pennycook authored Feb 13, 2024
2 parents cc416b4 + 6bb2fc1 commit 8b8105b
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 42 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include codebasin/schema/compilation-database.schema
include codebasin/schema/config.schema
include codebasin/schema/coverage-0.1.0.schema
include codebasin/schema/importcfg.schema
include codebasin/schema/cbiconfig.schema
14 changes: 7 additions & 7 deletions codebasin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,16 @@ def load_importcfg():
"""
global _importcfg
_importcfg = collections.defaultdict(list)
path = ".cbi/importcfg.json"
path = ".cbi/config"
if os.path.exists(path):
log.info(f"Found import configuration file at {path}")
with open(path) as f:
log.info(f"Found configuration file at {path}")
with util.safe_open_read_nofollow(path, "rb") as f:
try:
_importcfg_json = util._load_json(f, "importcfg")
for compiler in _importcfg_json["compilers"]:
_importcfg[compiler["name"]] = compiler["options"]
_importcfg_toml = util._load_toml(f, "cbiconfig")
for name, compiler in _importcfg_toml["compiler"].items():
_importcfg[name] = compiler["options"]
except BaseException:
log.error("importcfg file failed validation")
log.error("Configuration file failed validation")


class Compiler:
Expand Down
2 changes: 1 addition & 1 deletion codebasin/schema/config.schema
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/intel/code-base-investigator/main/codebasin/schema/config.schema",
"title": "Code Base Investigator Configuration File",
"title": "Legacy Code Base Investigator Configuration File",
"description": "Lists codebase files and compilation options",
"type": "object",
"properties": {
Expand Down
30 changes: 0 additions & 30 deletions codebasin/schema/importcfg.schema

This file was deleted.

68 changes: 65 additions & 3 deletions codebasin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import logging
import os
import pkgutil
import tomllib
import typing
import warnings
from collections.abc import Iterable
Expand Down Expand Up @@ -122,7 +123,7 @@ def _validate_json(json_object: object, schema_name: str) -> bool:
json_object : Object
The JSON to validate.
schema_name : {'compiledb', 'config', 'coverage', 'importcfg'}
schema_name : {'compiledb', 'config', 'coverage', 'cbiconfig'}
The schema to validate against.
Returns
Expand All @@ -142,7 +143,7 @@ def _validate_json(json_object: object, schema_name: str) -> bool:
"compiledb": "schema/compilation-database.schema",
"config": "schema/config.schema",
"coverage": "schema/coverage-0.1.0.schema",
"importcfg": "schema/importcfg.schema",
"cbiconfig": "schema/cbiconfig.schema",
}
if schema_name not in schema_paths.keys():
raise ValueError("Unrecognized schema name.")
Expand Down Expand Up @@ -199,6 +200,37 @@ def _validate_yaml(yaml_object: object, schema_name: str) -> bool:
return _validate_json(yaml_object, schema_name)


def _validate_toml(toml_object: object, schema_name: str) -> bool:
"""
Validate TOML against a schema.
Parameters
----------
yaml_object : Object
The YAML to validate.
schema_name : {'cbiconfig'}
The schema to validate against.
Returns
-------
bool
True if the TOML is valid.
Raises
------
ValueError
If the TOML fails to validate, or the schema name is unrecognized.
RuntimeError
If the schema file cannot be located.
"""
if schema_name != "cbiconfig":
raise ValueError("Unrecognized schema name.")

return _validate_json(toml_object, schema_name)


def _load_json(file_object: typing.TextIO, schema_name: str) -> object:
"""
Load JSON from file and validate it against a schema.
Expand All @@ -208,7 +240,7 @@ def _load_json(file_object: typing.TextIO, schema_name: str) -> object:
file_object : typing.TextIO
The file object to load from.
schema_name : {'compiledb', 'config', 'coverage', 'importcfg'}
schema_name : {'compiledb', 'config', 'coverage'}
The schema to validate against.
Returns
Expand All @@ -229,6 +261,36 @@ def _load_json(file_object: typing.TextIO, schema_name: str) -> object:
return json_object


def _load_toml(file_object: typing.TextIO, schema_name: str) -> object:
"""
Load TOML from file and validate it against a schema.
Parameters
----------
file_object : typing.TextIO
The file object to load from.
schema_name : {'cbiconfig'}
The schema to validate against.
Returns
-------
Object
The loaded JSON.
Raises
------
ValueError
If the JSON fails to validate, or the schema name is unrecognized.
RuntimeError
If the schema file cannot be located.
"""
toml_object = tomllib.load(file_object)
_validate_json(toml_object, schema_name)
return toml_object


def validate_coverage_json(json_string: str) -> bool:
"""
Validate coverage JSON string against schema.
Expand Down
22 changes: 22 additions & 0 deletions tests/schema/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import logging

import codebasin.config as config
import codebasin.util as util


class TestSchema(unittest.TestCase):
"""
Expand Down Expand Up @@ -34,5 +36,25 @@ def test_configuration_file(self):
path = "./tests/schema/invalid_config.yaml"
config.load(path, "")

def test_cbiconfig_file(self):
"""schema/cbiconfig_file"""

path = "./tests/schema/cbiconfig.toml"
with open(path, "rb") as f:
toml = util._load_toml(f, "cbiconfig")
expected = {
"compiler": {
"test_one": {"options": ["TEST_ONE"]},
"test_two": {"options": ["TEST_TWO"]},
},
}
self.assertEqual(toml, expected)

path = "./tests/schema/invalid_cbiconfig.toml"
with open(path, "rb") as f:
with self.assertRaises(ValueError):
toml = util._load_toml(f, "cbiconfig")


if __name__ == '__main__':
unittest.main()

0 comments on commit 8b8105b

Please sign in to comment.