Ejemplo n.º 1
0
    def _create_remote(self, remote, url, remove_dir=False):
        """Create new remote

        .. py:function:: _create_remote(remote, url, remove_dir=False)

        :param str remote: Remote name
        :param str url: URL of repo
        :param Optional[bool] remove_dir: Whether to remove the directory if commands fail
        """

        remote_names = [r.name for r in self.repo.remotes]
        if remote in remote_names:
            return 0

        remote_output = fmt.remote_string(remote)
        try:
            self._print(' - Create remote ' + remote_output)
            self.repo.create_remote(remote, url)
            return 0
        except GitError as err:
            message = colored(' - Failed to create remote ', 'red')
            if remove_dir:
                remove_directory(self.repo_path)
            self._print(message + remote_output)
            self._print(fmt.error(err))
            self._exit(
                fmt.parallel_exception_error(self.repo_path, message,
                                             remote_output))
        except (KeyboardInterrupt, SystemExit):
            if remove_dir:
                remove_directory(self.repo_path)
            self._exit()
Ejemplo n.º 2
0
    def checkout(self, truncated_ref, allow_failure=False):
        """Checkout git ref

        .. py:function:: checkout(truncated_ref, allow_failure=False)

        :param str truncated_ref: Ref to git checkout
        :param Optional[bool] allow_failure: Whether to allow failing to checkout branch
        """

        ref_output = fmt.ref_string(truncated_ref)
        try:
            self._print(' - Check out ' + ref_output)
            if self._print_output:
                print(self.repo.git.checkout(truncated_ref))
                return

            self.repo.git.checkout(truncated_ref)
        except GitError as err:
            message = colored(' - Failed to checkout ', 'red')
            self._print(message + ref_output)
            if allow_failure:
                return

            self._print(fmt.error(err))
            self._exit(fmt.parallel_exception_error(self.repo_path, message, ref_output))
        except (KeyboardInterrupt, SystemExit):
            self._exit()
Ejemplo n.º 3
0
    def _checkout_new_repo_commit(self, commit, remote, depth):
        """Checkout commit or fail and delete repo if it doesn't exist

        :param str commit: Commit sha
        :param str remote: Remote name
        :param int depth: Git clone depth. 0 indicates full clone, otherwise must be a positive integer
        """

        commit_output = fmt.ref_string(commit)
        self._remote(remote, remove_dir=True)
        self.fetch(remote, depth=depth, ref=commit, remove_dir=True)

        self._print(' - Checkout commit ' + commit_output)
        try:
            self.repo.git.checkout(commit)
        except GitError as err:
            remove_directory(self.repo_path)
            message = colored(' - Failed to checkout commit ', 'red')
            self._print(message + commit_output)
            self._print(fmt.error(err))
            self._exit(
                fmt.parallel_exception_error(self.repo_path, message,
                                             commit_output))
        except (KeyboardInterrupt, SystemExit):
            remove_directory(self.repo_path)
            self._exit()
Ejemplo n.º 4
0
    def _exit(self, message='', return_code=1):
        """Exit based on serial or parallel job"""

        if self.parallel:
            raise ClowderGitError(
                msg=fmt.parallel_exception_error(self.repo_path, message))
        sys.exit(return_code)
Ejemplo n.º 5
0
    def _checkout_tag(self, tag):
        """Checkout commit tag is pointing to"""

        tag_output = fmt.ref_string(tag)
        if tag not in self.repo.tags:
            self._print(' - No existing tag ' + tag_output)
            return 1

        try:
            same_commit = self.repo.head.commit == self.repo.tags[tag].commit
            is_detached = self.repo.head.is_detached
            if same_commit and is_detached:
                self._print(' - On correct commit for tag')
                return 0
            self._print(' - Checkout tag ' + tag_output)
            self.repo.git.checkout('refs/tags/' + tag)
            return 0
        except (GitError, ValueError) as err:
            message = colored(' - Failed to checkout tag ', 'red')
            self._print(message + tag_output)
            self._print(fmt.error(err))
            self._exit(
                fmt.parallel_exception_error(self.repo_path, message,
                                             tag_output))
        except (KeyboardInterrupt, SystemExit):
            self._exit()
Ejemplo n.º 6
0
    def _create_remote(self, remote, url, remove_dir=False):
        """Create new remote"""

        remote_names = [r.name for r in self.repo.remotes]
        if remote in remote_names:
            return 0

        remote_output = fmt.remote_string(remote)
        try:
            self._print(' - Create remote ' + remote_output)
            self.repo.create_remote(remote, url)
            return 0
        except GitError as err:
            message = colored(' - Failed to create remote ', 'red')
            if remove_dir:
                remove_directory(self.repo_path)
            self._print(message + remote_output)
            self._print(fmt.error(err))
            self._exit(
                fmt.parallel_exception_error(self.repo_path, message,
                                             remote_output))
        except (KeyboardInterrupt, SystemExit):
            if remove_dir:
                remove_directory(self.repo_path)
            self._exit()
