1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
#!/usr/bin/env python
"""
test2.py
[--log_file PATH]
[--verbose]
"""
################################################################################
#
# test2
#
#
# Copyright (c) 7/16/2010 Leo Goodstadt
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#################################################################################
import sys, os
# add self to search path for testing
if __name__ == '__main__':
exe_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
module_name = os.path.split(sys.argv[0])[1]
module_name = os.path.splitext(module_name)[0];
else:
module_name = __name__
# Use import path from <<../python_modules>>
if __name__ == '__main__':
sys.path.insert(0, os.path.abspath(os.path.join(exe_path,"../..")))
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# options
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
if __name__ == '__main__':
from optparse import OptionParser
import StringIO
parser = OptionParser(version="%prog 1.0", usage = "\n\n %progs [options]")
parser.add_option("-i", "--input_file", dest="input_file",
metavar="FILE",
type="string",
help="Name and path of input file. "
"Defaults to reading from STDIN.")
#
# general options: verbosity / logging
#
parser.add_option("-v", "--verbose", dest = "verbose",
action="count", default=0,
help="Print more verbose messages for each additional verbose level.")
parser.add_option("-L", "--log_file", dest="log_file",
metavar="FILE",
type="string",
help="Name and path of log file")
parser.add_option("--skip_parameter_logging", dest="skip_parameter_logging",
action="store_true", default=False,
help="Do not print program parameters to log.")
parser.add_option("--debug", dest="debug",
action="count", default=0,
help="Set default program parameters in debugging mode.")
#
# pipeline
#
parser.add_option("-t", "--target_tasks", dest="target_tasks",
action="append",
default = list(),
metavar="JOBNAME",
type="string",
help="Target task(s) of pipeline.")
parser.add_option("-j", "--jobs", dest="jobs",
default=1,
metavar="N",
type="int",
help="Allow N jobs (commands) to run simultaneously.")
parser.add_option("-n", "--just_print", dest="just_print",
action="store_true", default=False,
help="Don't actually run any commands; just print the pipeline.")
parser.add_option("--flowchart", dest="flowchart",
metavar="FILE",
type="string",
help="Don't actually run any commands; just print the pipeline "
"as a flowchart.")
#
# Less common pipeline options
#
parser.add_option("--key_legend_in_graph", dest="key_legend_in_graph",
action="store_true", default=False,
help="Print out legend and key for dependency graph.")
parser.add_option("--draw_graph_horizontally", dest="draw_horizontally",
action="store_true", default=False,
help="Draw horizontal dependency graph.")
parser.add_option("--forced_tasks", dest="forced_tasks",
action="append",
default = list(),
metavar="JOBNAME",
type="string",
help="Pipeline task(s) which will be included even if they are up to date.")
# get help string
f =StringIO.StringIO()
parser.print_help(f)
helpstr = f.getvalue()
original_args = " ".join(sys.argv)
(options, remaining_args) = parser.parse_args()
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# #
# Debug: Change these #
# #
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
options.flowchart = "front_page_flowchart.png"
options.key_legend_in_graph = True
if options.debug:
options.log_file = os.path.join("test2.log")
options.verbose = 5
options.log_parameters = True
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# #
# Debug: Change these #
# #
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# mandatory options
#
mandatory_options = []
def check_mandatory_options (options, mandatory_options, helpstr):
"""
Check if specified mandatory options have b een defined
"""
missing_options = []
for o in mandatory_options:
if not getattr(options, o):
missing_options.append("--" + o)
if not len(missing_options):
return
raise Exception("Missing mandatory parameter%s: %s.\n\n%s\n\n" %
("s" if len(missing_options) > 1 else "",
", ".join(missing_options),
helpstr))
check_mandatory_options (options, mandatory_options, helpstr)
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# imports
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
from ruffus import *
from ruffus.ruffus_exceptions import JobSignalledBreak
#from json import dumps
#from collections import defaultdict
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# Functions
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# Logger
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
if __name__ == '__main__':
import logging
import logging.handlers
MESSAGE = 15
logging.addLevelName(MESSAGE, "MESSAGE")
def setup_std_logging (logger, log_file, verbose):
"""
set up logging using programme options
"""
class debug_filter(logging.Filter):
"""
Ignore INFO messages
"""
def filter(self, record):
return logging.INFO != record.levelno
class NullHandler(logging.Handler):
"""
for when there is no logging
"""
def emit(self, record):
pass
# We are interesting in all messages
logger.setLevel(logging.DEBUG)
has_handler = False
# log to file if that is specified
if log_file:
handler = logging.FileHandler(log_file, delay=False)
handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)6s - %(message)s"))
handler.setLevel(MESSAGE)
logger.addHandler(handler)
has_handler = True
# log to stderr if verbose
if verbose:
stderrhandler = logging.StreamHandler(sys.stderr)
stderrhandler.setFormatter(logging.Formatter(" %(message)s"))
stderrhandler.setLevel(logging.DEBUG)
if log_file:
stderrhandler.addFilter(debug_filter())
logger.addHandler(stderrhandler)
has_handler = True
# no logging
if not has_handler:
logger.addHandler(NullHandler())
#
# set up log
#
logger = logging.getLogger(module_name)
setup_std_logging(logger, options.log_file, options.verbose)
#
# Allow logging across Ruffus pipeline
#
def get_logger (logger_name, args):
return logger
from ruffus.proxy_logger import *
(logger_proxy,
logging_mutex) = make_shared_logger_and_proxy (get_logger,
module_name,
{})
#
# log programme parameters
#
if not options.skip_parameter_logging:
programme_name = os.path.split(sys.argv[0])[1]
logger.info("%s %s" % (programme_name, original_args))
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# Pipeline
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
@files(None, "a.1")
def task1(input_file, output_file):
open(output_file, "w")
@transform(task1, suffix("1"), "2")
def task2(input_file, output_file):
open(output_file, "w")
@transform(task2, suffix("2"), "3")
def task3(input_file, output_file):
open(output_file, "w")
@transform(task3, suffix("3"), "4")
def task4(input_file, output_file):
open(output_file, "w")
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# Main logic
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
import time
open("a.2", "w")
time.sleep(1)
open("a.1", "w")
time.sleep(1)
open("a.3", "w")
pipeline_printout_graph ( open(options.flowchart, "w"),
os.path.splitext(options.flowchart)[1][1:],
[task4],
no_key_legend = not options.key_legend_in_graph,
user_colour_scheme = {"colour_scheme_index":0},
pipeline_name = "Pipeline Flowchart:",
size = (6,5),
dpi = 72,
)
os.unlink("a.1")
os.unlink("a.2")
os.unlink("a.3")
#pipeline_run(options.target_tasks, options.forced_tasks,
# multiprocess = options.jobs,
# logger = stderr_logger,
# verbose = options.verbose)
|