%pythoncode %{ import sys import re import os import os.path from optparse import OptionParser def _get_script_name(): return os.path.splitext(os.path.split(sys.argv[0])[1])[0] def _get_script_path(): return os.path.split(sys.argv[0])[0] def _get_is_sample(): return _get_script_name().startswith("sample") def _get_number_of_samples(): for i in range(0,100): if not os.path.exists(os.path.join(_get_script_path(), "sample_"+str(i)+".py")): return i def _get_is_analysis(): return _get_script_name().startswith("analyze") def _get_is_setup(): return _get_script_name() == "setup" def _get_script_index(): return int(_get_script_name().split("_")[1]) def _get_default_input(): if _get_is_sample() and _get_script_index()==0: return "setup" elif _get_is_sample(): return "sampled_"+str(_get_script_index()-1) elif _get_is_analysis() and _get_script_index()==0: return "sampled_"+str(_get_number_of_samples()-1) elif _get_is_analysis(): return "analyzed_"+str(_get_script_index()-1) else: raise ValueError("bad script name, expect sample_n or analyze_n") def _get_default_output(): if _get_is_sample(): return "sampled_"+str(_get_script_index()) elif _get_is_analysis(): return "analyzed_"+str(_get_script_index()) elif _get_is_setup(): return "setup" def _is_sample(): ptn="sample_.*.py" r= re.compile(ptn) nm= os.path.split(sys.argv[0])[1] #print nm, r.match(nm), ptn return r.search(nm) def _is_sample_0(): return sys.argv[0].endswith("sample_0.py") def _create_parser(): parser = OptionParser(usage='%prog [options]') parser.add_option( '-o', '--output-dir', dest='output_dir', default='auto', help='Where to put the output for the script', ) if not _is_sample_0(): parser.add_option( '-i', '--input-dir', dest='input_dir', default='auto', help='Where to find input for the script', ) parser.add_option( '-d', '--data-dir', dest='data_dir', default='auto', help='Where to find data for the script', ) if _is_sample(): parser.add_option( '-j', '--job', dest='job_index', help='The index for the current job') parser.add_option( '-n', '--num-jobs', dest='num_jobs', help='The total number of jobs to divide things into') parser.add_option( '-t', '--test', action="store_true", default=False, dest='test', help='Just run a smaller version of the sampling to test things.') return parser parser=_create_parser() (options, args) = parser.parse_args() def get_sample_parameters(): """Return the index and the number of parts to divide the job in to.""" if options.test: if options.job_index or options.num_jobs: parser.error("--test and -j or -n cannot be specified at " "the same time.") return (0, 1000) return (int(options.job_index),int(options.num_jobs)) def get_is_test(): """Return whether this run is called with --test. Scripts that use this can't don't expect -j and -n and so an error will be thrown if they are passed.""" if options.job_index or options.num_jobs: parser.error("This sampling script cannot be broken up into jobs. " "Only --test is supported.") return options.test def get_input_path(name): """Get the path where the current script should find output of the last script""" id=options.input_dir if id=="auto": id= _get_default_input() return os.path.join(id, name) def get_output_path(name): """Get the path to where the current script should write output files""" od=options.output_dir if od=="auto": od= _get_default_output() if not os.path.exists(od): os.makedirs(od) return os.path.join(od, name) def get_data_input_path(name): """Get the path where the current script should find data files""" da= options.data_dir if da=="auto": da= os.path.join(os.path.split(sys.argv[0])[0], "data") return os.path.join(da, name) %}