removed some unnecessary file checks and fixed up test cases

This commit is contained in:
Julian Hammer
2019-10-11 16:13:58 +02:00
parent f08fbf79ba
commit 792bbb1166
7 changed files with 31 additions and 55 deletions

View File

@@ -6,6 +6,7 @@ from datetime import datetime as dt
from ruamel import yaml from ruamel import yaml
from osaca import utils
from osaca.semantics import INSTR_FLAGS, KernelDG, SemanticsAppender from osaca.semantics import INSTR_FLAGS, KernelDG, SemanticsAppender
@@ -19,28 +20,11 @@ class Frontend(object):
self._arch = arch self._arch = arch
if arch: if arch:
self._arch = arch.lower() self._arch = arch.lower()
try: with open(utils.find_file(self._arch+'.yml'), 'r') as f:
with open(self._find_file(self._arch), 'r') as f: self._data = yaml.load(f, Loader=yaml.Loader)
self._data = yaml.load(f, Loader=yaml.Loader)
except AssertionError:
raise ValueError(
'Cannot find specified architecture. Make sure the machine file exists.'
)
elif path_to_yaml: elif path_to_yaml:
try: with open(path_to_yaml, 'r') as f:
assert os.path.exists(path_to_yaml) self._data = yaml.load(f, Loader=yaml.Loader)
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
def _is_comment(self, instruction_form): def _is_comment(self, instruction_form):
return instruction_form['comment'] is not None and instruction_form['instruction'] is None return instruction_form['comment'] is not None and instruction_form['instruction'] is None

View File

@@ -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 .hw_model import MachineModel
from .kernel_dg import KernelDG from .kernel_dg import KernelDG
from .marker_utils import reduce_to_section 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'] __all__ = ['MachineModel', 'KernelDG', 'reduce_to_section', 'SemanticsAppender', 'INSTR_FLAGS']

View File

@@ -5,6 +5,7 @@ import re
from ruamel import yaml from ruamel import yaml
from osaca import utils
from osaca.parser import ParserX86ATT from osaca.parser import ParserX86ATT
@@ -18,28 +19,11 @@ class MachineModel(object):
self._arch = arch self._arch = arch
if arch: if arch:
self._arch = arch.lower() self._arch = arch.lower()
try: with open(utils.find_file(self._arch+'.yml'), 'r') as f:
with open(self._find_file(self._arch), 'r') as f: self._data = yaml.load(f, Loader=yaml.Loader)
self._data = yaml.load(f, Loader=yaml.Loader)
except AssertionError:
raise ValueError(
'Cannot find specified architecture. Make sure the machine file exists.'
)
elif path_to_yaml: elif path_to_yaml:
try: with open(self._path, 'r') as f:
assert os.path.exists(self._path) self._data = yaml.load(f, Loader=yaml.Loader)
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
def __getitem__(self, key): def __getitem__(self, key):
"""Return configuration entry.""" """Return configuration entry."""

View File

@@ -4,6 +4,7 @@ import os
import warnings import warnings
from functools import reduce from functools import reduce
from osaca import utils
from osaca.parser import AttrDict, ParserAArch64v81, ParserX86ATT from osaca.parser import AttrDict, ParserAArch64v81, ParserX86ATT
from osaca.semantics import MachineModel from osaca.semantics import MachineModel
@@ -26,19 +27,13 @@ class SemanticsAppender(object):
def __init__(self, machine_model: MachineModel, path_to_yaml=None): def __init__(self, machine_model: MachineModel, path_to_yaml=None):
self._machine_model = machine_model self._machine_model = machine_model
self._isa = machine_model.get_ISA().lower() 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) self._isa_model = MachineModel(path_to_yaml=path)
if self._isa == 'x86': if self._isa == 'x86':
self._parser = ParserX86ATT() self._parser = ParserX86ATT()
elif self._isa == 'aarch64': elif self._isa == 'aarch64':
self._parser = ParserAArch64v81() 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 # SUMMARY FUNCTION
def add_semantics(self, kernel): def add_semantics(self, kernel):
for instruction_form in kernel: for instruction_form in kernel:

View File

@@ -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))

View File

@@ -10,7 +10,7 @@ from osaca.frontend import Frontend
from osaca.parser import ParserAArch64v81, ParserX86ATT from osaca.parser import ParserAArch64v81, ParserX86ATT
from osaca.semantics.hw_model import MachineModel from osaca.semantics.hw_model import MachineModel
from osaca.semantics.kernel_dg import KernelDG from osaca.semantics.kernel_dg import KernelDG
from osaca.semantics.semanticsAppender import SemanticsAppender from osaca.semantics.semantics_appender import SemanticsAppender
class TestFrontend(unittest.TestCase): class TestFrontend(unittest.TestCase):
@@ -60,9 +60,9 @@ class TestFrontend(unittest.TestCase):
Frontend() Frontend()
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
Frontend(arch='csx', path_to_yaml=os.path.join(self.MODULE_DATA_DIR, 'csx.yml')) 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')) 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='THE_MACHINE')
Frontend(arch='zen1') Frontend(arch='zen1')

View File

@@ -304,9 +304,9 @@ class TestSemanticTools(unittest.TestCase):
MachineModel() MachineModel()
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
MachineModel(arch='CSX', path_to_yaml=os.path.join(self.MODULE_DATA_DIR, 'csx.yml')) 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') 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')) MachineModel(path_to_yaml=os.path.join(self.MODULE_DATA_DIR, 'THE_MACHINE.yml'))
def test_MachineModel_getter(self): def test_MachineModel_getter(self):