-
Notifications
You must be signed in to change notification settings - Fork 1
/
dodo.py
195 lines (165 loc) · 6.42 KB
/
dodo.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import tk205
import subprocess
from doit.tools import create_folder
BUILD_PATH = "build"
SCHEMA_SOURCE_PATH = os.path.join("schema-205","schema-source")
JSON_SCHEMA_PATH = os.path.join("schema-205","build","schema")
EXAMPLES_SOURCE_PATH = os.path.join("schema-205","examples")
EXAMPLES_OUTPUT_PATH = os.path.join(BUILD_PATH,"examples")
TEMPLATE_OUTPUT_PATH = os.path.join(BUILD_PATH,"templates")
TEMPLATE_CONFIG = os.path.join('config','templates.json')
LIB_BUILD_PATH = BUILD_PATH
TK205_SOURCE_PATH = 'tk205'
SCHEMA205_SOURCE_PATH = os.path.join("schema-205","schema205")
DOIT_CONFIG = {'default_tasks': ['build_schema', 'cbor', 'yaml', 'xlsx', 'json', 'templates', 'test', 'web']}
def task_build_schema():
'''Build the schema'''
return {
'actions': ['doit -d schema-205']
}
def collect_schema_files():
file_list = []
for file_name in sorted(os.listdir(SCHEMA_SOURCE_PATH)):
if '.schema.yaml' in file_name:
file_name_root = os.path.splitext(os.path.splitext(file_name)[0])[0]
file_list.append(os.path.join(JSON_SCHEMA_PATH,f'{file_name_root}.schema.json'))
return file_list
def collect_examples(example_dir):
file_paths = []
for example in sorted(os.listdir(example_dir)):
example_path = os.path.join(example_dir,example)
if os.path.isdir(example_path):
file_paths += collect_examples(example_path)
else:
file_paths.append(os.path.join(example_dir,example))
return file_paths
def collect_generated_examples(examples_source_dir, examples_output_dir, extension):
file_paths = []
for example in sorted(os.listdir(examples_source_dir)):
example_source_path = os.path.join(examples_source_dir,example)
example_output_path = os.path.join(examples_output_dir,example)
if os.path.isdir(example_source_path):
file_paths += collect_generated_examples(example_source_path, example_output_path, extension)
else:
base_name = os.path.basename(example)
file_name = os.path.splitext(base_name)[0]
file_paths.append(os.path.join(examples_output_dir,f'{file_name}.{extension}'))
return file_paths
def collect_template_outputs(template_config):
file_paths = []
for rs, templates in template_config.items():
for t in templates:
file_name_components = [rs]
if t["file-name-suffix"]:
file_name_components.append(t["file-name-suffix"])
file_name_components.append("template.a205.xlsx")
file_name = '-'.join(file_name_components)
file_paths.append(os.path.join(TEMPLATE_OUTPUT_PATH,file_name))
return file_paths
source_examples = collect_examples(EXAMPLES_SOURCE_PATH)
CBOR_OUTPUT_PATH = os.path.join(EXAMPLES_OUTPUT_PATH,'cbor')
cbor_examples = collect_generated_examples(EXAMPLES_SOURCE_PATH, CBOR_OUTPUT_PATH, 'cbor')
def task_translate_to_cbor():
'''Translate example files to CBOR'''
return {
'basename': 'cbor',
'file_dep': source_examples,
'targets': cbor_examples,
'actions': [
(create_folder, [CBOR_OUTPUT_PATH]),
(tk205.translate_directory, [EXAMPLES_SOURCE_PATH, CBOR_OUTPUT_PATH])
],
'clean': True
}
YAML_OUTPUT_PATH = os.path.join(EXAMPLES_OUTPUT_PATH,'yaml')
yaml_examples = collect_generated_examples(EXAMPLES_SOURCE_PATH, YAML_OUTPUT_PATH, 'yaml')
def task_translate_to_yaml():
'''Translate example files to YAML'''
return {
'basename': 'yaml',
'file_dep': source_examples,
'targets': yaml_examples,
'actions': [
(create_folder, [YAML_OUTPUT_PATH]),
(tk205.translate_directory, [EXAMPLES_SOURCE_PATH, YAML_OUTPUT_PATH])
],
'clean': True
}
XLSX_OUTPUT_PATH = os.path.join(EXAMPLES_OUTPUT_PATH,'xlsx')
xlsx_examples = collect_generated_examples(EXAMPLES_SOURCE_PATH, XLSX_OUTPUT_PATH, 'xlsx')
def task_translate_to_xlsx():
'''Translate example files to XLSX'''
return {
'basename': 'xlsx',
'file_dep': source_examples + [os.path.join(TK205_SOURCE_PATH, 'xlsx.py')],
'targets': xlsx_examples,
'actions': [
(create_folder, [XLSX_OUTPUT_PATH]),
(tk205.translate_directory, [EXAMPLES_SOURCE_PATH, XLSX_OUTPUT_PATH])
],
'clean': True
}
JSON_OUTPUT_PATH = os.path.join(EXAMPLES_OUTPUT_PATH,'json')
json_examples = collect_generated_examples(EXAMPLES_SOURCE_PATH, JSON_OUTPUT_PATH, 'json')
def task_translate_to_json():
'''Translate XLSX example files back to JSON'''
return {
'basename': 'json',
'file_dep': xlsx_examples + [os.path.join(TK205_SOURCE_PATH, 'xlsx.py')],
'targets': json_examples,
'task_dep': ['xlsx'],
'actions': [
(create_folder, [JSON_OUTPUT_PATH]),
(tk205.translate_directory, [XLSX_OUTPUT_PATH, JSON_OUTPUT_PATH])
],
'clean': True
}
template_config = tk205.load(TEMPLATE_CONFIG)
template_files = collect_template_outputs(template_config)
def task_templates():
'''Create XLSX templates based on the schema'''
return {
'task_dep': ['build_schema'],
'file_dep': [TEMPLATE_CONFIG] + collect_schema_files() + [os.path.join(TK205_SOURCE_PATH, 'xlsx.py')],
'targets': template_files,
'actions': [
(create_folder, [TEMPLATE_OUTPUT_PATH]),
(tk205.generate_templates, [TEMPLATE_OUTPUT_PATH, template_config])
],
'clean': True
}
def task_test():
'''Performs unit tests and example file validation tests'''
return {
'task_dep': ['build_schema','cbor','yaml','xlsx','json'],
'file_dep': cbor_examples + yaml_examples + xlsx_examples + json_examples,
'actions': [
'pytest -v test',
]
}
def task_web():
'''Generates the web contents for data.ashrae.org'''
return {
'task_dep': ['build_schema','cbor','yaml','xlsx','json', 'templates'],
'file_dep': cbor_examples + yaml_examples + xlsx_examples + json_examples + template_files, # Add markdown content
'actions': ['python web/web-content.py']
}
def task_libtk205():
'''Build libtk205'''
def configure_build(PAT):
subprocess.run(f'cmake -B {LIB_BUILD_PATH} -DPA_TOKEN={PAT} -DBUILD_LIBTK205=ON -Dlibtk205_BUILD_TESTING=ON', cwd='.', shell=True, check=True)
subprocess.run(f'cmake --build {LIB_BUILD_PATH} --config Release', cwd='.', shell=True, check=True)
def run_tests(build_path: str):
subprocess.run('ctest', cwd=build_path, shell=True, check=True)
return {
'params':[{'name':'PAT',
'long': 'PAT',
'default': ''}],
'actions': [
(create_folder, [LIB_BUILD_PATH]),
(configure_build, ),
(run_tests, [f'{LIB_BUILD_PATH}/libtk205']),
],
'clean': ['doit -d schema-205 clean cpp'],
}