jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.9KB

  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. # Mathieu Courtois - EDF R&D, 2013 - http://www.code-aster.org
  4. """
  5. When a project has a lot of options the 'waf configure' command line can be
  6. very long and it becomes a cause of error.
  7. This tool provides a convenient way to load a set of configuration parameters
  8. from a local file or from a remote url.
  9. The configuration parameters are stored in a Python file that is imported as
  10. an extra waf tool can be.
  11. Example:
  12. $ waf configure --use-config-dir=http://www.anywhere.org --use-config=myconf1 ...
  13. The file 'myconf1' will be downloaded from 'http://www.anywhere.org'
  14. (or 'http://www.anywhere.org/wafcfg').
  15. If the files are available locally, it could be:
  16. $ waf configure --use-config-dir=/somewhere/myconfigurations --use-config=myconf1 ...
  17. The configuration of 'myconf1.py' is automatically loaded by calling
  18. its 'configure' function. In this example, it defines environment variables and
  19. set options:
  20. def configure(self):
  21. self.env['CC'] = 'gcc-4.8'
  22. self.env.append_value('LIBPATH', [...])
  23. self.options.perlbinary = '/usr/local/bin/perl'
  24. self.options.pyc = False
  25. The corresponding command line should have been:
  26. $ CC=gcc-4.8 LIBPATH=... waf configure --nopyc --with-perl-binary=/usr/local/bin/perl
  27. This is an extra tool, not bundled with the default waf binary.
  28. To add the use_config tool to the waf file:
  29. $ ./waf-light --tools=use_config
  30. When using this tool, the wscript will look like:
  31. def options(opt):
  32. opt.load('use_config')
  33. def configure(conf):
  34. conf.load('use_config')
  35. """
  36. import sys
  37. import os.path as osp
  38. import os
  39. try:
  40. from urllib import request
  41. except ImportError:
  42. from urllib import urlopen
  43. else:
  44. urlopen = request.urlopen
  45. from waflib import Errors, Context, Logs, Utils, Options
  46. try:
  47. from urllib.parse import urlparse
  48. except ImportError:
  49. from urlparse import urlparse
  50. DEFAULT_DIR = 'wafcfg'
  51. # add first the current wafcfg subdirectory
  52. sys.path.append(osp.abspath(DEFAULT_DIR))
  53. def options(self):
  54. group = self.add_option_group('configure options')
  55. group.add_option('--download', dest='download', default=False, action='store_true', help='try to download the tools if missing')
  56. group.add_option('--use-config', action='store', default=None,
  57. metavar='CFG', dest='use_config',
  58. help='force the configuration parameters by importing '
  59. 'CFG.py. Several modules may be provided (comma '
  60. 'separated).')
  61. group.add_option('--use-config-dir', action='store', default=DEFAULT_DIR,
  62. metavar='CFG_DIR', dest='use_config_dir',
  63. help='path or url where to find the configuration file')
  64. def download_check(node):
  65. """
  66. Hook to check for the tools which are downloaded. Replace with your function if necessary.
  67. """
  68. pass
  69. def download_tool(tool, force=False, ctx=None):
  70. """
  71. Download a Waf tool from the remote repository defined in :py:const:`waflib.Context.remote_repo`::
  72. $ waf configure --download
  73. """
  74. for x in Utils.to_list(Context.remote_repo):
  75. for sub in Utils.to_list(Context.remote_locs):
  76. url = '/'.join((x, sub, tool + '.py'))
  77. try:
  78. web = urlopen(url)
  79. try:
  80. if web.getcode() != 200:
  81. continue
  82. except AttributeError:
  83. pass
  84. except Exception:
  85. # on python3 urlopen throws an exception
  86. # python 2.3 does not have getcode and throws an exception to fail
  87. continue
  88. else:
  89. tmp = ctx.root.make_node(os.sep.join((Context.waf_dir, 'waflib', 'extras', tool + '.py')))
  90. tmp.write(web.read(), 'wb')
  91. Logs.warn('Downloaded %s from %s' % (tool, url))
  92. download_check(tmp)
  93. try:
  94. module = Context.load_tool(tool)
  95. except Exception:
  96. Logs.warn('The tool %s from %s is unusable' % (tool, url))
  97. try:
  98. tmp.delete()
  99. except Exception:
  100. pass
  101. continue
  102. return module
  103. raise Errors.WafError('Could not load the Waf tool')
  104. def load_tool(tool, tooldir=None, ctx=None):
  105. try:
  106. module = Context.load_tool_default(tool, tooldir)
  107. except ImportError as e:
  108. if Options.options.download:
  109. module = download_tool(tool, ctx=ctx)
  110. if not module:
  111. ctx.fatal('Could not load the Waf tool %r or download a suitable replacement from the repository (sys.path %r)\n%s' % (tool, sys.path, e))
  112. else:
  113. ctx.fatal('Could not load the Waf tool %r from %r (try the --download option?):\n%s' % (tool, sys.path, e))
  114. return module
  115. Context.load_tool_default = Context.load_tool
  116. Context.load_tool = load_tool
  117. def configure(self):
  118. opts = self.options
  119. use_cfg = opts.use_config
  120. if use_cfg is None:
  121. return
  122. url = urlparse(opts.use_config_dir)
  123. kwargs = {}
  124. if url.scheme:
  125. kwargs['download'] = True
  126. kwargs['remote_url'] = url.geturl()
  127. # search first with the exact url, else try with +'/wafcfg'
  128. kwargs['remote_locs'] = ['', DEFAULT_DIR]
  129. tooldir = url.geturl() + ' ' + DEFAULT_DIR
  130. for cfg in use_cfg.split(','):
  131. Logs.pprint('NORMAL', "Searching configuration '%s'..." % cfg)
  132. self.load(cfg, tooldir=tooldir, **kwargs)
  133. self.start_msg('Checking for configuration')
  134. self.end_msg(use_cfg)