示例#1
0
    def get_redirect_url(self, *args, **kwargs):
        user_dir = os.path.expanduser('~')
        base_dir = os.path.join(user_dir, '.pan_cnc', 'panhandler', 'repositories')
        base_path = Path(base_dir)

        try:
            base_path.stat()

        except PermissionError:
            messages.add_message(self.request, messages.ERROR,
                                 'Could not update, Permission Denied')
            return '/panhandler/repos'

        except OSError:
            messages.add_message(self.request, messages.ERROR,
                                 'Could not update, Access Error for repository directory')
            return '/panhandler/repos'

        if not base_path.exists():
            messages.add_message(self.request, messages.ERROR,
                                 'Could not update, repositories directory does not exist')
            return '/panhandler/repos'

        err_condition = False
        updates = list()

        for d in base_path.iterdir():
            git_dir = d.joinpath('.git')

            if git_dir.exists() and git_dir.is_dir():
                msg = git_utils.update_repo(str(d))

                if 'Error' in msg:
                    print(f'Error updating Repository: {d.name}')
                    print(msg)
                    messages.add_message(self.request, messages.ERROR, f'Could not update repository {d.name}')
                    err_condition = True

                elif 'updated' in msg or 'Checked out new' in msg:
                    print(f'Updated Repository: {d.name}')
                    updates.append(d.name)
                    cnc_utils.set_long_term_cached_value(self.app_dir, f'{d.name}_detail', None, 0, 'git_repo_details')

                    # remove all python3 init touch files if there is an update
                    task_utils.python3_reset_init(str(d))

        if not err_condition:
            repos = ", ".join(updates)
            messages.add_message(self.request, messages.SUCCESS, f'Successfully Updated repositories: {repos}')

        cnc_utils.evict_cache_items_of_type(self.app_dir, 'imported_git_repos')
        snippet_utils.invalidate_snippet_caches(self.app_dir)
        return '/panhandler/repos'
示例#2
0
    def get_redirect_url(self, *args, **kwargs):
        repo_name = kwargs['repo_name']
        user_dir = os.path.expanduser('~')
        repo_dir = os.path.join(user_dir, '.pan_cnc', 'panhandler', 'repositories', repo_name)

        msg = git_utils.update_repo(repo_dir)
        if 'Error' in msg:
            level = messages.ERROR
            cnc_utils.evict_cache_items_of_type(self.app_dir, 'imported_git_repos')
        elif 'Updated' in msg:
            print('Invalidating snippet cache')
            snippet_utils.invalidate_snippet_caches(self.app_dir)
            cnc_utils.set_long_term_cached_value(self.app_dir, f'{repo_name}_detail', None, 0, 'git_repo_details')
            level = messages.INFO
        else:
            level = messages.INFO

        messages.add_message(self.request, level, msg)
        return f'/panhandler/repo_detail/{repo_name}'
示例#3
0
    def get_redirect_url(self, *args, **kwargs):
        repo_name = kwargs['repo_name']
        user_dir = os.path.expanduser('~')
        repo_dir = os.path.join(user_dir, '.pan_cnc', 'panhandler', 'repositories', repo_name)

        if not os.path.exists(repo_dir):
            messages.add_message(self.request, messages.ERROR, 'Repository directory does not exist!')
            return f'/panhandler/repo_detail/{repo_name}'

        msg = git_utils.update_repo(repo_dir)
        if 'Error' in msg:
            level = messages.ERROR
            cnc_utils.evict_cache_items_of_type(self.app_dir, 'imported_git_repos')
        elif 'Updated' in msg:
            print('Invalidating snippet cache')
            snippet_utils.invalidate_snippet_caches(self.app_dir)
            cnc_utils.set_long_term_cached_value(self.app_dir, f'{repo_name}_detail', None, 0, 'git_repo_details')
            level = messages.INFO
        else:
            level = messages.INFO

        messages.add_message(self.request, level, msg)

        debug_errors = snippet_utils.debug_snippets_in_repo(Path(repo_dir), list())
        if debug_errors:
            messages.add_message(self.request, messages.ERROR, 'Found Skillets with errors! Please open an issue on '
                                                               'this repository to help resolve this issue')
            for d in debug_errors:
                if 'err_list' in d and 'path' in d:
                    for e in d['err_list']:
                        if d['severity'] == 'warn':
                            level = messages.WARNING
                        else:
                            level = messages.ERROR

                        messages.add_message(self.request, level, f'Skillet: {d["path"]}\n\nError: {e}')

        return f'/panhandler/repo_detail/{repo_name}'
