4 from io
import StringIO
6 deps_only
= sys
.argv
[1] == '--deps'
13 def open_tracked(name
, mode
='r'):
14 opened_files
.append(name
)
16 return open(name
, mode
)
17 except FileNotFoundError
:
22 output_file
= sys
.argv
[2]
24 os
.remove(output_file
)
25 except FileNotFoundError
:
27 temp_output_file
= output_file
+ '.tmp'
28 file_path
= os
.path
.abspath(__file__
)
29 openpower_path
= os
.path
.split(file_path
)[0]
30 wiki_path
= os
.path
.split(openpower_path
)[0]
33 def recursive_inline(f
, input_name
, depth
):
34 assert depth
< 10, "probably found an [[!inline]]-loop"
35 skip
= False # found the pattern <!-- hide -->
36 for line
in f
.readlines():
39 if line
.startswith("<!-- show -->"):
42 elif line
.startswith("<!-- hide -->"):
45 # fix separation in comparison table
46 if input_name
.endswith("comparison_table.tex") and \
47 line
.startswith("\begin{itemize}"):
49 o
.write("\\itemsep -0.3em\n")
51 # find headings and create name-refs, including filename
52 if line
.startswith("#"):
53 iname
= input_name
.split("/")
54 if iname
[0] == 'openpower': iname
.pop(0)
55 l
= line
.strip().split(" ")[1:3]
56 l
= map(lambda x
: ''.join(filter(str.isalnum
, x
)), l
)
58 l
= filter(lambda x
:x
, l
)
59 l
= list(dict.fromkeys(iname
+ list(l
)))
61 line
= '%s <a name="%s"> </>\n' % (line
.strip(), l
)
63 if not line
.startswith("[[!inline"):
67 # assume first thing is pagename
68 line
= line
.split('"')
70 print(f
"\tdepth={depth}: {fname}")
71 if fname
.endswith(".py"):
72 if fname
.startswith("gf_reference"):
74 wiki_path
+ "/../nmigen-gf/" + fname
) as inc
:
75 recursive_inline(inc
, fname
, depth
+ 1)
77 with
open_tracked(wiki_path
+ "/" + fname
) as inc
:
78 recursive_inline(inc
, fname
, depth
+ 1)
80 if fname
.endswith(".mdwn"):
81 with
open_tracked(wiki_path
+ "/" + fname
) as inc
:
82 recursive_inline(inc
, fname
, depth
+ 1)
83 elif fname
.startswith('openpower/isatables'):
84 pth
= wiki_path
+ "/../openpower-isa/" + fname
85 with
open_tracked(pth
) as inc
:
86 recursive_inline(inc
, fname
, depth
+ 1)
87 elif fname
.startswith('openpower/isa'):
88 pth
= wiki_path
+ "/../openpower-isa/" + fname
+ ".mdwn"
89 with
open_tracked(pth
) as inc
:
90 recursive_inline(inc
, fname
, depth
+ 1)
92 with
open_tracked(wiki_path
+ "/" + fname
+ ".mdwn") as inc
:
93 recursive_inline(inc
, fname
, depth
+ 1)
96 def body(o
, print=print):
97 with
open_tracked(sys
.argv
[1], "r") as f
:
98 recursive_inline(f
, sys
.argv
[1], 0)
101 with
StringIO() as o
:
102 body(o
, print=lambda *_
: None)
103 deps_file
= output_file
+ '.d'
104 with
open(deps_file
, "w") as o
:
105 deps
= " ".join(opened_files
)
106 o
.write(f
"{output_file} {deps_file}: {deps}\n")
108 with
open(temp_output_file
, "w") as o
:
110 os
.rename(temp_output_file
, output_file
)