PageRenderTime 51ms CodeModel.GetById 36ms app.highlight 10ms RepoModel.GetById 3ms app.codeStats 0ms

/ruffus/test/simpler.py

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