|
| 1 | +import utils as utl |
| 2 | +import importlib |
| 3 | +from os.path import dirname,splitext,basename,join |
| 4 | +import state |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +class ArtifactError(Exception): |
| 8 | + """Custom exception for artifact management errors.""" |
| 9 | + pass |
| 10 | + |
| 11 | +def log_job(stage_name, job_name,start): |
| 12 | + stop = datetime.now() |
| 13 | + state.pipe.append({ |
| 14 | + "stage":stage_name, |
| 15 | + "job":job_name, |
| 16 | + "start":str(start), |
| 17 | + "stop":str(stop), |
| 18 | + "duration": str(stop-start), |
| 19 | + "duration_text": utl.duration_text(stop-start) |
| 20 | + }) |
| 21 | + return |
| 22 | + |
| 23 | +def set_artifact(data,filepath,type="generic"): |
| 24 | + id,ext = splitext(basename(filepath)) |
| 25 | + if(id in state.artifacts): |
| 26 | + raise ArtifactError(f"Artifact with ID '{id}' already exists.") |
| 27 | + path = dirname(filepath) |
| 28 | + state.artifacts[id] = { |
| 29 | + "path":path, |
| 30 | + "ext":ext, |
| 31 | + "type":type, |
| 32 | + "filepath":filepath |
| 33 | + } |
| 34 | + abs_filepath = join("cache",filepath) |
| 35 | + if(ext == ".json"): |
| 36 | + utl.save_json(data,abs_filepath) |
| 37 | + return |
| 38 | + |
| 39 | +def get_artifact(id): |
| 40 | + if(id not in state.artifacts): |
| 41 | + raise ArtifactError(f"Artifact with ID '{id}' does not exist") |
| 42 | + artifact = state.artifacts[id] |
| 43 | + if(artifact["ext"] == ".json"): |
| 44 | + return utl.load_json(join("cache",artifact["filepath"])) |
| 45 | + return None |
| 46 | + |
| 47 | +def run_stage(stage_name, jobs): |
| 48 | + print(f"Running stage: {stage_name}") |
| 49 | + state.stage = stage_name |
| 50 | + for job_name, job in jobs.items(): |
| 51 | + module_name, function_name = job.split('#') |
| 52 | + state.job = job_name |
| 53 | + state.step = function_name |
| 54 | + module = importlib.import_module(module_name.replace('.py', '')) |
| 55 | + func = getattr(module, function_name) |
| 56 | + print(f" Executing job: {job_name}") |
| 57 | + start = datetime.now() |
| 58 | + func() |
| 59 | + log_job(stage_name, job_name,start) |
| 60 | + |
| 61 | +def run_pipeline(pipeline): |
| 62 | + state.value = 1 |
| 63 | + for stage, jobs in pipeline.items(): |
| 64 | + run_stage(stage, jobs) |
| 65 | + utl.save_json(state.artifacts,"cache/artifacts.json") |
| 66 | + utl.save_json(state.pipe,"cache/pipeline.json") |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == '__main__': |
| 70 | + manifest = utl.load_yaml("manifest.yaml") |
| 71 | + run_pipeline(manifest["pipeline"]) |
0 commit comments