示例#4
0
    def get_redirect_url(self, *args, **kwargs):
        repo_name = kwargs['repo_name']
        # we are going to keep the snippets in the snippets dir in the panhandler app
        # get the dir where all apps are installed
        # src_dir = settings.SRC_PATH
        # get the panhandler app dir
        # panhandler_dir = os.path.join(src_dir, self.app_dir)
        # get the snippets dir under that
        # snippets_dir = os.path.join(panhandler_dir, 'snippets')
        snippets_dir = os.path.join(os.path.expanduser('~/.pan_cnc'), 'bootstrapper', 'repositories')

        repo_dir = os.path.join(snippets_dir, repo_name)

        msg = git_utils.update_repo(repo_dir)
        if 'Error' in msg:
            level = messages.ERROR
        else:
            print('Invalidating snippet cache')
            snippet_utils.invalidate_snippet_caches(self.app_dir)
            level = messages.INFO

        messages.add_message(self.request, level, msg)
        return self.next_url
示例#5
0
    def get_redirect_url(self, *args, **kwargs):
        repo_name = kwargs['repo_name']
        branch = kwargs['branch']
        user_dir = os.path.expanduser('~')
        repo_dir = os.path.join(user_dir, '.pan_cnc', 'panhandler',
                                'repositories', repo_name)

        if not os.path.exists(repo_dir):
            messages.add_message(self.request, messages.ERROR,
                                 'Repository directory does not exist!')
            return f'/panhandler/repo_detail/{repo_name}'

        msg = git_utils.update_repo(repo_dir, branch)

        if 'Error' in msg:
            level = messages.ERROR
            cnc_utils.evict_cache_items_of_type(self.app_dir,
                                                'imported_git_repos')

        elif 'updated' in msg or 'Checked out new' in msg:
            # msg updated will catch both switching branches as well as new commits
            # since this repoo has been updated, we need to ensure the caches are all in sync
            print('Invalidating snippet cache')
            snippet_utils.invalidate_snippet_caches(self.app_dir)
            git_utils.update_repo_in_cache(repo_name, repo_dir, self.app_dir)

            # remove all python3 init touch files if there is an update
            task_utils.python3_reset_init(repo_dir)

            level = messages.INFO
        else:
            level = messages.INFO

        messages.add_message(self.request, level, msg)

        # check if there are new branches available
        repo_detail = git_utils.get_repo_details(repo_name, repo_dir,
                                                 self.app_dir)
        repo_branches = git_utils.get_repo_branches_from_dir(repo_dir)
        if repo_detail['branches'] != repo_branches:
            messages.add_message(self.request, messages.INFO,
                                 'New Branches are available')
            git_utils.update_repo_in_cache(repo_name, repo_dir, self.app_dir)

        # check each snippet found for dependencies
        repos = cnc_utils.get_long_term_cached_value(self.app_dir,
                                                     'imported_repositories')
        loaded_skillets = snippet_utils.load_snippets_of_type_from_dir(
            self.app_dir, repo_dir)
        for skillet in loaded_skillets:
            for depends in skillet['depends']:
                url = depends.get('url', None)
                branch = depends.get('branch', 'master')

                # now check each repo to see if we already have it, add an error if not
                found = False

                for repo in repos:
                    if repo['url'] == url and repo['branch'] == branch:
                        found = True
                        break

                if not found:
                    messages.add_message(
                        self.request, messages.ERROR,
                        f'Unresolved Dependency found!! Please ensure you import the following'
                        f' repository: {url} with branch: {branch}')

        debug_errors = snippet_utils.debug_snippets_in_repo(
            Path(repo_dir), list())

        if debug_errors:
            messages.add_message(
                self.request, messages.ERROR,
                'Found Skillets with errors! Please open an issue on '
                'this repository to help resolve this issue')
            for d in debug_errors:
                if 'err_list' in d and 'path' in d:
                    for e in d['err_list']:
                        if d['severity'] == 'warn':
                            level = messages.WARNING

                        else:
                            level = messages.ERROR

                        messages.add_message(
                            self.request, level,
                            f'Skillet: {d["path"]}\n\nError: {e}')

        return f'/panhandler/repo_detail/{repo_name}'