1515from os import listdir
1616from os .path import isfile , join , abspath
1717
18- TABLE_TEMPLATE = \
19- r"""
20- <table>
21- <tr>
22- {%- for item in items %}
23- <td align="{% if align is defined %}{{align}}{% else %}center{% endif %}">{% if item.url %}<a href="{{item.url}}">{% endif %}{{item.name}}{% if item.url %}</a>{% endif %}</td>{%- if loop.index % width == 0 %}
24- </tr><tr>{%- endif -%}
25- {%- endfor %}
26- </tr>
27- </table>
28-
29- """
30-
3118repopath = lambda path : Path (__file__ ).parents [2 ] / path
3219
3320def run (where , command , stdin = None ):
@@ -84,8 +71,18 @@ def extract(text, key):
8471 return re .search (r"<!--{0}-->(.*?)<!--/{0}-->" .format (key ), text , flags = re .DOTALL | re .MULTILINE ).group (1 )
8572
8673def format_table (items , width , align = None ):
87- subs = {"items" : items , "width" : width }
88- if align : subs ["align" ] = align ;
74+ subs = {"items" : items , "width" : width ,
75+ "align" : "center" if align is None else align }
76+ TABLE_TEMPLATE = r"""
77+ <table>
78+ {% for bitems in items | batch(width) %}<tr>
79+ {%- for item in bitems %}
80+ <td align="{{ align }}">{% if item.url %}<a href="{{item.url}}">{% endif %}{{item.name}}{% if item.url %}</a>{% endif %}</td>
81+ {%- endfor %}
82+ </tr>{%- endfor %}
83+ </table>
84+
85+ """
8986 return Environment ().from_string (TABLE_TEMPLATE ).render (subs )
9087
9188def get_lbuild (root , target = None ):
@@ -97,90 +94,91 @@ def get_lbuild(root, target=None):
9794 return builder .parser
9895
9996
100- # All the paths
101- root = repopath ("." )
102- board_path = root / "src/modm/board"
103- example_path = root / "examples"
104- ignored_path = root / "test/all/ignored.txt"
105- examples_readme_in_path = root / "examples/README.md"
106- examples_readme_path = root / "docs/src/guide/examples.md"
107- readme_path = root / "README.md"
108- index_in_path = root / "docs/index.md.in"
109- index_path = root / "docs/src/index.md"
110- whoweare_in_path = root / "docs/who-we-are.md.in"
111- whoweare_path = root / "docs/src/who-we-are.md"
112- modules_in_path = root / "docs/modules.md.in"
113- modules_path = root / "docs/src/reference/modules.md"
114-
115- # We cannot use lbuild to enumerate the boards since they only make themselves available for certain devices
116- boards = [re .search (r"<module>modm:board:(.*?)</module>" , config .read_text ()).group (1 )
117- for config in Path (repopath ("src/modm/board" )).glob ("*/board.xml" )]
118- boards = [{"name" : name (b ), "url" : board_url (b )} for b in boards ]
119- boards .sort (key = lambda b : b ["name" ])
120- bsp_table = format_table (boards , 4 )
121-
122- # Get all the example directory paths
123- examples = [e .relative_to (example_path ) for e in example_path .glob ('**/project.xml' )]
124- examples = [{"name" : "{}: {}" .format (name (str (e .parts [0 ])), e .parent .relative_to (e .parts [0 ])),
125- "url" : github_url (Path ("examples" ) / e .parent / "main.cpp" )} for e in examples ]
126- examples .sort (key = lambda b : b ["name" ])
127- example_table = format_table (examples , 2 , "left" )
128-
129- # Get all supported targets
130- targets = set (get_lbuild (root ).find_option ("modm:target" ).values )
131- ignored_devices = set (d for d in ignored_path .read_text ().strip ().splitlines () if "#" not in d )
132- targets -= ignored_devices
133- avr_count = len ([t for t in targets if t .startswith ("at" )])
134- stm_count = len ([t for t in targets if t .startswith ("stm32" )])
135- sam_count = len ([t for t in targets if t .startswith ("sam" )])
136- all_count = avr_count + stm_count + sam_count
137-
138- # get the author count
139- from authors import author_handles
140- author_count = len (author_handles )
141-
142- # Get all the modules that are available for the STM32
143- # Get all drivers, we assume they are available for all devices
144- drivers = (get_lbuild (root , t ).modules for t in {"avr" , "stm32" , "hosted" })
145- drivers = {m for mg in drivers for m in mg if m .startswith ("modm:driver:" )}
146- drivers = sorted (m .replace ("modm:driver:" , "" ) for m in drivers )
147- drivers = [{"name" : name (d ), "url" : driver_url (d )} for d in drivers if name (d )]
148- driver_table = format_table (drivers , 6 )
149-
150- # Read the repo README.md and replace these keys
151- readme = readme_path .read_text ()
152- readme = replace (readme , "authorcount" , author_count - 7 )
153- readme = replace (readme , "avrcount" , avr_count )
154- readme = replace (readme , "samcount" , sam_count )
155- readme = replace (readme , "stmcount" , stm_count )
156- readme = replace (readme , "allcount" , all_count )
157- readme = replace (readme , "bsptable" , bsp_table )
158- readme = replace (readme , "drivertable" , driver_table )
159- readme_path .write_text (readme )
160-
161- # extract these keys
162- links = extract (readme , "links" )
163- authors = extract (readme , "authors" ).replace ("\\ @" , "@" )
164- # remove
165- readme = re .sub (r"((<!--webignore-->.*?<!--/webignore-->)|(<!--links-->.*?<!--/links-->)|(<!--/?bsptable-->))\n" , "" , readme , flags = re .DOTALL | re .MULTILINE )
166-
167- index = Environment ().from_string (index_in_path .read_text ()).render ({"content" : readme , "links" : links , "example_table" : example_table })
168- index_path .write_text (index )
169-
170- whoweare = Environment ().from_string (whoweare_in_path .read_text ()).render ({"authors" : authors , "links" : links })
171- whoweare_path .write_text (whoweare )
172-
173- # Copy the example readme over
174- shutil .copy (examples_readme_in_path , examples_readme_path )
175-
176- # Check git differences and fail
177- if "-d" in sys .argv :
178- differences = run (repopath ("." ), r"git diff" )[1 ]
179- if len (differences ):
180- subprocess .run ("git --no-pager diff" , shell = True , cwd = repopath ("." ))
181- print ("\n Please synchronize the modm documentation:\n \n "
182- " $ python3 tools/scripts/synchronize_docs.py\n \n "
183- "and then commit the results!" )
184- exit (1 )
185-
186- exit (0 )
97+ if __name__ == "__main__" :
98+ # All the paths
99+ root = repopath ("." )
100+ board_path = root / "src/modm/board"
101+ example_path = root / "examples"
102+ ignored_path = root / "test/all/ignored.txt"
103+ examples_readme_in_path = root / "examples/README.md"
104+ examples_readme_path = root / "docs/src/guide/examples.md"
105+ readme_path = root / "README.md"
106+ index_in_path = root / "docs/index.md.in"
107+ index_path = root / "docs/src/index.md"
108+ whoweare_in_path = root / "docs/who-we-are.md.in"
109+ whoweare_path = root / "docs/src/who-we-are.md"
110+ modules_in_path = root / "docs/modules.md.in"
111+ modules_path = root / "docs/src/reference/modules.md"
112+
113+ # We cannot use lbuild to enumerate the boards since they only make themselves available for certain devices
114+ boards = [re .search (r"<module>modm:board:(.*?)</module>" , config .read_text ()).group (1 )
115+ for config in Path (repopath ("src/modm/board" )).glob ("*/board.xml" )]
116+ boards = [{"name" : name (b ), "url" : board_url (b )} for b in boards ]
117+ boards .sort (key = lambda b : b ["name" ])
118+ bsp_table = format_table (boards , 4 )
119+
120+ # Get all the example directory paths
121+ examples = [e .relative_to (example_path ) for e in example_path .glob ('**/project.xml' )]
122+ examples = [{"name" : "{}: {}" .format (name (str (e .parts [0 ])), e .parent .relative_to (e .parts [0 ])),
123+ "url" : github_url (Path ("examples" ) / e .parent / "main.cpp" )} for e in examples ]
124+ examples .sort (key = lambda b : b ["name" ])
125+ example_table = format_table (examples , 2 , "left" )
126+
127+ # Get all supported targets
128+ targets = set (get_lbuild (root ).find_option ("modm:target" ).values )
129+ ignored_devices = set (d for d in ignored_path .read_text ().strip ().splitlines () if "#" not in d )
130+ targets -= ignored_devices
131+ avr_count = len ([t for t in targets if t .startswith ("at" )])
132+ stm_count = len ([t for t in targets if t .startswith ("stm32" )])
133+ sam_count = len ([t for t in targets if t .startswith ("sam" )])
134+ all_count = avr_count + stm_count + sam_count
135+
136+ # get the author count
137+ from authors import author_handles
138+ author_count = len (author_handles )
139+
140+ # Get all the modules that are available for the STM32
141+ # Get all drivers, we assume they are available for all devices
142+ drivers = (get_lbuild (root , t ).modules for t in {"avr" , "stm32" , "hosted" })
143+ drivers = {m for mg in drivers for m in mg if m .startswith ("modm:driver:" )}
144+ drivers = sorted (m .replace ("modm:driver:" , "" ) for m in drivers )
145+ drivers = [{"name" : name (d ), "url" : driver_url (d )} for d in drivers if name (d )]
146+ driver_table = format_table (drivers , 6 )
147+
148+ # Read the repo README.md and replace these keys
149+ readme = readme_path .read_text ()
150+ readme = replace (readme , "authorcount" , author_count - 7 )
151+ readme = replace (readme , "avrcount" , avr_count )
152+ readme = replace (readme , "samcount" , sam_count )
153+ readme = replace (readme , "stmcount" , stm_count )
154+ readme = replace (readme , "allcount" , all_count )
155+ readme = replace (readme , "bsptable" , bsp_table )
156+ readme = replace (readme , "drivertable" , driver_table )
157+ readme_path .write_text (readme )
158+
159+ # extract these keys
160+ links = extract (readme , "links" )
161+ authors = extract (readme , "authors" ).replace ("\\ @" , "@" )
162+ # remove
163+ readme = re .sub (r"((<!--webignore-->.*?<!--/webignore-->)|(<!--links-->.*?<!--/links-->)|(<!--/?bsptable-->))\n" , "" , readme , flags = re .DOTALL | re .MULTILINE )
164+
165+ index = Environment ().from_string (index_in_path .read_text ()).render ({"content" : readme , "links" : links , "example_table" : example_table })
166+ index_path .write_text (index )
167+
168+ whoweare = Environment ().from_string (whoweare_in_path .read_text ()).render ({"authors" : authors , "links" : links })
169+ whoweare_path .write_text (whoweare )
170+
171+ # Copy the example readme over
172+ shutil .copy (examples_readme_in_path , examples_readme_path )
173+
174+ # Check git differences and fail
175+ if "-d" in sys .argv :
176+ differences = run (repopath ("." ), r"git diff" )[1 ]
177+ if len (differences ):
178+ subprocess .run ("git --no-pager diff" , shell = True , cwd = repopath ("." ))
179+ print ("\n Please synchronize the modm documentation:\n \n "
180+ " $ python3 tools/scripts/synchronize_docs.py\n \n "
181+ "and then commit the results!" )
182+ exit (1 )
183+
184+ exit (0 )
0 commit comments