/doc/static_data/example_scripts/ruffus_template.py

https://code.google.com/p/ruffus/ · Python · 270 lines · 113 code · 73 blank · 84 comment · 15 complexity · 3acf92f4984760581c786670e9ebaca1 MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. ruffus_template.py
  4. [--log_file PATH]
  5. [--verbose]
  6. [--target_tasks]
  7. [--jobs]
  8. [--just_print]
  9. [--flowchart]
  10. [--key_legend_in_graph]
  11. [--forced_tasks]
  12. """
  13. import sys, os
  14. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  15. # options
  16. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  17. if __name__ == '__main__':
  18. from optparse import OptionParser
  19. import StringIO
  20. parser = OptionParser(version="%prog 1.0", usage = "\n\n %progs [options]")
  21. #
  22. # general options: verbosity / logging
  23. #
  24. parser.add_option("-v", "--verbose", dest = "verbose",
  25. action="count", default=0,
  26. help="Print more verbose messages for each additional verbose level.")
  27. parser.add_option("-L", "--log_file", dest="log_file",
  28. metavar="FILE",
  29. type="string",
  30. help="Name and path of log file")
  31. #
  32. # pipeline
  33. #
  34. parser.add_option("-t", "--target_tasks", dest="target_tasks",
  35. action="append",
  36. default = list(),
  37. metavar="JOBNAME",
  38. type="string",
  39. help="Target task(s) of pipeline.")
  40. parser.add_option("-j", "--jobs", dest="jobs",
  41. default=1,
  42. metavar="N",
  43. type="int",
  44. help="Allow N jobs (commands) to run simultaneously.")
  45. parser.add_option("-n", "--just_print", dest="just_print",
  46. action="store_true", default=False,
  47. help="Don't actually run any commands; just print the pipeline.")
  48. parser.add_option("--flowchart", dest="flowchart",
  49. metavar="FILE",
  50. type="string",
  51. help="Don't actually run any commands; just print the pipeline "
  52. "as a flowchart.")
  53. #
  54. # Less common pipeline options
  55. #
  56. parser.add_option("--key_legend_in_graph", dest="key_legend_in_graph",
  57. action="store_true", default=False,
  58. help="Print out legend and key for dependency graph.")
  59. parser.add_option("--forced_tasks", dest="forced_tasks",
  60. action="append",
  61. default = list(),
  62. metavar="JOBNAME",
  63. type="string",
  64. help="Pipeline task(s) which will be included even if they are up to date.")
  65. # get help string
  66. f =StringIO.StringIO()
  67. parser.print_help(f)
  68. helpstr = f.getvalue()
  69. (options, remaining_args) = parser.parse_args()
  70. #vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  71. # #
  72. # Change this if necessary #
  73. # #
  74. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  75. #
  76. # Add names of mandatory options,
  77. # strings corresponding to the "dest" parameter
  78. # in the options defined above
  79. #
  80. mandatory_options = [ ]
  81. #vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  82. # #
  83. # Change this if necessary #
  84. # #
  85. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  86. def check_mandatory_options (options, mandatory_options, helpstr):
  87. """
  88. Check if specified mandatory options have b een defined
  89. """
  90. missing_options = []
  91. for o in mandatory_options:
  92. if not getattr(options, o):
  93. missing_options.append("--" + o)
  94. if not len(missing_options):
  95. return
  96. raise Exception("Missing mandatory parameter%s: %s.\n\n%s\n\n" %
  97. ("s" if len(missing_options) > 1 else "",
  98. ", ".join(missing_options),
  99. helpstr))
  100. check_mandatory_options (options, mandatory_options, helpstr)
  101. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  102. # imports
  103. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  104. from ruffus import *
  105. from ruffus.ruffus_exceptions import JobSignalledBreak
  106. #from json import dumps
  107. #from collections import defaultdict
  108. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  109. # Functions
  110. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  111. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  112. # Logger
  113. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  114. if __name__ == '__main__':
  115. import logging
  116. import logging.handlers
  117. MESSAGE = 15
  118. logging.addLevelName(MESSAGE, "MESSAGE")
  119. def setup_std_logging (logger, log_file, verbose):
  120. """
  121. set up logging using programme options
  122. """
  123. class debug_filter(logging.Filter):
  124. """
  125. Ignore INFO messages
  126. """
  127. def filter(self, record):
  128. return logging.INFO != record.levelno
  129. class NullHandler(logging.Handler):
  130. """
  131. for when there is no logging
  132. """
  133. def emit(self, record):
  134. pass
  135. # We are interesting in all messages
  136. logger.setLevel(logging.DEBUG)
  137. has_handler = False
  138. # log to file if that is specified
  139. if log_file:
  140. handler = logging.FileHandler(log_file, delay=False)
  141. handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)6s - %(message)s"))
  142. handler.setLevel(MESSAGE)
  143. logger.addHandler(handler)
  144. has_handler = True
  145. # log to stderr if verbose
  146. if verbose:
  147. stderrhandler = logging.StreamHandler(sys.stderr)
  148. stderrhandler.setFormatter(logging.Formatter(" %(message)s"))
  149. stderrhandler.setLevel(logging.DEBUG)
  150. if log_file:
  151. stderrhandler.addFilter(debug_filter())
  152. logger.addHandler(stderrhandler)
  153. has_handler = True
  154. # no logging
  155. if not has_handler:
  156. logger.addHandler(NullHandler())
  157. #
  158. # set up log
  159. #
  160. logger = logging.getLogger(module_name)
  161. setup_std_logging(logger, options.log_file, options.verbose)
  162. #
  163. # Allow logging across Ruffus pipeline
  164. #
  165. def get_logger (logger_name, args):
  166. return logger
  167. from ruffus.proxy_logger import *
  168. (logger_proxy,
  169. logging_mutex) = make_shared_logger_and_proxy (get_logger,
  170. module_name,
  171. {})
  172. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  173. # Pipeline
  174. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  175. # Put pipeline code here
  176. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  177. # Main logic
  178. #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  179. if __name__ == '__main__':
  180. if options.just_print:
  181. pipeline_printout(sys.stdout, options.target_tasks, options.forced_tasks,
  182. verbose=options.verbose)
  183. elif options.flowchart:
  184. pipeline_printout_graph ( open(options.flowchart, "w"),
  185. # use flowchart file name extension to decide flowchart format
  186. # e.g. svg, jpg etc.
  187. os.path.splitext(options.flowchart)[1][1:],
  188. options.target_tasks,
  189. options.forced_tasks,
  190. no_key_legend = not options.key_legend_in_graph)
  191. else:
  192. pipeline_run(options.target_tasks, options.forced_tasks,
  193. multiprocess = options.jobs,
  194. logger = stderr_logger,
  195. verbose = options.verbose)