From 792bbb116607fdcb4ba0880d816dc6e1b9368d10 Mon Sep 17 00:00:00 2001 From: Julian Hammer Date: Fri, 11 Oct 2019 16:13:58 +0200 Subject: [PATCH] removed some unnecessary file checks and fixed up test cases --- osaca/frontend.py | 26 +++++--------------------- osaca/semantics/__init__.py | 2 +- osaca/semantics/hw_model.py | 26 +++++--------------------- osaca/semantics/semantics_appender.py | 9 ++------- osaca/utils.py | 13 +++++++++++++ tests/test_frontend.py | 6 +++--- tests/test_semantics.py | 4 ++-- 7 files changed, 31 insertions(+), 55 deletions(-) diff --git a/osaca/frontend.py b/osaca/frontend.py index e7392e0..2cc3dd2 100755 --- a/osaca/frontend.py +++ b/osaca/frontend.py @@ -6,6 +6,7 @@ from datetime import datetime as dt from ruamel import yaml +from osaca import utils from osaca.semantics import INSTR_FLAGS, KernelDG, SemanticsAppender @@ -19,28 +20,11 @@ class Frontend(object): self._arch = arch if arch: self._arch = arch.lower() - try: - with open(self._find_file(self._arch), 'r') as f: - self._data = yaml.load(f, Loader=yaml.Loader) - except AssertionError: - raise ValueError( - 'Cannot find specified architecture. Make sure the machine file exists.' - ) + with open(utils.find_file(self._arch+'.yml'), 'r') as f: + self._data = yaml.load(f, Loader=yaml.Loader) elif path_to_yaml: - try: - assert os.path.exists(path_to_yaml) - with open(path_to_yaml, 'r') as f: - self._data = yaml.load(f, Loader=yaml.Loader) - except (AssertionError, FileNotFoundError): - raise ValueError( - 'Cannot find specified path to YAML file. Make sure the machine file exists.' - ) - - def _find_file(self, name): - data_dir = os.path.expanduser('~/.osaca/data') - name = os.path.join(data_dir, name + '.yml') - assert os.path.exists(name) - return name + with open(path_to_yaml, 'r') as f: + self._data = yaml.load(f, Loader=yaml.Loader) def _is_comment(self, instruction_form): return instruction_form['comment'] is not None and instruction_form['instruction'] is None diff --git a/osaca/semantics/__init__.py b/osaca/semantics/__init__.py index 39f8704..167cef1 100644 --- a/osaca/semantics/__init__.py +++ b/osaca/semantics/__init__.py @@ -6,6 +6,6 @@ Only the classes below will be exported, so please add new semantic tools to __a from .hw_model import MachineModel from .kernel_dg import KernelDG from .marker_utils import reduce_to_section -from .semanticsAppender import SemanticsAppender, INSTR_FLAGS +from .semantics_appender import SemanticsAppender, INSTR_FLAGS __all__ = ['MachineModel', 'KernelDG', 'reduce_to_section', 'SemanticsAppender', 'INSTR_FLAGS'] diff --git a/osaca/semantics/hw_model.py b/osaca/semantics/hw_model.py index bf71fd3..6191fbe 100755 --- a/osaca/semantics/hw_model.py +++ b/osaca/semantics/hw_model.py @@ -5,6 +5,7 @@ import re from ruamel import yaml +from osaca import utils from osaca.parser import ParserX86ATT @@ -18,28 +19,11 @@ class MachineModel(object): self._arch = arch if arch: self._arch = arch.lower() - try: - with open(self._find_file(self._arch), 'r') as f: - self._data = yaml.load(f, Loader=yaml.Loader) - except AssertionError: - raise ValueError( - 'Cannot find specified architecture. Make sure the machine file exists.' - ) + with open(utils.find_file(self._arch+'.yml'), 'r') as f: + self._data = yaml.load(f, Loader=yaml.Loader) elif path_to_yaml: - try: - assert os.path.exists(self._path) - with open(self._path, 'r') as f: - self._data = yaml.load(f, Loader=yaml.Loader) - except (AssertionError, FileNotFoundError): - raise ValueError( - 'Cannot find specified path to YAML file. Make sure the machine file exists.' - ) - - def _find_file(self, name): - data_dir = os.path.expanduser('~/.osaca/data') - name = os.path.join(data_dir, name + '.yml') - assert os.path.exists(name) - return name + with open(self._path, 'r') as f: + self._data = yaml.load(f, Loader=yaml.Loader) def __getitem__(self, key): """Return configuration entry.""" diff --git a/osaca/semantics/semantics_appender.py b/osaca/semantics/semantics_appender.py index dec13db..d0c1a44 100755 --- a/osaca/semantics/semantics_appender.py +++ b/osaca/semantics/semantics_appender.py @@ -4,6 +4,7 @@ import os import warnings from functools import reduce +from osaca import utils from osaca.parser import AttrDict, ParserAArch64v81, ParserX86ATT from osaca.semantics import MachineModel @@ -26,19 +27,13 @@ class SemanticsAppender(object): def __init__(self, machine_model: MachineModel, path_to_yaml=None): self._machine_model = machine_model self._isa = machine_model.get_ISA().lower() - path = self._find_file(self._isa) + path = utils.find_file('isa/'+self._isa+'.yml') self._isa_model = MachineModel(path_to_yaml=path) if self._isa == 'x86': self._parser = ParserX86ATT() elif self._isa == 'aarch64': self._parser = ParserAArch64v81() - def _find_file(self, isa): - data_dir = os.path.expanduser('~/.osaca/data/isa') - name = os.path.join(data_dir, isa + '.yml') - assert os.path.exists(name) - return name - # SUMMARY FUNCTION def add_semantics(self, kernel): for instruction_form in kernel: diff --git a/osaca/utils.py b/osaca/utils.py index e69de29..054be1d 100644 --- a/osaca/utils.py +++ b/osaca/utils.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 +import os.path + + +def find_file(name): + """Check for existence of name in user or package data folders and return path.""" + search_paths = [os.path.expanduser('~/.osaca/data'), + os.path.join(os.path.dirname(__file__), 'data')] + for dir in search_paths: + path = os.path.join(dir, name) + if os.path.exists(path): + return path + raise FileNotFoundError("Could not find {!r} in {!r}.".format(name, search_paths)) \ No newline at end of file diff --git a/tests/test_frontend.py b/tests/test_frontend.py index 564e7b9..5e03fdd 100755 --- a/tests/test_frontend.py +++ b/tests/test_frontend.py @@ -10,7 +10,7 @@ from osaca.frontend import Frontend from osaca.parser import ParserAArch64v81, ParserX86ATT from osaca.semantics.hw_model import MachineModel from osaca.semantics.kernel_dg import KernelDG -from osaca.semantics.semanticsAppender import SemanticsAppender +from osaca.semantics.semantics_appender import SemanticsAppender class TestFrontend(unittest.TestCase): @@ -60,9 +60,9 @@ class TestFrontend(unittest.TestCase): Frontend() with self.assertRaises(ValueError): Frontend(arch='csx', path_to_yaml=os.path.join(self.MODULE_DATA_DIR, 'csx.yml')) - with self.assertRaises(ValueError): + with self.assertRaises(FileNotFoundError): Frontend(path_to_yaml=os.path.join(self.MODULE_DATA_DIR, 'THE_MACHINE.yml')) - with self.assertRaises(ValueError): + with self.assertRaises(FileNotFoundError): Frontend(arch='THE_MACHINE') Frontend(arch='zen1') diff --git a/tests/test_semantics.py b/tests/test_semantics.py index 005088c..3148421 100755 --- a/tests/test_semantics.py +++ b/tests/test_semantics.py @@ -304,9 +304,9 @@ class TestSemanticTools(unittest.TestCase): MachineModel() with self.assertRaises(ValueError): MachineModel(arch='CSX', path_to_yaml=os.path.join(self.MODULE_DATA_DIR, 'csx.yml')) - with self.assertRaises(ValueError): + with self.assertRaises(FileNotFoundError): MachineModel(arch='THE_MACHINE') - with self.assertRaises(ValueError): + with self.assertRaises(FileNotFoundError): MachineModel(path_to_yaml=os.path.join(self.MODULE_DATA_DIR, 'THE_MACHINE.yml')) def test_MachineModel_getter(self):