Beispiel #1
0
    def move(self, from_path, to_path):
        """
        Renames an output file and modifies the stage associated
        to reflect the change on the pipeline.

        If the output has the same name as its stage, it would
        also rename the corresponding stage file.

        E.g.
              Having: (hello, hello.dvc)

              $ dvc move hello greetings

              Result: (greeting, greeting.dvc)

        It only works with outputs generated by `add` or `import`,
        also known as data sources.
        """
        import dvc.output as Output
        from dvc.stage import Stage

        from_out = Output.loads_from(Stage(self, cwd=os.curdir),
                                     [from_path])[0]

        to_path = self._expand_target_path(from_path, to_path)

        try:
            stage, out = next((stage, out) for stage in self.stages()
                              for out in stage.outs
                              if from_out.path == out.path)
        except StopIteration:
            raise DvcException(
                "unable to find stage file with output '{path}'".format(
                    path=from_path))

        if not stage.is_data_source:
            raise MoveNotDataSourceError(stage.relpath)

        stage_name = os.path.splitext(os.path.basename(stage.path))[0]
        from_name = os.path.basename(from_out.path)
        if stage_name == from_name:
            os.unlink(stage.path)

            stage.path = os.path.join(
                os.path.dirname(to_path),
                os.path.basename(to_path) + Stage.STAGE_FILE_SUFFIX,
            )

            stage.cwd = os.path.join(self.root_dir, os.path.dirname(to_path))

        to_out = Output.loads_from(stage, [os.path.basename(to_path)],
                                   out.cache, out.metric)[0]

        with self.state:
            out.move(to_out)

        stage.dump()

        self._remind_to_git_add()
Beispiel #2
0
def move(self, from_path, to_path):
    """
    Renames an output file and modifies the stage associated
    to reflect the change on the pipeline.

    If the output has the same name as its stage, it would
    also rename the corresponding stage file.

    E.g.
          Having: (hello, hello.dvc)

          $ dvc move hello greetings

          Result: (greeting, greeting.dvc)

    It only works with outputs generated by `add` or `import`,
    also known as data sources.
    """
    import dvc.output as Output
    from dvc.stage import Stage

    from_out = Output.loads_from(Stage(self, cwd=os.curdir), [from_path])[0]

    to_path = _expand_target_path(from_path, to_path)

    outs = self.find_outs_by_path(from_out.path)
    assert len(outs) == 1
    out = outs[0]
    stage = out.stage

    if not stage.is_data_source:
        raise MoveNotDataSourceError(stage.relpath)

    stage_name = os.path.splitext(os.path.basename(stage.path))[0]
    from_name = os.path.basename(from_out.path)
    if stage_name == from_name:
        os.unlink(stage.path)

        stage.path = os.path.join(
            os.path.dirname(to_path),
            os.path.basename(to_path) + Stage.STAGE_FILE_SUFFIX,
        )

        stage.cwd = os.path.abspath(
            os.path.join(os.curdir, os.path.dirname(to_path))
        )

    to_out = Output.loads_from(
        stage, [os.path.basename(to_path)], out.cache, out.metric
    )[0]

    with self.state:
        out.move(to_out)

    stage.dump()

    self.remind_to_git_add()
Beispiel #3
0
def move(self, from_path, to_path):
    """
    Renames an output file and modifies the stage associated
    to reflect the change on the pipeline.

    If the output has the same name as its stage, it would
    also rename the corresponding DVC-file.

    E.g.
          Having: (hello, hello.dvc)

          $ dvc move hello greetings

          Result: (greeting, greeting.dvc)

    It only works with outputs generated by `add` or `import`,
    also known as data sources.
    """
    import dvc.output as Output
    from dvc.stage import Stage

    from ..dvcfile import DVC_FILE_SUFFIX, Dvcfile

    from_out = Output.loads_from(Stage(self), [from_path])[0]
    assert from_out.scheme == "local"

    to_path = _expand_target_path(from_path, to_path)

    outs = self.find_outs_by_path(from_out.fspath)
    assert len(outs) == 1
    out = outs[0]
    stage = out.stage

    if not stage.is_data_source:
        raise MoveNotDataSourceError(stage.addressing)

    stage_name = os.path.splitext(os.path.basename(stage.path))[0]
    from_name = os.path.basename(from_out.fspath)
    if stage_name == from_name:
        os.unlink(stage.path)

        stage.path = os.path.join(
            os.path.dirname(to_path),
            os.path.basename(to_path) + DVC_FILE_SUFFIX,
        )

        stage.wdir = os.path.abspath(
            os.path.join(os.curdir, os.path.dirname(to_path)))

    to_path = os.path.relpath(to_path, stage.wdir)

    to_out = Output.loads_from(stage, [to_path], out.use_cache, out.metric)[0]

    out.move(to_out)
    stage.save()

    Dvcfile(self, stage.path).dump(stage)
Beispiel #4
0
    def move(self, from_path, to_path):
        import dvc.output as Output
        from dvc.stage import Stage

        from_out = Output.loads_from(Stage(self, cwd=os.curdir),
                                     [from_path])[0]

        found = False
        self._files_to_git_add = []
        with self.state:
            for stage in self.stages():
                for out in stage.outs:
                    if out.path != from_out.path:
                        continue

                    if not stage.is_data_source:
                        raise MoveNotDataSourceError(stage.relpath)

                    found = True
                    to_out = Output.loads_from(out.stage,
                                               [to_path],
                                               out.cache,
                                               out.metric)[0]
                    out.move(to_out)

                    stage_base = os.path.basename(stage.path)
                    stage_base = stage_base.rstrip(Stage.STAGE_FILE_SUFFIX)

                    stage_dir = os.path.dirname(stage.path)
                    from_base = os.path.basename(from_path)
                    to_base = os.path.basename(to_path)
                    if stage_base == from_base:
                        os.unlink(stage.path)
                        path = to_base + Stage.STAGE_FILE_SUFFIX
                        stage.path = os.path.join(stage_dir, path)

                stage.dump()

        self._remind_to_git_add()

        if not found:
            msg = 'Unable to find dvcfile with output \'{}\''
            raise DvcException(msg.format(from_path))