Ejemplo n.º 1
0
    def run(self, command, ignore_errors, parallel=False):
        """Run command or script in project directory"""

        if not parallel:
            if not os.path.isdir(self.full_path()):
                print(colored(" - Project is missing\n", 'red'))
                return

        self._print_output = not parallel
        self._print(fmt.command(command))

        forall_env = {
            'CLOWDER_PATH': self._root_directory,
            'PROJECT_PATH': self.full_path(),
            'PROJECT_NAME': self.name,
            'PROJECT_REMOTE': self._remote,
            'PROJECT_REF': self._ref
        }

        if self.fork:
            forall_env['FORK_REMOTE'] = self.fork.remote_name

        return_code = execute_forall_command(command.split(), self.full_path(),
                                             forall_env, self._print_output)
        if not ignore_errors:
            err = fmt.command_failed_error(command)
            if return_code != 0:
                self._print(err)
                self._exit(err, return_code=return_code, parallel=parallel)
Ejemplo n.º 2
0
    def sync(self, fork_remote, rebase=False):
        """Sync fork with upstream remote

        .. py:function:: sync(fork_remote, rebase=False)

        :param str fork_remote: Fork remote name
        :param Optional[bool] rebase: Whether to use rebase instead of pulling latest changes.
        """

        self._print(' - Sync fork with upstream remote')
        if ref_type(self.default_ref) != 'branch':
            message = colored(' - Can only sync branches', 'red')
            self._print(message)
            self._exit(message)

        fork_remote_output = fmt.remote_string(fork_remote)
        branch_output = fmt.ref_string(truncate_ref(self.default_ref))
        if rebase:
            self._rebase_remote_branch(self.remote, truncate_ref(self.default_ref))
        else:
            self._pull(self.remote, truncate_ref(self.default_ref))

        self._print(' - Push to ' + fork_remote_output + ' ' + branch_output)
        command = ['git', 'push', fork_remote, truncate_ref(self.default_ref)]
        try:
            execute_command(command, self.repo_path, print_output=self._print_output)
        except ClowderError:
            message = colored(' - Failed to push to ', 'red') + fork_remote_output + ' ' + branch_output
            self._print(message)
            self._print(fmt.command_failed_error(command))
            self._exit(message)
Ejemplo n.º 3
0
    def sync(self, fork_remote, rebase=False):
        """Sync fork with upstream remote"""

        self._print(' - Sync fork with upstream remote')
        if self.ref_type(self.default_ref) != 'branch':
            message = colored(' - Can only sync branches', 'red')
            self._print(message)
            self._exit(message)
        fork_remote_output = fmt.remote_string(fork_remote)
        branch_output = fmt.ref_string(self.truncate_ref(self.default_ref))
        if rebase:
            self._rebase_remote_branch(self.remote,
                                       self.truncate_ref(self.default_ref))
        else:
            self._pull(self.remote, self.truncate_ref(self.default_ref))
        self._print(' - Push to ' + fork_remote_output + ' ' + branch_output)
        command = [
            'git', 'push', fork_remote,
            self.truncate_ref(self.default_ref)
        ]
        return_code = execute_command(command,
                                      self.repo_path,
                                      print_output=self.print_output)
        if return_code != 0:
            message = colored(' - Failed to push to ',
                              'red') + fork_remote_output + ' ' + branch_output
            self._print(message)
            self._print(fmt.command_failed_error(command))
            self._exit(message)
Ejemplo n.º 4
0
    def run_command(self, command):
        """Run command in clowder repo"""

        print(fmt.command(command))
        return_code = execute_command(command.split(), self.clowder_path)
        if return_code != 0:
            print(fmt.command_failed_error(command))
            sys.exit(return_code)
Ejemplo n.º 5
0
    def sha_branch_remote(self, remote, branch):
        """Return sha for remote branch"""

        command = "git --git-dir={0}.git rev-parse {1}/{2}".format(
            self.repo_path, remote, branch)
        return_code = execute_command(command, self.repo_path)
        if return_code != 0:
            message = colored(' - Failed to get remote sha\n',
                              'red') + fmt.command_failed_error(command)
            self._print(message)
            self._exit(message, return_code=return_code)
Ejemplo n.º 6
0
    def status_verbose(self):
        """Print git status"""

        command = 'git status -vv'
        self._print(fmt.command(command))

        return_code = execute_command(command, self.repo_path)
        if return_code != 0:
            message = colored(' - Failed to print status\n',
                              'red') + fmt.command_failed_error(command)
            self._print(message)
            self._exit(message, return_code=return_code)
