-
Notifications
You must be signed in to change notification settings - Fork 1
/
exceptions.py
142 lines (90 loc) · 4.08 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import json
from combination_validator import CombinationValidator
import jsonschema
from globals import ExceptionConfig, GlobalsConfig, ComparConfig, CombinatorConfig
class FileError(Exception):
pass
class CompilationError(Exception):
pass
class ExecutionError(Exception):
pass
class UserInputError(Exception):
pass
class FolderError(Exception):
pass
class DatabaseError(Exception):
pass
class CombinationFailure(Exception):
pass
class MissingDataError(Exception):
pass
class MakefileError(Exception):
pass
class FragmentError(Exception):
pass
class DeadCodeLoop(Exception):
pass
class NoOptimalCombinationError(Exception):
pass
class DeadCodeFile(Exception):
pass
def assert_file_exist(file_path: str):
if not os.path.exists(file_path):
raise FileError(f'File {file_path} dose not exist')
def assert_file_from_format(file_path: str, _format: str):
if not os.path.basename(file_path).split('.')[1].endswith(_format):
raise FileError(f'File {file_path} should be in {_format} format')
def assert_file_is_empty(file: str):
if not file:
raise FileError(f'File {file} is empty')
def assert_only_files(folder_path: str):
folder_content = os.listdir(folder_path)
if len(folder_content) != len(list(filter(os.path.isfile,
[os.path.join(folder_path, file) for file in folder_content]))):
raise UserInputError('Input dir must contain only files!')
def assert_rel_path_starts_without_sep(path: str):
if path.startswith(os.sep):
raise UserInputError('Relative path should not start with separator!')
def assert_forbidden_characters(path: str):
forbidden_characters = ["{", "}"]
for char in forbidden_characters:
if char in path:
raise UserInputError(f'Path cannot contain any char from: {forbidden_characters}')
def assert_test_file_name(test_file_name: str):
if test_file_name != CombinationValidator.UNIT_TEST_FILE_NAME:
raise UserInputError(f'Unit test file must be named as: {CombinationValidator.UNIT_TEST_FILE_NAME}!')
def assert_test_file_function_name(test_file_path: str):
if not CombinationValidator.check_if_test_exists(test_file_path):
raise UserInputError(f'Unit test file must contain test named: "{CombinationValidator.UNIT_TEST_NAME}"!')
def assert_original_files_folder_exists(working_directory: str):
original_files_path = os.path.join(working_directory, ComparConfig.ORIGINAL_FILES_FOLDER_NAME)
if not os.path.exists(original_files_path):
raise UserInputError(f'Original files folder from the last Compar operation must be exist in'
f' {working_directory}')
def assert_folder_exist(folder_path: str):
if not os.path.exists(folder_path):
raise FolderError(f'Folder {folder_path} dose not exist')
def assert_allowed_directive_type(directive_type: str):
allowed_types = (CombinatorConfig.PARALLEL_DIRECTIVE_PREFIX, CombinatorConfig.FOR_DIRECTIVE_PREFIX)
if directive_type not in allowed_types:
raise UserInputError(f'omp directives prefix of {directive_type} is incorrect!')
def assert_user_json_structure():
schema_file_path = os.path.join(GlobalsConfig.ASSETS_DIR_PATH, ExceptionConfig.PARAMETERS_SCHEMA_FILE_NAME)
with open(schema_file_path, 'r') as fp:
json_schema = json.load(fp)
args_for_validation_func = (
(json_schema['compilation'], CombinatorConfig.COMPILATION_PARAMS_FILE_PATH),
(json_schema['omp_rtl'], CombinatorConfig.OMP_RTL_PARAMS_FILE_PATH),
(json_schema['omp_directives'], CombinatorConfig.OMP_DIRECTIVES_FILE_PATH)
)
for args in args_for_validation_func:
assert_params_json_is_valid(*args)
def assert_params_json_is_valid(json_schema: dict, json_path: str):
assert_file_exist(json_path)
with open(json_path, 'r') as fp:
params_json = json.load(fp)
try:
jsonschema.validate(params_json, json_schema)
except jsonschema.exceptions.ValidationError:
raise UserInputError(f'{json_path} must conform to scheme!')