66import shlex
77import sys
88import os
9+ import re
910
1011rust_dir = os .path .dirname (os .path .abspath (__file__ ))
1112rust_dir = os .path .dirname (rust_dir )
@@ -585,16 +586,31 @@ def parse_example_config(known_args, config):
585586 section_order = [None ]
586587 targets = {}
587588 top_level_keys = []
589+ comment_lines = []
588590
589591 with open (rust_dir + "/bootstrap.example.toml" ) as example_config :
590592 example_lines = example_config .read ().split ("\n " )
591593 for line in example_lines :
592- if cur_section is None :
593- if line .count ("=" ) == 1 :
594- top_level_key = line .split ("=" )[0 ]
595- top_level_key = top_level_key .strip (" #" )
596- top_level_keys .append (top_level_key )
597- if line .startswith ("[" ):
594+ if line .count ("=" ) == 1 and not line .startswith ("# " ):
595+ key = line .split ("=" )[0 ]
596+ key = key .strip (" #" )
597+ parts = key .split ("." )
598+ if len (parts ) > 1 :
599+ cur_section = parts [0 ]
600+ if cur_section not in sections :
601+ sections [cur_section ] = ["[" + cur_section + "]" ]
602+ section_order .append (cur_section )
603+ elif cur_section is None :
604+ top_level_keys .append (key )
605+ # put the comment lines within the start of
606+ # a new section, not outside it.
607+ sections [cur_section ] += comment_lines
608+ comment_lines = []
609+ # remove just the `section.` part from the line, if present.
610+ sections [cur_section ].append (
611+ re .sub ("(#?)([a-zA-Z_-]+\\ .)?(.*)" , "\\ 1\\ 3" , line )
612+ )
613+ elif line .startswith ("[" ):
598614 cur_section = line [1 :- 1 ]
599615 if cur_section .startswith ("target" ):
600616 cur_section = "target"
@@ -605,8 +621,9 @@ def parse_example_config(known_args, config):
605621 sections [cur_section ] = [line ]
606622 section_order .append (cur_section )
607623 else :
608- sections [ cur_section ] .append (line )
624+ comment_lines .append (line )
609625
626+ sections [cur_section ] += comment_lines
610627 # Fill out the `targets` array by giving all configured targets a copy of the
611628 # `target` section we just loaded from the example config
612629 configured_targets = [build (known_args )]
0 commit comments