-
Notifications
You must be signed in to change notification settings - Fork 13
/
iceview_html.py
executable file
·76 lines (64 loc) · 2.62 KB
/
iceview_html.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
#! /usr/bin/python
import sys, os.path, re, getopt, subprocess
def usage():
sys.stderr.write("""\
Usage: iceview_html.py [-s BROWSER] INPUT.asc OUTPUT.html
Create a stand-alone .html file to display the floorplan / layout of an ICE40
design. Options:
-s BROWSER
Spawn BROWSER to display the generated OUTPUT.html file immediately.
""")
sys.exit(0)
try:
opts, args = getopt.getopt(sys.argv[1:], "s:")
except:
usage()
spawn_browser = None
for opt, arg in opts:
if opt == "-s":
spawn_browser = arg
else:
usage()
if len(args) != 2:
usage()
basedir = os.path.dirname(os.path.abspath(__file__))
asc_filename = args[0]
html_out_filename = args[1]
html_in_filename = basedir + "/" + "ice40_viewer.html"
# Copy the ice40_viewer.html file with appropriate replacements.
discard = False
with open(html_in_filename, "r") as f:
with open(html_out_filename, "w") as g:
for line in f:
line = line.replace("var standalone_mode = false",
"var standalone_mode = true")
# Inline referenced javascript files.
m = re.match(r'^ *<script type="text/javascript" src="([^"]+)"></script> *$', line)
if m:
if m.group(1) == "chipdbs.txt.js":
# The chipdb is huge, so don't inline it. And the format is
# not likely to change often.
line = (' <script type="text/javascript" src="file://' +
basedir + '/chipdbs.txt.js"></script>\n')
else:
# Other javascript files are not too big. So inline them
# so that we do not later pick up a new version that is
# incompatible with our old code in .html.
js = open(basedir + "/" + m.group(1)).read()
line = "<script>\n" + js + "\n</script>\n";
elif line == " <!-- Start skip in standalone mode -->\n":
discard = True
if not discard:
g.write(line)
if line == " <!-- End skip in standalone mode -->\n":
discard = False
if line == "// Standalone file data goes here.\n":
# Print out the embedded .asc contents.
g.write("g_fileData = \"\\\n")
with open(asc_filename, "r") as h:
for a in h:
g.write(a.replace("\n", "\\n\\\n").replace("\r", "\\r"))
g.write("\";\n")
if spawn_browser:
url = "file://" + os.path.abspath(html_out_filename)
subprocess.call([spawn_browser, url])