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.

107 lines
3.8KB

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Hans-Martin von Gaudecker, 2012
  4. """
  5. Run a Python script in the directory specified by **ctx.bldnode**.
  6. Select a Python version by specifying the **version** keyword for
  7. the task generator instance as integer 2 or 3. Default is 3.
  8. If the build environment has an attribute "PROJECT_PATHS" with
  9. a key "PROJECT_ROOT", its value will be appended to the PYTHONPATH.
  10. Same a string passed to the optional **add_to_pythonpath**
  11. keyword (appended after the PROJECT_ROOT).
  12. Usage::
  13. ctx(features='run_py_script', version=3,
  14. source='some_script.py',
  15. target=['some_table.tex', 'some_figure.eps'],
  16. deps='some_data.csv',
  17. add_to_pythonpath='src/some/library')
  18. """
  19. import os, re
  20. from waflib import Task, TaskGen, Logs
  21. def configure(conf):
  22. """TODO: Might need to be updated for Windows once
  23. "PEP 397":http://www.python.org/dev/peps/pep-0397/ is settled.
  24. """
  25. conf.find_program('python', var='PY2CMD', mandatory=False)
  26. conf.find_program('python3', var='PY3CMD', mandatory=False)
  27. if not conf.env.PY2CMD and not conf.env.PY3CMD:
  28. conf.fatal("No Python interpreter found!")
  29. @Task.update_outputs
  30. class run_py_2_script(Task.Task):
  31. """Run a Python 2 script."""
  32. run_str = '${PY2CMD} ${SRC[0].abspath()}'
  33. shell=True
  34. @Task.update_outputs
  35. class run_py_3_script(Task.Task):
  36. """Run a Python 3 script."""
  37. run_str = '${PY3CMD} ${SRC[0].abspath()}'
  38. shell=True
  39. @TaskGen.feature('run_py_script')
  40. @TaskGen.before_method('process_source')
  41. def apply_run_py_script(tg):
  42. """Task generator for running either Python 2 or Python 3 on a single
  43. script.
  44. Attributes:
  45. * source -- A **single** source node or string. (required)
  46. * target -- A single target or list of targets (nodes or strings).
  47. * deps -- A single dependency or list of dependencies (nodes or strings)
  48. * add_to_pythonpath -- A string that will be appended to the PYTHONPATH environment variable.
  49. If the build environment has an attribute "PROJECT_PATHS" with
  50. a key "PROJECT_ROOT", its value will be appended to the PYTHONPATH.
  51. """
  52. # Set the Python version to use, default to 3.
  53. v = getattr(tg, 'version', 3)
  54. if v not in (2, 3): raise ValueError("Specify the 'version' attribute for run_py_script task generator as integer 2 or 3.\n Got: %s" %v)
  55. # Convert sources and targets to nodes
  56. src_node = tg.path.find_resource(tg.source)
  57. tgt_nodes = [tg.path.find_or_declare(t) for t in tg.to_list(tg.target)]
  58. # Create the task.
  59. tsk = tg.create_task('run_py_%d_script' %v, src=src_node, tgt=tgt_nodes)
  60. # custom execution environment
  61. # TODO use a list and os.sep.join(lst) at the end instead of concatenating strings
  62. tsk.env.env = dict(os.environ)
  63. tsk.env.env['PYTHONPATH'] = tsk.env.env.get('PYTHONPATH', '')
  64. project_paths = getattr(tsk.env, 'PROJECT_PATHS', None)
  65. if project_paths and 'PROJECT_ROOT' in project_paths:
  66. tsk.env.env['PYTHONPATH'] += os.pathsep + project_paths['PROJECT_ROOT'].abspath()
  67. if getattr(tg, 'add_to_pythonpath', None):
  68. tsk.env.env['PYTHONPATH'] += os.pathsep + tg.add_to_pythonpath
  69. # Clean up the PYTHONPATH -- replace double occurrences of path separator
  70. tsk.env.env['PYTHONPATH'] = re.sub(os.pathsep + '+', os.pathsep, tsk.env.env['PYTHONPATH'])
  71. # Clean up the PYTHONPATH -- doesn't like starting with path separator
  72. if tsk.env.env['PYTHONPATH'].startswith(os.pathsep):
  73. tsk.env.env['PYTHONPATH'] = tsk.env.env['PYTHONPATH'][1:]
  74. # dependencies (if the attribute 'deps' changes, trigger a recompilation)
  75. for x in tg.to_list(getattr(tg, 'deps', [])):
  76. node = tg.path.find_resource(x)
  77. if not node:
  78. tg.bld.fatal('Could not find dependency %r for running %r' % (x, src_node.abspath()))
  79. tsk.dep_nodes.append(node)
  80. Logs.debug('deps: found dependencies %r for running %r' % (tsk.dep_nodes, src_node.abspath()))
  81. # Bypass the execution of process_source by setting the source to an empty list
  82. tg.source = []