Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify schema handling and fix several bugs #35

Merged
merged 10 commits into from
Jan 26, 2024
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +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
129 changes: 3 additions & 126 deletions codebasin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,128 +8,17 @@
import collections
import glob
import itertools as it
import json
import logging
import os
import re
import shlex

import jsonschema
import yaml

from codebasin import util

log = logging.getLogger("codebasin")

_compiledb_schema_id = (
"https://raw.githubusercontent.com/intel/"
"code-base-investigator/schema/compilation-database.schema"
)
_compiledb_schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": _compiledb_schema_id,
"title": "Compilation Database",
"description": "Compilation database format used by many projects.",
"type": "array",
"items": {
"type": "object",
"properties": {
"directory": {"type": "string"},
"arguments": {"type": "array", "items": {"type": "string"}},
"file": {"type": "string"},
"command": {"type": "string"},
"output": {"type": "string"},
},
"anyOf": [
{
"required": [
"arguments",
],
},
{
"required": [
"command",
],
},
],
},
}

_config_schema_id = (
"https://raw.githubusercontent.com/intel/"
"code-base-investigator/schema/config.schema"
)

_config_schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": _config_schema_id,
"title": "Code Base Investigator Configuration File",
"description": "Lists codebase files and compilation options",
"type": "object",
"properties": {
"codebase": {
"type": "object",
"properties": {
"files": {"type": "array", "items": {"type": "string"}},
"platforms": {"type": "array", "items": {"type": "string"}},
"exclude_files": {
"type": "array",
"items": {"type": "string"},
},
},
"required": ["files", "platforms"],
},
},
"patternProperties": {
".*": {
"type": "object",
"properties": {
"files": {"type": "array", "items": {"type": "string"}},
"defines": {"type": "array", "items": {"type": "string"}},
"include_paths": {
"type": "array",
"items": {"type": "string"},
},
"commands": {"type": "string"},
},
"anyOf": [{"required": ["files"]}, {"required": ["commands"]}],
},
},
"additionalProperties": False,
"required": ["codebase"],
}

_importcfg_schema_id = (
"https://raw.githubusercontent.com/intel/",
"code-base-investigator/schema/importcfg.schema",
)

_importcfg_schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": _importcfg_schema_id,
"title": "Code Base Investigator Import Configuration File",
"description": "Configuration options for importing commands.",
"type": "object",
"properties": {
"compilers": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
},
"options": {"type": "array", "items": {"type": "string"}},
},
"required": ["name", "options"],
"additionalProperties": False,
},
},
},
"required": ["compilers"],
"additionalProperties": False,
}


def extract_defines(args):
"""
Expand Down Expand Up @@ -292,7 +181,7 @@ def load_importcfg():
log.info(f"Found import configuration file at {path}")
with open(path) as f:
try:
_importcfg_json = json.load(f)
_importcfg_json = util._load_json(f, "importcfg")
for compiler in _importcfg_json["compilers"]:
_importcfg[compiler["name"]] = compiler["options"]
except BaseException:
Expand Down Expand Up @@ -513,14 +402,7 @@ def load_database(dbpath, rootdir):
represented as a compilation database entry.
"""
with util.safe_open_read_nofollow(dbpath, "r") as fi:
db = json.load(fi)

# Validate compilation database against schema
try:
jsonschema.validate(instance=db, schema=_compiledb_schema)
except Exception:
msg = "Compilation database failed schema validation"
raise ValueError(msg)
db = util._load_json(fi, schema_name="compiledb")

configuration = []
for e in db:
Expand Down Expand Up @@ -689,12 +571,7 @@ def load(config_file, rootdir):
raise RuntimeError(f"Could not open {config_file!s}.")

# Validate config against a schema
# We don't use any advanced features of YAML, so can use JSON here
try:
jsonschema.validate(instance=config, schema=_config_schema)
except Exception:
msg = "Configuration file failed schema validation"
raise ValueError(msg)
util._validate_yaml(config, schema_name="config")

# Read codebase definition
if "codebase" in config:
Expand Down
10 changes: 5 additions & 5 deletions codebasin/schema/compilation-database.schema
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/intel/code-base-investigator/schema/compilation-database.schema",
"$id": "https://raw.githubusercontent.com/intel/code-base-investigator/main/codebasin/schema/compilation-database.schema",
"title": "Compilation Database",
"description": "Compilation database format used by many projects.",
"type": "array",
Expand All @@ -24,17 +24,17 @@
},
"output": {
"type": "string"
},
}
laserkelvin marked this conversation as resolved.
Show resolved Hide resolved
},
"anyOf": [
{
"required": [
"arguments",
],
"arguments"
]
},
{
"required": [
"command",
"command"
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion codebasin/schema/config.schema
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/intel/code-base-investigator/schema/config.schema",
"$id": "https://raw.githubusercontent.com/intel/code-base-investigator/main/codebasin/schema/config.schema",
"title": "Code Base Investigator Configuration File",
"description": "Lists codebase files and compilation options",
"type": "object",
Expand Down
37 changes: 37 additions & 0 deletions codebasin/schema/coverage-0.1.0.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/intel/p3-analysis-library/main/p3/data/coverage-0.1.0.schema",
"title": "Coverage",
"description": "Lines of code used in each file of a code base.",
"type": "array",
"items": {
"type": "object",
"properties": {
"file": {
"type": "string"
},
"regions": {
"type": "array",
"items": {
"type": "array",
"prefixItems": [
{
"type": "integer"
},
{
"type": "integer"
},
{
"type": "integer"
}
],
"items": false
}
}
},
"required": [
"file",
"regions"
]
}
}
6 changes: 3 additions & 3 deletions codebasin/schema/importcfg.schema
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/intel/code-base-investigator/schema/importcfg.schema",
"$id": "https://raw.githubusercontent.com/intel/code-base-investigator/main/codebasin/schema/importcfg.schema",
"title": "Code Base Investigator Import Configuration File",
"description": "Configuration options for importing commands.",
"type": "object",
Expand All @@ -11,7 +11,7 @@
"type": "object",
"properties": {
"name": {
"type": "string",
"type": "string"
},
"options": {
"type": "array",
Expand All @@ -21,7 +21,7 @@
}
},
"required": ["name", "options"],
"additionalProperties": false,
"additionalProperties": false
}
}
},
Expand Down
Loading
Loading