class Copy(Task):

    def __init__(self, source, destination):
        super(Copy, self).__init__()
        if isinstance(source, str):
            source = [source]
        self.__source = Lazy(source)
        self.__destination = Lazy(destination)

    @property
    def name(self):
        if self.__source.type() == list:
            return "Copy {}...".format(self.__source()[0])
        else:
            return "Copy {}".format(self.__source.peek())

    def process(self, progress):
        if os.path.isabs(self.__destination()):
            full_destination = self.__destination()
        else:
            full_destination = os.path.join(self._context["build_path"], self.__destination())

        for source in self.__source():
            if not os.path.isabs(source):
                source = os.path.join(self._context["build_path"], source)
            if not os.path.exists(full_destination):
                os.makedirs(full_destination)
            shutil.copy(source, full_destination)
        return True
 def __init__(self, command, fail_behaviour=Task.FailBehaviour.FAIL, environment=None, working_directory=None,
              name=None):
     super(Run, self).__init__()
     self.__command = Lazy(command)
     self.__name = name
     self.__fail_behaviour = fail_behaviour
     self.__environment = Lazy(environment)
     self.__working_directory = Lazy(working_directory)
class Run(Builder):
    def __init__(self, command, fail_behaviour=Task.FailBehaviour.FAIL, environment=None, working_directory=None,
                 name=None):
        super(Run, self).__init__()
        self.__command = Lazy(command)
        self.__name = name
        self.__fail_behaviour = fail_behaviour
        self.__environment = Lazy(environment)
        self.__working_directory = Lazy(working_directory)

    @property
    def name(self):
        if self.__name:
            return "run {}".format(self.__name)
        else:
            return "run {}".format(self.__command.peek().split()[0]).replace("\\", "/")

    def process(self, progress):
        if "build_path" not in self._context:
            logging.error("source path not known for {},"
                          " are you missing a matching retrieval script?".format(self.name))

        soutpath = os.path.join(self._context["build_path"], "stdout.log")
        serrpath = os.path.join(self._context["build_path"], "stderr.log")
        with open(soutpath, "w") as sout:
            with open(serrpath, "w") as serr:
                environment = dict(self.__environment()
                                   if self.__environment() is not None
                                   else config["__environment"])
                cwd = str(self.__working_directory()
                          if self.__working_directory() is not None
                          else self._context["build_path"])

                sout.write("running {} in {}".format(self.__command(), cwd))
                proc = Popen(self.__command(),
                             env=environment,
                             cwd=cwd,
                             shell=True,
                             stdout=sout, stderr=serr)
                proc.communicate()
                if proc.returncode != 0:
                    logging.error("failed to run %s (returncode %s), see %s and %s",
                                  self.__command(), proc.returncode, soutpath, serrpath)
                    return False

        return True
 def __init__(self, source, destination):
     super(Copy, self).__init__()
     if isinstance(source, str):
         source = [source]
     self.__source = Lazy(source)
     self.__destination = Lazy(destination)