Ejemplo n.º 7
0
    def status_verbose(self):
        """Print git status

        Equivalent to: ``git status -vv``
        """

        command = 'git status -vv'
        self._print(fmt.command(command))

        try:
            execute_command(command, self.repo_path)
        except ClowderError:
            message = colored(' - Failed to print status\n', 'red') + fmt.command_failed_error(command)
            self._print(message)
            self._exit(message)
Ejemplo n.º 8
0
    def sha_branch_remote(self, remote, branch):
        """Return sha for remote branch

        :param str remote: Remote name
        :param str branch: Remote branch name
        :return: Commit sha of remote branch
        :rtype: str
        """

        command = "git --git-dir={0}.git rev-parse {1}/{2}".format(self.repo_path, remote, branch)
        try:
            execute_command(command, self.repo_path)
        except ClowderError:
            message = colored(' - Failed to get remote sha\n', 'red') + fmt.command_failed_error(command)
            self._print(message)
            self._exit(message)
    def submodule_update_recursive(self, depth=0):
        """Update submodules recursively and initialize if not present"""

        print(' - Recursively update and init submodules')

        if depth == 0:
            command = ['git', 'submodule', 'update', '--init', '--recursive']
        else:
            command = [
                'git', 'submodule', 'update', '--init', '--recursive',
                '--depth', depth
            ]

        return_code = execute_command(command, self.repo_path)
        if return_code != 0:
            message = colored(' - Failed to update submodules\n',
                              'red') + fmt.command_failed_error(command)
            self._print(message)
            self._exit(message)
Ejemplo n.º 10
0
    def submodule_update_recursive(self, depth=0):
        """Update submodules recursively and initialize if not present

        .. py:function:: submodule_update_recursive(depth=0)

        :param Optional[int] depth: Git clone depth. 0 indicates full clone, otherwise must be a positive integer
        """

        self._print(' - Recursively update and init submodules')

        if depth == 0:
            command = ['git', 'submodule', 'update', '--init', '--recursive']
        else:
            command = ['git', 'submodule', 'update', '--init', '--recursive', '--depth', depth]

        try:
            execute_command(command, self.repo_path)
        except ClowderError:
            message = colored(' - Failed to update submodules\n', 'red') + fmt.command_failed_error(command)
            self._print(message)
            self._exit(message)
Ejemplo n.º 11
0
    def _rebase_remote_branch(self, remote, branch):
        """Rebase from remote branch"""

        if self.repo.head.is_detached:
            self._print(' - HEAD is detached')
            return

        branch_output = fmt.ref_string(branch)
        remote_output = fmt.remote_string(remote)
        self._print(' - Rebase onto ' + remote_output + ' ' + branch_output)
        command = ['git pull --rebase', remote, branch]

        return_code = execute_command(command,
                                      self.repo_path,
                                      print_output=self.print_output)
        if return_code != 0:
            message = colored(' - Failed to rebase onto ',
                              'red') + remote_output + ' ' + branch_output
            self._print(message)
            self._print(fmt.command_failed_error(command))
            self._exit(message)
Ejemplo n.º 12
0
    def _rebase_remote_branch(self, remote, branch):
        """Rebase onto remote branch

        :param str remote: Remote name
        :param str branch: Branch name
        """

        branch_output = fmt.ref_string(branch)
        remote_output = fmt.remote_string(remote)
        self._print(' - Rebase onto ' + remote_output + ' ' + branch_output)
        command = ['git pull --rebase', remote, branch]

        try:
            execute_command(command,
                            self.repo_path,
                            print_output=self._print_output)
        except ClowderError:
            message = colored(' - Failed to rebase onto ',
                              'red') + remote_output + ' ' + branch_output
            self._print(message)
            self._print(fmt.command_failed_error(command))
            self._exit(message)
Ejemplo n.º 13
0
    def _run_forall_command(self, command, env, ignore_errors, parallel):
        """Run command or script in project directory

        :param str command: Command to run
        :param dict env: Environment variables
        :param bool ignore_errors: Whether to exit if command returns a non-zero exit code
        :param bool parallel: Whether command is being run in parallel, affects output

        Raises:
            ClowderError
            ClowderExit
        """

        self._print(fmt.command(command))
        try:
            execute_forall_command(command, self.full_path(), env,
                                   self._print_output)
        except ClowderError:
            if not ignore_errors:
                err = fmt.command_failed_error(command)
                self._print(err)
                if parallel:
                    raise ClowderError(err)
                raise ClowderExit(1)