Skip to content

Commit

Permalink
Merge pull request #4 from riscv/resolve-issue-#3
Browse files Browse the repository at this point in the history
Made logging optional in check_specs
  • Loading branch information
neelgala committed Aug 9, 2019
2 parents 560264f + 6b9e65a commit acd6701
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 48 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ script:
- pip install -r requirements.txt
- make latexpdf
- cd ../
- python setup.py sdist
- python -m twine upload --username $pypiusername --password $pypipassword dist/*
- if [ "$TRAVIS_BRANCH" = "master" ]; then python setup.py sdist; fi
- if [ "$TRAVIS_BRANCH" = "master" ]; then python -m twine upload --username $pypiusername --password $pypipassword dist/*; fi
before_deploy:
- git tag $TAGNAME
deploy:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.0.2 - 2019-08-09
### Changed
- Log is generated only if specified(for API calls to checker.check_specs).
### Fixed
- link in readme now points to github instead of gitlab.

## 1.0.0 - 2019-07-30
### Changed
- Work directory isnt deleted if the directory exists, although the files of the same name will be overwritten.
Expand Down
4 changes: 2 additions & 2 deletions docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Installation and Setup

.. code-block:: bash
git clone https://gitlab.com/incoresemi/riscv_config.git
git clone https://github.com/riscv/riscv-config.git
cd riscv_config
pip3 install -r requirements.txt
Expand Down Expand Up @@ -89,7 +89,7 @@ Example

.. code-block:: bash
git clone https://gitlab.com/incoresemi/riscv_config.git
git clone https://github.com/riscv/riscv-config.git
cd riscv_config/
Expand Down
2 changes: 1 addition & 1 deletion riscv_config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
__version__ = "1.0.0"
__version__ = "1.0.2"
73 changes: 34 additions & 39 deletions riscv_config/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

logger = logging.getLogger(__name__)


def iset():
'''Function to check and set defaults for all "implemented" fields which are dependent on
the xlen.'''
Expand Down Expand Up @@ -210,23 +209,7 @@ def imp_normalise(foo):
break
return foo


def errPrint(foo, space=' '):
'''
Function to petty print the error from cerberus.
'''
error = ''
for key in foo.keys():
error += space + str(key) + ":"
if isinstance(foo[key][0], dict):
error += "\n" + errPrint(foo[key][0], space + space)
else:
error += str(foo[key][0])
error += "\n"
return error.rstrip()


def check_specs(isa_spec, platform_spec, work_dir):
def check_specs(isa_spec, platform_spec, work_dir,logging=False):
'''
Function to perform ensure that the isa and platform specifications confirm
to their schemas. The :py:mod:`Cerberus` module is used to validate that the
Expand All @@ -235,20 +218,25 @@ def check_specs(isa_spec, platform_spec, work_dir):
:param isa_spec: The path to the DUT isa specification yaml file.
:param platform_spec: The path to the DUT platform specification yaml file.
:param logging: A boolean to indicate whether log is to be printed.
:type logging: bool
:type isa_spec: str
:type platform_spec: str
:raise ValidationError: It is raised when the specifications violate the
schema rules.
schema rules. It also contains the specific errors in each of the fields.
:return: A tuple with the first entry being the path to normalized isa file
and the second being path to the platform spec file.
:return: A tuple with the first entry being the absolute path to normalized isa file
and the second being the absolute path to the platform spec file.
'''
global inp_yaml

logger.info('Input-ISA file')
if logging:
logger.info('Input-ISA file')

foo = isa_spec
schema = constants.isa_schema
Expand All @@ -257,11 +245,13 @@ def check_specs(isa_spec, platform_spec, work_dir):
and constraints
"""
# Load input YAML file
logger.info('Loading input file: ' + str(foo))
if logging:
logger.info('Loading input file: ' + str(foo))
inp_yaml = utils.load_yaml(foo)

# instantiate validator
logger.info('Load Schema ' + str(schema))
if logging:
logger.info('Load Schema ' + str(schema))
schema_yaml = utils.load_yaml(schema)

#Extract xlen
Expand All @@ -274,28 +264,30 @@ def check_specs(isa_spec, platform_spec, work_dir):
normalized = validator.normalized(inp_yaml, schema_yaml)

# Perform Validation
logger.info('Initiating Validation')
if logging:
logger.info('Initiating Validation')
valid = validator.validate(inp_yaml)

# Print out errors
if valid:
logger.info('No Syntax errors in Input ISA Yaml. :)')
if logging:
logger.info('No Syntax errors in Input ISA Yaml. :)')
else:
error_list = validator.errors
logger.error("Error in " + foo + "\n" + errPrint(error_list))
raise ValidationError(
"Error in ISA Yaml. Refer to logs for more details.")
raise ValidationError("Error in " + foo + ".",error_list)

file_name = os.path.split(foo)
file_name_split = file_name[1].split('.')
output_filename = os.path.join(
work_dir, file_name_split[0] + '_checked.' + file_name_split[1])
ifile = output_filename
outfile = open(output_filename, 'w')
logger.info('Dumping out Normalized Checked YAML: ' + output_filename)
if logging:
logger.info('Dumping out Normalized Checked YAML: ' + output_filename)
yaml.dump(imp_normalise(normalized), outfile)

logger.info('Input-Platform file')
if logging:
logger.info('Input-Platform file')

foo = platform_spec
schema = constants.platform_schema
Expand All @@ -304,13 +296,15 @@ def check_specs(isa_spec, platform_spec, work_dir):
and constraints
"""
# Load input YAML file
logger.info('Loading input file: ' + str(foo))
if logging:
logger.info('Loading input file: ' + str(foo))
inp_yaml = utils.load_yaml(foo)
if inp_yaml is None:
inp_yaml = {'mtime': {'implemented': False}}

# instantiate validator
logger.info('Load Schema ' + str(schema))
if logging:
logger.info('Load Schema ' + str(schema))
schema_yaml = utils.load_yaml(schema)

validator = schemaValidator(schema_yaml, xlen=xlen)
Expand All @@ -319,24 +313,25 @@ def check_specs(isa_spec, platform_spec, work_dir):
normalized = validator.normalized(inp_yaml, schema_yaml)

# Perform Validation
logger.info('Initiating Validation')
if logging:
logger.info('Initiating Validation')
valid = validator.validate(inp_yaml)

# Print out errors
if valid:
logger.info('No Syntax errors in Input Platform Yaml. :)')
if logging:
logger.info('No Syntax errors in Input Platform Yaml. :)')
else:
error_list = validator.errors
logger.error("Error in " + foo + "\n" + errPrint(error_list))
raise ValidationError("Error in Platform\
Yaml. Refer to logs for more details.")
raise ValidationError("Error in " + foo + ".",error_list)

file_name = os.path.split(foo)
file_name_split = file_name[1].split('.')
output_filename = os.path.join(
work_dir, file_name_split[0] + '_checked.' + file_name_split[1])
pfile = output_filename
outfile = open(output_filename, 'w')
logger.info('Dumping out Normalized Checked YAML: ' + output_filename)
if logging:
logger.info('Dumping out Normalized Checked YAML: ' + output_filename)
yaml.dump(imp_normalise(normalized), outfile)
return (ifile, pfile)
20 changes: 19 additions & 1 deletion riscv_config/errors.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
class ValidationError(Exception):
pass
def __init__(self,message,errors):
super().__init__(message)
self.message=message
self.errors=errors
def __errPrint__(self,foo, space=' '):
'''
Function to petty print the error from cerberus.
'''
error = ''
for key in foo.keys():
error += space + str(key) + ":"
if isinstance(foo[key][0], dict):
error += "\n" + self.__errPrint__(foo[key][0], space + space)
else:
error += str(foo[key][0])
error += "\n"
return error.rstrip()
def __str__(self):
return self.message+"\n"+self.__errPrint__(self.errors)
5 changes: 2 additions & 3 deletions riscv_config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import riscv_config.utils as utils
from riscv_config.errors import ValidationError


def main():
'''
Entry point for riscv_config.
Expand All @@ -32,9 +31,9 @@ def main():

try:
checker.check_specs(os.path.abspath(args.isa_spec),
os.path.abspath(args.platform_spec), work_dir)
os.path.abspath(args.platform_spec), work_dir, True)
except ValidationError as msg:
logger.error(msg)
logger.error(str(msg))
return 1


Expand Down

0 comments on commit acd6701

Please sign in to comment.