From 7fd00cbd49df2f47d7d3933f9cd215a652e55ea4 Mon Sep 17 00:00:00 2001 From: Jeremiah C Leary Date: Sun, 5 Nov 2017 20:08:00 -0600 Subject: [PATCH] Updates for main vsg program: 1) Added ability to load rules from local directory --- README.md | 8 ++++++++ bin/vsg | 10 ++++++++-- vsg/rule_list.py | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f8c88376..1f54284c 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,14 @@ You can run all the tests at the top level by invoking the following command: python setup.py test +## Local rules + +VSG supports customization by allowing localized rules. +This is simply a directory with an __init__.py file and one or more python files. +The files should follow the same structure and naming convention as the rules found in the vsg/rules directory. + +The localized rules will be used when the -l command line argument is given. + ## Phases The rules will be grouped together and ran in phases. diff --git a/bin/vsg b/bin/vsg index e13ebd03..c14d3b18 100755 --- a/bin/vsg +++ b/bin/vsg @@ -19,7 +19,8 @@ def parse_command_line_arguments(): description='Analyzes VHDL files\ for style guide violations.') - parser.add_argument('-f', '--filename', required=True) + parser.add_argument('-f', '--filename', required=True, help = 'File to analyze') + parser.add_argument('-l', '--local_rules', help = 'Path to local rules') if len(sys.argv) == 1: parser.print_help() @@ -32,7 +33,12 @@ def main(): '''Main routine of the VHDL Style Guide (VSG) program.''' commandLineArguments = parse_command_line_arguments() - oRules = rule_list.list(vhdlFile.vhdlFile(commandLineArguments.filename)) + + ### Add local rule path to system path so the rules can be loaded + if commandLineArguments.local_rules: + sys.path.append(os.path.abspath(commandLineArguments.local_rules)) + + oRules = rule_list.list(vhdlFile.vhdlFile(commandLineArguments.filename), commandLineArguments.local_rules) oRules.check_rules() oRules.report_violations() diff --git a/vsg/rule_list.py b/vsg/rule_list.py index 289515bf..2a432970 100644 --- a/vsg/rule_list.py +++ b/vsg/rule_list.py @@ -5,6 +5,34 @@ import re import importlib import inspect + +def get_python_modules_from_directory(sDirectoryName, lModules): + + lDirectoryContents = os.listdir(sDirectoryName) + for sFileName in lDirectoryContents: + if '.py' in sFileName and not '.pyc' in sFileName and not sFileName.startswith('__'): + lModules.append(sFileName.replace('.py','')) + + +def get_rules_from_module(lModules, lRules): + + for sModuleName in lModules: + for name, obj in inspect.getmembers(importlib.import_module(sModuleName)): + if name.startswith('rule_'): + lRules.append(obj()) + + +def load_local_rules(sDirectoryName): + '''Loads rules from the directory passed to this routine.''' + + lLocalModules = [] + get_python_modules_from_directory(sDirectoryName, lLocalModules) + + lRules = [] + get_rules_from_module(lLocalModules, lRules) + return lRules + + def load_base_rules(): pysearchre = re.compile('.py$', re.IGNORECASE) rulefiles = filter(pysearchre.search, os.listdir(os.path.join(os.path.dirname(__file__),'rules'))) @@ -37,8 +65,10 @@ def maximum_phase(lRules): class list(): ''' Contains a list of all rules to be checked. It also contains methods to check the rules.''' - def __init__(self, oVhdlFile): + def __init__(self, oVhdlFile, sLocalRulesDirectory=None): self.rules = load_base_rules() + if sLocalRulesDirectory: + self.rules.extend(load_local_rules(sLocalRulesDirectory)) self.iNumberRulesRan = 0 self.lastPhaseRan = 0 self.oVhdlFile = oVhdlFile @@ -49,7 +79,6 @@ class list(): iFailures = 0 for phase in range(1,10): iPhaseRuleCount = 0 -# print ('Running Phase ' + str(phase) + '...') for oRule in self.rules: if oRule.phase == phase: oRule.analyze(self.oVhdlFile) @@ -77,5 +106,5 @@ class list(): print ('=' * len(sFileTitle)) print ('Total Rules Checked: ' + str(self.iNumberRulesRan)) - print ('Total Rules Failed: ' + str(iFailures)) + print ('Total Failures: ' + str(iFailures))