def test_dag_objects(): def end(retval): os.chdir(orig_dir) return retval print("Hello, test world!") import dag import os orig_dir = os.getcwd() os.chdir("test") # Create DAG d = dag.DAG() p = dag.Process() p.cmd = "test" p.input_files = [] p.output_files = [] p.args = "-arg1 -arg2 FILE" p.workunit_name = "test1" p2 = p p2.workunit_name = "test2" for i in [p, p2]: d.add_process(i) # Save DAG dag_filename = d.save() # Load DAG d2 = dag.load(dag_filename) # Do processes in DAGs match? procs = d2.processes if len(procs) != 2: print("Saving DAG caused process count to change.") print("Expected %d but have %d" % (2, len(d2.processes))) return end(False) for proc in procs: for attr in ["workunit_name", "cmd", "args"]: matches = False for orig_proc in [p, p2]: if getattr(proc, attr) == getattr(orig_proc, attr): matches = True break if not matches: print("Saving DAG caused process to change") print("Original 1: %s" % p) print("Original 2: %s" % p2) print("Loaded: %s" % proc) return end(False) os.unlink(dag_filename) return end(True)
def update_dag(cmd, cmd_args, dagfile=dag.DEFAULT_DAGFILE_NAME, debug=False, num_cores=None, init_file=None): """ This is the main forking function that operates on a DAG and its workunits Arguments: @param cmd: Command to run on DAG @type cmd: str @param cmd_args: List of string arguments for the specified command @type cmd_args: list @param dagfile: Optional filename for the DAG to be updated. Default: jobs.dag @type dagfile: str @param debug: Indicate whether or not debugging information is provided, including stack track if an exception is raised. Default: False @type debug: bool @param num_cores: Optional number of cores used in local multiprocessing @type num_cores: int @raise Exception: if the DAG file is missing or if the command is unknown. """ from os import path as OP import dag def needs_dagfile(cmd): return cmd not in ["help"] # If we still don't have the init file, there is a problem. if not init_file: if init_file is None: raise dag.DagException("No init file provided.") else: raise dag.DagException("Could not open init file ({0}). File not found." .format(init_file.name)) parsers = {} init_code = compile(init_file.read(), init_file.name, "exec") exec(init_code) if debug: print("Running command: %s" % cmd) # If the dag is needed (probably), load it. root_dag = None if needs_dagfile(cmd): if not OP.isfile(dagfile): raise Exception("Could not open '%s'" % dagfile) root_dag = dag.load(dagfile) root_dag.filename = dagfile if num_cores: root_dag.num_cores = num_cores print(modify_dag(root_dag, cmd, cmd_args, debug))
def test_dag_pickle(self): import os.path as OP import os from dag import DAG,Process,load # PICKLE d = DAG() d.add_process(Process("ple",['a','b'],['c','d'],"-brief")) d.add_process(Process("ple",['c','e'],['f'])) filename = d.save() self.assertTrue(OP.isfile(filename)) # UNPICKLE try: with open(filename,"rb") as file: d2 = load(file) finally: os.unlink(filename)
def result_to_dag(result_name): """ Takes a BOINC result name and returns the corresponding DAG. @param result_name: String representation of the result name. @type result_name: str @return: DAG object for the result @rtype: dag.DAG @raise dag.utils.NoDagMarkerException: if the result does not have a dag marker file. @raise dag.utils.MissingDAGFile: If the file in the dag marker is missing. """ import dag import dag.util as dag_utils import os.path as OP wuname = name_result2workunit(result_name) marker_path = dag_marker_filename(wuname) try: dagpath = marker_to_dagpath(marker_path) except IOError as ioe: raise dag_utils.NoDagMarkerException("Missing DAG marker.\n" "Error Message: %s\nFile: %s" % (ioe.strerror, marker_path)) if not OP.isfile(dagpath): raise dag.MissingDAGFile("Missing dag file '%s' listed in marker '%s'" % (dagpath, marker_path)) try: return dag.load(dagpath) except Exception as e: print("Error loading dag file '%s' listed in marker '%s'" % (dagpath, marker_path)) raise e