forked from TinyTapeout/tt02-submission-template
-
Notifications
You must be signed in to change notification settings - Fork 55
/
configure.py
executable file
·205 lines (165 loc) · 6.91 KB
/
configure.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
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python3
import requests
import argparse
import os
import yaml
import logging
import sys
import csv
import re
import subprocess
def load_yaml(yaml_file):
with open(yaml_file, "r") as stream:
return (yaml.safe_load(stream))
def write_user_config(module_name, sources):
filename = 'user_config.tcl'
with open(os.path.join('src', filename), 'w') as fh:
fh.write("set ::env(DESIGN_NAME) {}\n".format(module_name))
fh.write('set ::env(VERILOG_FILES) "\\\n')
for line, source in enumerate(sources):
fh.write(" $::env(DESIGN_DIR)/" + source)
if line != len(sources) - 1:
fh.write(' \\\n')
fh.write('"\n')
def fetch_file(url, filename):
logging.info("trying to download {}".format(url))
r = requests.get(url)
if r.status_code != 200:
logging.warning("couldn't download {}".format(url))
exit(1)
with open(filename, 'wb') as fh:
logging.info("written to {}".format(filename))
fh.write(r.content)
def get_project_source(yaml):
# wokwi_id must be an int or 0
try:
wokwi_id = int(yaml['project']['wokwi_id'])
except ValueError:
logging.error("wokwi id must be an integer")
exit(1)
# it's a wokwi project
if wokwi_id != 0:
url = "https://wokwi.com/api/projects/{}/verilog".format(wokwi_id)
src_file = "user_module_{}.v".format(wokwi_id)
fetch_file(url, os.path.join("src", src_file))
# also fetch the wokwi diagram
url = "https://wokwi.com/api/projects/{}/diagram.json".format(wokwi_id)
diagram_file = "wokwi_diagram.json"
fetch_file(url, os.path.join("src", diagram_file))
return [src_file, 'cells.v']
# else it's HDL, so check source files
else:
if 'source_files' not in yaml['project']:
logging.error("source files must be provided if wokwi_id is set to 0")
exit(1)
source_files = yaml['project']['source_files']
if source_files is None:
logging.error("must be more than 1 source file")
exit(1)
if len(source_files) == 0:
logging.error("must be more than 1 source file")
exit(1)
if 'top_module' not in yaml['project']:
logging.error("must provide a top module name")
exit(1)
for filename in source_files:
if not os.path.exists(os.path.join('src', filename)):
logging.error(f"{filename} doesn't exist in the repo")
exit(1)
return source_files
# documentation
def check_docs(yaml):
for key in ['author', 'title', 'description', 'how_it_works', 'how_to_test', 'language']:
if key not in yaml['documentation']:
logging.error("missing key {} in documentation".format(key))
exit(1)
if yaml['documentation'][key] == "":
logging.error("missing value for {} in documentation".format(key))
exit(1)
# if provided, check discord handle is valid
if len(yaml['documentation']['discord']):
parts = yaml['documentation']['discord'].split('#')
if len(parts) != 2 or len(parts[0]) == 0 or not re.match('^[0-9]{4}$', parts[1]):
logging.error('Invalid format for discord username')
exit(1)
def build_pdf(yaml_data):
with open(".github/workflows/doc_header.md") as fh:
doc_header = fh.read()
with open(".github/workflows/doc_preview.md") as fh:
doc_template = fh.read()
with open('datasheet.md', 'w') as fh:
fh.write(doc_header)
# handle pictures
yaml_data['picture_link'] = ''
if yaml_data['picture']:
# skip SVG for now, not supported by pandoc
picture_name = yaml_data['picture']
if 'svg' not in picture_name:
yaml_data['picture_link'] = '![picture]({})'.format(picture_name)
else:
logging.warning("svg not supported")
# now build the doc & print it
try:
doc = doc_template.format(**yaml_data)
fh.write(doc)
fh.write("\n\pagebreak\n")
except IndexError:
logging.warning("missing pins in info.yaml, skipping")
pdf_cmd = 'pandoc --pdf-engine=xelatex -i datasheet.md -o datasheet.pdf'
logging.info(pdf_cmd)
p = subprocess.run(pdf_cmd, shell=True)
if p.returncode != 0:
logging.error("pdf command failed")
def get_top_module(yaml):
wokwi_id = int(yaml['project']['wokwi_id'])
if wokwi_id != 0:
return "user_module_{}".format(wokwi_id)
else:
return yaml['project']['top_module']
def get_stats():
with open('runs/wokwi/reports/metrics.csv') as f:
report = list(csv.DictReader(f))[0]
print('# Routing stats')
print()
print('| Utilisation | Wire length (um) |')
print('|-------------|------------------|')
print('| {} | {} |'.format(report['OpenDP_Util'], report['wire_length']))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="TT setup")
parser.add_argument('--check-docs', help="check the documentation part of the yaml", action="store_const", const=True)
parser.add_argument('--build-pdf', help="build a single page PDF", action="store_const", const=True)
parser.add_argument('--get-stats', help="print some stats from the run", action="store_const", const=True)
parser.add_argument('--create-user-config', help="create the user_config.tcl file with top module and source files", action="store_const", const=True)
parser.add_argument('--debug', help="debug logging", action="store_const", dest="loglevel", const=logging.DEBUG, default=logging.INFO)
parser.add_argument('--yaml', help="yaml file to load", default='info.yaml')
args = parser.parse_args()
# setup log
log_format = logging.Formatter('%(asctime)s - %(module)-10s - %(levelname)-8s - %(message)s')
# configure the client logging
log = logging.getLogger('')
# has to be set to debug as is the root logger
log.setLevel(args.loglevel)
# create console handler and set level to info
ch = logging.StreamHandler(sys.stdout)
# create formatter for console
ch.setFormatter(log_format)
log.addHandler(ch)
if args.get_stats:
get_stats()
elif args.check_docs:
logging.info("checking docs")
config = load_yaml(args.yaml)
check_docs(config)
elif args.build_pdf:
logging.info("building pdf")
config = load_yaml(args.yaml)
build_pdf(config['documentation'])
elif args.create_user_config:
logging.info("creating include file")
config = load_yaml(args.yaml)
source_files = get_project_source(config)
top_module = get_top_module(config)
if top_module == 'top':
logging.error("top module cannot be called top - prepend your repo name to make it unique")
exit(1)
write_user_config(top_module, source_files)