Ejemplo n.º 7
0
    def _checkout_branch_local(self, branch, remove_dir=False):
        """Checkout local branch

        .. py:function:: _checkout_branch_local(branch, remove_dir=False)

        :param str branch: Branch name
        :param Optional[bool] remove_dir: Whether to remove the directory if commands fail
        """

        branch_output = fmt.ref_string(branch)
        try:
            self._print(' - Checkout branch ' + branch_output)
            default_branch = self.repo.heads[branch]
            default_branch.checkout()
        except GitError as err:
            if remove_dir:
                remove_directory(self.repo_path)
            message = colored(' - Failed to checkout branch ', 'red')
            self._print(message + branch_output)
            self._print(fmt.error(err))
            self._exit(
                fmt.parallel_exception_error(self.repo_path, message,
                                             branch_output))
        except (KeyboardInterrupt, SystemExit):
            if remove_dir:
                remove_directory(self.repo_path)
            self._exit()
Ejemplo n.º 8
0
    def _checkout_new_repo_tag(self, tag, remote, depth, remove_dir=False):
        """Checkout tag or fail and delete repo if it doesn't exist"""

        tag_output = fmt.ref_string(tag)
        self._remote(remote, remove_dir=remove_dir)
        self.fetch(remote,
                   depth=depth,
                   ref='refs/tags/' + tag,
                   remove_dir=remove_dir)

        try:
            remote_tag = self.repo.tags[tag]
        except (GitError, IndexError):
            message = ' - No existing tag '
            if remove_dir:
                remove_directory(self.repo_path)
                self._print(colored(message, 'red') + tag_output)
                self._exit(
                    fmt.parallel_exception_error(self.repo_path,
                                                 colored(message, 'red'),
                                                 tag_output))
            if self.print_output:
                self._print(message + tag_output)
            return 1
        except (KeyboardInterrupt, SystemExit):
            if remove_dir:
                remove_directory(self.repo_path)
            self._exit()
        else:
            try:
                self._print(' - Checkout tag ' + tag_output)
                self.repo.git.checkout(remote_tag)
                return 0
            except GitError as err:
                message = colored(' - Failed to checkout tag ', 'red')
                self._print(message + tag_output)
                self._print(fmt.error(err))
                if remove_dir:
                    remove_directory(self.repo_path)
                    self._exit(
                        fmt.parallel_exception_error(self.repo_path, message,
                                                     tag_output))
                return 1
            except (KeyboardInterrupt, SystemExit):
                if remove_dir:
                    remove_directory(self.repo_path)
                self._exit()
Ejemplo n.º 9
0
    def _exit(self, message=''):
        """Exit based on serial or parallel job

        :param Optional[str] message: Error message

        Raises:
            ClowderGitError
            ClowderExit
        """

        if self.parallel:
            raise ClowderGitError(msg=fmt.parallel_exception_error(self.repo_path, message))

        raise ClowderExit(1)
Ejemplo n.º 10
0
    def checkout(self, truncated_ref):
        """Checkout git ref"""

        ref_output = fmt.ref_string(truncated_ref)
        try:
            self._print(' - Check out ' + ref_output)
            if self.print_output:
                print(self.repo.git.checkout(truncated_ref))
                return
            self.repo.git.checkout(truncated_ref)
        except GitError as err:
            message = colored(' - Failed to checkout ref ', 'red')
            self._print(message + ref_output)
            self._print(fmt.error(err))
            self._exit(
                fmt.parallel_exception_error(self.repo_path, message,
                                             ref_output))
        except (KeyboardInterrupt, SystemExit):
            self._exit()
Ejemplo n.º 11
0
    def _checkout_sha(self, sha):
        """Checkout commit by sha"""

        commit_output = fmt.ref_string(sha)
        try:
            if self.repo.head.commit.hexsha == sha:
                self._print(' - On correct commit')
                return 0
            self._print(' - Checkout commit ' + commit_output)
            self.repo.git.checkout(sha)
        except GitError as err:
            message = colored(' - Failed to checkout commit ', 'red')
            self._print(message + commit_output)
            self._print(fmt.error(err))
            self._exit(
                fmt.parallel_exception_error(self.repo_path, message,
                                             commit_output))
        except (KeyboardInterrupt, SystemExit):
            self._exit()
Ejemplo n.º 12
0
    def _checkout_new_repo_branch(self, branch, depth):
        """Checkout remote branch or fail and delete repo if it doesn't exist"""

        branch_output = fmt.ref_string(branch)
        remote_output = fmt.remote_string(self.remote)
        self._remote(self.remote, remove_dir=True)
        self.fetch(self.remote, depth=depth, ref=branch, remove_dir=True)

        if not self.existing_remote_branch(branch, self.remote):
            remove_directory(self.repo_path)
            message = colored(' - No existing remote branch ',
                              'red') + remote_output + ' ' + branch_output
            self._print(message)
            self._exit(fmt.parallel_exception_error(self.repo_path, message))

        self._create_branch_local_tracking(branch,
                                           self.remote,
                                           depth=depth,
                                           fetch=False,
                                           remove_dir=True)
