/ruffus/test/simpler_at_runtime.py

https://code.google.com/p/ruffus/ · Python · 250 lines · 138 code · 60 blank · 52 comment · 21 complexity · f6b487568b2cdf498a476b10d77032c5 MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. test_tasks.py
  4. """
  5. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  6. # options
  7. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  8. from optparse import OptionParser
  9. import sys, os
  10. import os.path
  11. import StringIO
  12. # add self to search path for testing
  13. exe_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
  14. sys.path.insert(0,os.path.abspath(os.path.join(exe_path,"..", "..")))
  15. if __name__ == '__main__':
  16. module_name = os.path.split(sys.argv[0])[1]
  17. module_name = os.path.splitext(module_name)[0];
  18. else:
  19. module_name = __name__
  20. parser = OptionParser(version="%prog 1.0")
  21. parser.add_option("-t", "--target_tasks", dest="target_tasks",
  22. action="append",
  23. default = list(),
  24. metavar="JOBNAME",
  25. type="string",
  26. help="Target task(s) of pipeline.")
  27. parser.add_option("-R", "--runtime_files", dest="runtime_files",
  28. action="append",
  29. default = ["a.3"],
  30. metavar="JOBNAME",
  31. type="string",
  32. help="Target task(s) of pipeline.")
  33. parser.add_option("-f", "--forced_tasks", dest="forced_tasks",
  34. action="append",
  35. default = list(),
  36. metavar="JOBNAME",
  37. type="string",
  38. help="Pipeline task(s) which will be included even if they are up to date.")
  39. parser.add_option("-j", "--jobs", dest="jobs",
  40. default=1,
  41. metavar="jobs",
  42. type="int",
  43. help="Specifies the number of jobs (commands) to run simultaneously.")
  44. parser.add_option("-v", "--verbose", dest = "verbose",
  45. action="count", default=0,
  46. help="Print more verbose messages for each additional verbose level.")
  47. parser.add_option("-d", "--debug", dest = "debug",
  48. action="count", default=0,
  49. help="Cleanup afterwards.")
  50. parser.add_option("--dependency", dest="dependency_file",
  51. metavar="FILE",
  52. type="string",
  53. help="Print a dependency graph of the pipeline that would be executed "
  54. "to FILE, but do not execute it.")
  55. parser.add_option("-F", "--dependency_graph_format", dest="dependency_graph_format",
  56. metavar="FORMAT",
  57. type="string",
  58. default = 'svg',
  59. help="format of dependency graph file. Can be 'ps' (PostScript), "+
  60. "'svg' 'svgz' (Structured Vector Graphics), " +
  61. "'png' 'gif' (bitmap graphics) etc ")
  62. parser.add_option("-n", "--just_print", dest="just_print",
  63. action="store_true", default=False,
  64. help="Print a description of the jobs that would be executed, "
  65. "but do not execute them.")
  66. parser.add_option("-M", "--minimal_rebuild_mode", dest="minimal_rebuild_mode",
  67. action="store_true", default=False,
  68. help="Rebuild a minimum of tasks necessary for the target. "
  69. "Ignore upstream out of date tasks if intervening tasks are fine.")
  70. parser.add_option("-K", "--no_key_legend_in_graph", dest="no_key_legend_in_graph",
  71. action="store_true", default=False,
  72. help="Do not print out legend and key for dependency graph.")
  73. parser.add_option("-H", "--draw_graph_horizontally", dest="draw_horizontally",
  74. action="store_true", default=False,
  75. help="Draw horizontal dependency graph.")
  76. parameters = [
  77. ]
  78. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  79. # imports
  80. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  81. import StringIO
  82. import re
  83. import operator
  84. import sys,os
  85. from collections import defaultdict
  86. sys.path.append(os.path.abspath(os.path.join(exe_path,"..", "..")))
  87. from ruffus import *
  88. # use simplejson in place of json for python < 2.6
  89. try:
  90. import json
  91. except ImportError:
  92. import simplejson
  93. json = simplejson
  94. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  95. # Functions
  96. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  97. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  98. # Main logic
  99. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  100. # get help string
  101. f =StringIO.StringIO()
  102. parser.print_help(f)
  103. helpstr = f.getvalue()
  104. (options, remaining_args) = parser.parse_args()
  105. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  106. # Tasks
  107. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  108. #
  109. # task1
  110. #
  111. @split(None, ['a.1'] + options.runtime_files)
  112. def task1(infile, outfiles):
  113. """
  114. First task
  115. """
  116. output_text = ""
  117. output_text += json.dumps(infile) + " -> " + json.dumps(outfiles) + "\n"
  118. for outfile in outfiles:
  119. open(outfile, "w").write(output_text)
  120. #
  121. # task2
  122. #
  123. @transform(task1, suffix(".1"), ".2")
  124. def task2(infile, outfile):
  125. """
  126. Second task
  127. """
  128. output_text = open(infile).read() if infile else ""
  129. output_text += json.dumps(infile) + " -> " + json.dumps(outfile) + "\n"
  130. open(outfile, "w").write(output_text)
  131. #
  132. # task3
  133. #
  134. @transform(task2, suffix(".2"), ".3")
  135. def task3(infile, outfile):
  136. """
  137. Third task
  138. """
  139. output_text = open(infile).read() if infile else ""
  140. output_text += json.dumps(infile) + " -> " + json.dumps(outfile) + "\n"
  141. open(outfile, "w").write(output_text)
  142. #
  143. # task4
  144. #
  145. @follows(task3)
  146. @transform(runtime_parameter("a"), suffix(".3"), ".4")
  147. def task4(infile, outfile):
  148. """
  149. Fourth task
  150. """
  151. output_text = open(infile).read() if infile else ""
  152. output_text += json.dumps(infile) + " -> " + json.dumps(outfile) + "\n"
  153. open(outfile, "w").write(output_text)
  154. #
  155. # Necessary to protect the "entry point" of the program under windows.
  156. # see: http://docs.python.org/library/multiprocessing.html#multiprocessing-programming
  157. #
  158. if __name__ == '__main__':
  159. if options.just_print:
  160. pipeline_printout(sys.stdout, options.target_tasks, options.forced_tasks,
  161. gnu_make_maximal_rebuild_mode = not options.minimal_rebuild_mode,
  162. verbose = options.verbose, runtime_data = {"a": options.runtime_files})
  163. elif options.dependency_file:
  164. pipeline_printout_graph ( open(options.dependency_file, "w"),
  165. options.dependency_graph_format,
  166. options.target_tasks,
  167. options.forced_tasks,
  168. draw_vertically = not options.draw_horizontally,
  169. gnu_make_maximal_rebuild_mode = not options.minimal_rebuild_mode,
  170. no_key_legend = options.no_key_legend_in_graph)
  171. elif options.debug:
  172. import os
  173. for f in ["a.1", "a.2","a.3","a.4"]:
  174. if os.path.exists(f):
  175. os.unlink(f)
  176. pipeline_run(options.target_tasks, options.forced_tasks, multiprocess = options.jobs,
  177. gnu_make_maximal_rebuild_mode = not options.minimal_rebuild_mode,
  178. verbose = options.verbose, runtime_data = {"a": options.runtime_files})
  179. for f in ["a.1", "a.2","a.3","a.4"]:
  180. if os.path.exists(f):
  181. os.unlink(f)
  182. else:
  183. raise Exception("%s is missing" % f)
  184. print "OK"
  185. else:
  186. pipeline_run(options.target_tasks, options.forced_tasks, multiprocess = options.jobs,
  187. gnu_make_maximal_rebuild_mode = not options.minimal_rebuild_mode,
  188. verbose = options.verbose, runtime_data = {"a": options.runtime_files})