33import os
44import sys
55import datetime
6+ import ast
67from collections import OrderedDict , namedtuple
78
89# we do not use the nicer sys.version_info.major
910# for compatibility with Python < 2.7
1011if sys .version_info [0 ] > 2 :
1112 from io import StringIO
12- from configparser import RawConfigParser
13+ from configparser import ConfigParser
1314 import urllib .request
1415
1516 class URLopener (urllib .request .FancyURLopener ):
@@ -18,7 +19,7 @@ def http_error_default(self, url, fp, errcode, errmsg, headers):
1819 sys .exit (- 1 )
1920else :
2021 from StringIO import StringIO
21- from ConfigParser import RawConfigParser
22+ from ConfigParser import ConfigParser
2223 import urllib
2324
2425 class URLopener (urllib .FancyURLopener ):
@@ -231,15 +232,15 @@ def gen_cmakelists(project_name, min_cmake_version, relative_path, modules):
231232# ------------------------------------------------------------------------------
232233
233234
234- def prepend_or_set (config , section , option , value ):
235+ def prepend_or_set (config , section , option , value , defaults ):
235236 """
236237 If option is already set, then value is prepended.
237238 If option is not set, then it is created and set to value.
238239 This is used to prepend options with values which come from the module documentation.
239240 """
240241 if value :
241242 if config .has_option (section , option ):
242- value += '\n %s' % config .get (section , option )
243+ value += '\n %s' % config .get (section , option , 0 , defaults )
243244 config .set (section , option , value )
244245 return config
245246
@@ -276,22 +277,33 @@ def fetch_modules(config, relative_path):
276277 dst = os .path .join (download_directory , 'autocmake_%s' % module_name )
277278 fetch_url (src , dst )
278279 file_name = dst
280+ fetch_dst_directory = download_directory
279281 else :
280282 if os .path .exists (src ):
281283 path = os .path .dirname (src )
282284 name = module_name
283285 file_name = src
286+ fetch_dst_directory = path
284287 else :
285288 sys .stderr .write ("ERROR: %s does not exist\n " % src )
286289 sys .exit (- 1 )
287290
291+ if config .has_option (section , 'override' ):
292+ defaults = ast .literal_eval (config .get (section , 'override' ))
293+ else :
294+ defaults = {}
295+
288296 # we infer config from the module documentation
289297 with open (file_name , 'r' ) as f :
290- config_docopt , config_define , config_export , config_fetch = parse_cmake_module (f .read ())
291- config = prepend_or_set (config , section , 'docopt' , config_docopt )
292- config = prepend_or_set (config , section , 'define' , config_define )
293- config = prepend_or_set (config , section , 'export' , config_export )
294- config = prepend_or_set (config , section , 'fetch' , config_fetch )
298+ config_docopt , config_define , config_export , config_fetch = parse_cmake_module (f .read (), defaults )
299+ config = prepend_or_set (config , section , 'docopt' , config_docopt , defaults )
300+ config = prepend_or_set (config , section , 'define' , config_define , defaults )
301+ config = prepend_or_set (config , section , 'export' , config_export , defaults )
302+ if config_fetch :
303+ for src in config_fetch .split ('\n ' ):
304+ dst = os .path .join (fetch_dst_directory , os .path .basename (src ))
305+ fetch_url (src , dst )
306+
295307 modules .append (Module (path = path , name = name ))
296308 i += 1
297309 print_progress_bar (
@@ -301,6 +313,8 @@ def fetch_modules(config, relative_path):
301313 width = 30
302314 )
303315 if config .has_option (section , 'fetch' ):
316+ # when we fetch directly from autocmake.cfg
317+ # we download into downloaded/
304318 for src in config .get (section , 'fetch' ).split ('\n ' ):
305319 dst = os .path .join (download_directory , os .path .basename (src ))
306320 fetch_url (src , dst )
@@ -369,7 +383,7 @@ def main(argv):
369383
370384 # read config file
371385 print ('- parsing autocmake.cfg' )
372- config = RawConfigParser (dict_type = OrderedDict )
386+ config = ConfigParser (dict_type = OrderedDict )
373387 config .read ('autocmake.cfg' )
374388
375389 if not config .has_option ('project' , 'name' ):
@@ -417,7 +431,7 @@ def make_executable(path):
417431# ------------------------------------------------------------------------------
418432
419433
420- def parse_cmake_module (s_in ):
434+ def parse_cmake_module (s_in , defaults = {} ):
421435
422436 config_docopt = None
423437 config_define = None
@@ -448,18 +462,18 @@ def parse_cmake_module(s_in):
448462 autocmake_entry = '[foo]\n ' + autocmake_entry
449463
450464 buf = StringIO (autocmake_entry )
451- config = RawConfigParser (dict_type = OrderedDict )
465+ config = ConfigParser (dict_type = OrderedDict )
452466 config .readfp (buf )
453467
454468 for section in config .sections ():
455469 if config .has_option (section , 'docopt' ):
456- config_docopt = config .get (section , 'docopt' )
470+ config_docopt = config .get (section , 'docopt' , 0 , defaults )
457471 if config .has_option (section , 'define' ):
458- config_define = config .get (section , 'define' )
472+ config_define = config .get (section , 'define' , 0 , defaults )
459473 if config .has_option (section , 'export' ):
460- config_export = config .get (section , 'export' )
474+ config_export = config .get (section , 'export' , 0 , defaults )
461475 if config .has_option (section , 'fetch' ):
462- config_fetch = config .get (section , 'fetch' )
476+ config_fetch = config .get (section , 'fetch' , 0 , defaults )
463477
464478 return config_docopt , config_define , config_export , config_fetch
465479
0 commit comments