Ejemplo n.º 13
0
    def _checkout_new_repo_commit(self, commit, remote, depth):
        """Checkout commit or fail and delete repo if it doesn't exist"""

        commit_output = fmt.ref_string(commit)
        self._remote(remote, remove_dir=True)
        self.fetch(remote, depth=depth, ref=commit, remove_dir=True)

        self._print(' - Checkout commit ' + commit_output)
        try:
            self.repo.git.checkout(commit)
        except GitError as err:
            remove_directory(self.repo_path)
            message = colored(' - Failed to checkout commit ', 'red')
            self._print(message + commit_output)
            self._print(fmt.error(err))
            self._exit(
                fmt.parallel_exception_error(self.repo_path, message,
                                             commit_output))
        except (KeyboardInterrupt, SystemExit):
            remove_directory(self.repo_path)
            self._exit()
Ejemplo n.º 14
0
    def _checkout_branch_local(self, branch, remove_dir=False):
        """Checkout local branch"""

        branch_output = fmt.ref_string(branch)
        try:
            self._print(' - Checkout branch ' + branch_output)
            default_branch = self.repo.heads[branch]
            default_branch.checkout()
            return 0
        except GitError as err:
            if remove_dir:
                remove_directory(self.repo_path)
            message = colored(' - Failed to checkout branch ', 'red')
            self._print(message + branch_output)
            self._print(fmt.error(err))
            self._exit(
                fmt.parallel_exception_error(self.repo_path, message,
                                             branch_output))
        except (KeyboardInterrupt, SystemExit):
            if remove_dir:
                remove_directory(self.repo_path)
            self._exit()
Ejemplo n.º 15
0
    def _get_remote_tag(self, tag, remote, depth=0, remove_dir=False):
        """Returns Tag object

        .. py:function:: _get_remote_tag(tag, remote, depth=0, remove_dir=False)

        :param str tag: Tag name
        :param str remote: Remote name
        :param Optional[int] depth: Git clone depth. 0 indicates full clone, otherwise must be a positive integer
        :param Optional[bool] remove_dir: Whether to remove the directory if commands fail
        :return: GitPython Tag object if it exists, otherwise None
        :rtype: Tag
        """

        tag_output = fmt.ref_string(tag)
        self._remote(remote, remove_dir=remove_dir)
        self.fetch(remote,
                   depth=depth,
                   ref='refs/tags/' + tag,
                   remove_dir=remove_dir)

        try:
            return self.repo.tags[tag]
        except (GitError, IndexError):
            message = ' - No existing tag '
            if remove_dir:
                remove_directory(self.repo_path)
                self._print(colored(message, 'red') + tag_output)
                self._exit(
                    fmt.parallel_exception_error(self.repo_path,
                                                 colored(message, 'red'),
                                                 tag_output))
            if self._print_output:
                self._print(message + tag_output)
            return None
        except (KeyboardInterrupt, SystemExit):
            if remove_dir:
                remove_directory(self.repo_path)
            self._exit()
Ejemplo n.º 16
0
    def _checkout_new_repo_tag(self, tag, remote, depth, remove_dir=False):
        """Checkout tag or fail and delete repo if it doesn't exist

        .. py:function:: _checkout_new_repo_tag(tag, remote, depth, remove_dir=False)

        :param str tag: Tag name
        :param str remote: Remote name
        :param int depth: Git clone depth. 0 indicates full clone, otherwise must be a positive integer
        :param Optional[bool] remove_dir: Whether to remove the directory if commands fail
        """

        remote_tag = self._get_remote_tag(tag,
                                          remote,
                                          depth=depth,
                                          remove_dir=remove_dir)
        if remote_tag is None:
            return 1

        tag_output = fmt.ref_string(tag)
        try:
            self._print(' - Checkout tag ' + tag_output)
            self.repo.git.checkout(remote_tag)
            return 0
        except GitError as err:
            message = colored(' - Failed to checkout tag ', 'red')
            self._print(message + tag_output)
            self._print(fmt.error(err))
            if remove_dir:
                remove_directory(self.repo_path)
                self._exit(
                    fmt.parallel_exception_error(self.repo_path, message,
                                                 tag_output))
            return 1
        except (KeyboardInterrupt, SystemExit):
            if remove_dir:
                remove_directory(self.repo_path)
            self._exit()
Ejemplo n.º 17
0
    def _checkout_new_repo_branch(self, branch, depth):
        """Checkout remote branch or fail and delete repo if it doesn't exist

        :param str branch: Branch name
        :param int depth: Git clone depth. 0 indicates full clone, otherwise must be a positive integer
        """

        branch_output = fmt.ref_string(branch)
        remote_output = fmt.remote_string(self.remote)
        self._remote(self.remote, remove_dir=True)
        self.fetch(self.remote, depth=depth, ref=branch, remove_dir=True)

        if not self.existing_remote_branch(branch, self.remote):
            remove_directory(self.repo_path)
            message = colored(' - No existing remote branch ',
                              'red') + remote_output + ' ' + branch_output
            self._print(message)
            self._exit(fmt.parallel_exception_error(self.repo_path, message))

        self._create_branch_local_tracking(branch,
                                           self.remote,
                                           depth=depth,
                                           fetch=False,
                                           remove_dir=True)