def run(self, fname=None, no_exec=False, single_stage=False, **kwargs): from dvc.stage import PipelineStage, Stage, create_stage from dvc.dvcfile import Dvcfile, PIPELINE_FILE if not kwargs.get("cmd"): raise InvalidArgumentError("command is not specified") stage_cls = PipelineStage path = PIPELINE_FILE stage_name = kwargs.get("name") if stage_name and single_stage: raise InvalidArgumentError( "`-n|--name` is incompatible with `--single-stage`") if not stage_name and not single_stage: raise InvalidArgumentError("`-n|--name` is required") if single_stage: kwargs.pop("name", None) stage_cls = Stage path = fname or _get_file_path(kwargs) else: if not is_valid_name(stage_name): raise InvalidStageName params = parse_params(kwargs.pop("params", [])) stage = create_stage(stage_cls, repo=self, path=path, params=params, **kwargs) if stage is None: return None dvcfile = Dvcfile(self, stage.path) if dvcfile.exists(): if kwargs.get("overwrite", True): dvcfile.remove_stage(stage) elif stage_cls != PipelineStage: raise StageFileAlreadyExistsError(dvcfile.relpath) elif stage_name and stage_name in dvcfile.stages: raise DuplicateStageName(stage_name, dvcfile) try: self.check_modified_graph([stage]) except OutputDuplicationError as exc: raise OutputDuplicationError(exc.output, set(exc.stages) - {stage}) if no_exec: stage.ignore_outs() else: stage.run( no_commit=kwargs.get("no_commit", False), run_cache=kwargs.get("run_cache", True), ) dvcfile.dump(stage, update_pipeline=True, no_lock=no_exec) return stage
def remove_with_prompt(self, force=False): if not self.exists(): return msg = ("'{}' already exists. Do you wish to run the command and " "overwrite it?".format(relpath(self.path))) if not (force or prompt.confirm(msg)): raise StageFileAlreadyExistsError(self.path) self.remove()
def _check_stage_exists(dvcfile, stage): if not dvcfile.exists(): return hint = "Use '--force' to overwrite." if stage.__class__ != PipelineStage: raise StageFileAlreadyExistsError( f"'{stage.relpath}' already exists. {hint}") elif stage.name and stage.name in dvcfile.stages: raise DuplicateStageName( f"Stage '{stage.name}' already exists in '{stage.relpath}'. {hint}" )
def check_stage_exists(repo: "Repo", stage: Union["Stage", "PipelineStage"], path: str): from dvc.dvcfile import make_dvcfile from dvc.stage import PipelineStage from dvc.stage.exceptions import ( DuplicateStageName, StageFileAlreadyExistsError, ) dvcfile = make_dvcfile(repo, path) if not dvcfile.exists(): return hint = "Use '--force' to overwrite." if not isinstance(stage, PipelineStage): raise StageFileAlreadyExistsError( f"'{stage.relpath}' already exists. {hint}") elif stage.name and stage.name in dvcfile.stages: raise DuplicateStageName( f"Stage '{stage.name}' already exists in '{stage.relpath}'. {hint}" )