Beispiel #1
0
    def __rename_group(self, old, old_parent_id, new, new_parent_id):
        """
        Renames a group on filesystem
        
        :param group_name:
        """
        log.debug('renaming repos group from %s to %s', old, new)

        if new_parent_id:
            paths = Group.get(new_parent_id).full_path.split(Group.url_sep())
            new_parent_path = os.sep.join(paths)
        else:
            new_parent_path = ''

        if old_parent_id:
            paths = Group.get(old_parent_id).full_path.split(Group.url_sep())
            old_parent_path = os.sep.join(paths)
        else:
            old_parent_path = ''

        old_path = os.path.join(self.repos_path, old_parent_path, old)
        new_path = os.path.join(self.repos_path, new_parent_path, new)

        log.debug('renaming repos paths from %s to %s', old_path, new_path)

        if os.path.isdir(new_path):
            raise Exception('Was trying to rename to already '
                            'existing dir %s' % new_path)
        shutil.move(old_path, new_path)
Beispiel #2
0
    def update(self, repos_group_id, form_data):

        try:
            repos_group = Group.get(repos_group_id)
            old_path = repos_group.full_path

            #change properties
            repos_group.group_description = form_data['group_description']
            repos_group.parent_group = Group.get(form_data['group_parent_id'])
            repos_group.group_name = repos_group.get_new_name(form_data['group_name'])

            new_path = repos_group.full_path

            self.sa.add(repos_group)

            self.__rename_group(old_path, new_path)

            # we need to get all repositories from this new group and 
            # rename them accordingly to new group path
            for r in repos_group.repositories:
                r.repo_name = r.get_new_name(r.just_name)
                self.sa.add(r)

            self.sa.commit()
            return repos_group
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise
Beispiel #3
0
    def test_remove_group(self):
        sg1 = self.__make_group('deleteme')
        self.__delete_group(sg1.group_id)

        self.assertEqual(Group.get(sg1.group_id), None)
        self.assertFalse(self.__check_path('deteteme'))

        sg1 = self.__make_group('deleteme', parent_id=self.g1.group_id)
        self.__delete_group(sg1.group_id)

        self.assertEqual(Group.get(sg1.group_id), None)
        self.assertFalse(self.__check_path('test1', 'deteteme'))
Beispiel #4
0
    def __create_repo(self, repo_name, alias, new_parent_id, clone_uri=False):
        """
        makes repository on filesystem. It's group aware means it'll create
        a repository within a group, and alter the paths accordingly of
        group location

        :param repo_name:
        :param alias:
        :param parent_id:
        :param clone_uri:
        """
        from rhodecode.lib.utils import check_repo

        if new_parent_id:
            paths = Group.get(new_parent_id).full_path.split(Group.url_sep())
            new_parent_path = os.sep.join(paths)
        else:
            new_parent_path = ""

        repo_path = os.path.join(*map(lambda x: safe_str(x), [self.repos_path, new_parent_path, repo_name]))

        if check_repo(repo_path, self.repos_path):
            log.info("creating repo %s in %s @ %s", repo_name, repo_path, clone_uri)
            backend = get_backend(alias)

            backend(repo_path, create=True, src_url=clone_uri)
Beispiel #5
0
    def update(self, id):
        """PUT /repos_groups/id: Update an existing item"""
        # Forms posted to this method should contain a hidden field:
        #    <input type="hidden" name="_method" value="PUT" />
        # Or using helpers:
        #    h.form(url('repos_group', id=ID),
        #           method='put')
        # url('repos_group', id=ID)

        self.__load_defaults()
        c.repos_group = Group.get(id)

        repos_group_model = ReposGroupModel()
        repos_group_form = ReposGroupForm(edit=True,
                                          old_data=c.repos_group.get_dict(),
                                          available_groups=
                                            c.repo_groups_choices)()
        try:
            form_result = repos_group_form.to_python(dict(request.POST))
            repos_group_model.update(id, form_result)
            h.flash(_('updated repos group %s') \
                    % form_result['group_name'], category='success')
            #TODO: in futureaction_logger(, '', '', '', self.sa)
        except formencode.Invalid, errors:

            return htmlfill.render(
                render('admin/repos_groups/repos_groups_edit.html'),
                defaults=errors.value,
                errors=errors.error_dict or {},
                prefix_error=False,
                encoding="UTF-8")
Beispiel #6
0
    def update(self, repos_group_id, form_data):

        try:
            repos_group = Group.get(repos_group_id)
            old_name = repos_group.group_name
            old_parent_id = repos_group.group_parent_id

            repos_group.group_name = form_data['group_name']
            repos_group.group_description = \
                form_data['group_description']
            repos_group.group_parent_id = form_data['group_parent_id']

            self.sa.add(repos_group)

            if old_name != form_data['group_name'] or (old_parent_id !=
                                                form_data['group_parent_id']):
                self.__rename_group(old=old_name, old_parent_id=old_parent_id,
                                    new=form_data['group_name'],
                                    new_parent_id=form_data['group_parent_id'])

            self.sa.commit()
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise
Beispiel #7
0
        def validate_python(self, value, state):
            #TODO WRITE VALIDATIONS
            group_name = value.get('group_name')
            group_parent_id = int(value.get('group_parent_id') or -1)

            # slugify repo group just in case :)
            slug = repo_name_slug(group_name)

            # check for parent of self
            if edit and old_data['group_id'] == group_parent_id:
                    e_dict = {'group_parent_id':_('Cannot assign this group '
                                                  'as parent')}
                    raise formencode.Invalid('', value, state,
                                             error_dict=e_dict)

            old_gname = None
            if edit:
                old_gname = Group.get(
                            old_data.get('group_id')).group_name

            if old_gname != group_name or not edit:
                # check filesystem
                gr = Group.query().filter(Group.group_name == slug)\
                    .filter(Group.group_parent_id == group_parent_id).scalar()

                if gr:
                    e_dict = {'group_name':_('This group already exists')}
                    raise formencode.Invalid('', value, state,
                                             error_dict=e_dict)
Beispiel #8
0
    def delete(self, id):
        """DELETE /repos_groups/id: Delete an existing item"""
        # Forms posted to this method should contain a hidden field:
        #    <input type="hidden" name="_method" value="DELETE" />
        # Or using helpers:
        #    h.form(url('repos_group', id=ID),
        #           method='delete')
        # url('repos_group', id=ID)

        repos_group_model = ReposGroupModel()
        gr = Group.get(id)
        repos = gr.repositories.all()
        if repos:
            h.flash(_('This group contains %s repositores and cannot be '
                      'deleted' % len(repos)),
                    category='error')
            return redirect(url('repos_groups'))

        try:
            repos_group_model.delete(id)
            h.flash(_('removed repos group %s' % gr.group_name), category='success')
            #TODO: in future action_logger(, '', '', '', self.sa)
        except IntegrityError, e:
            if e.message.find('groups_group_parent_id_fkey'):
                log.error(traceback.format_exc())
                h.flash(_('Cannot delete this group it still contains '
                          'subgroups'),
                        category='warning')
            else:
                log.error(traceback.format_exc())
                h.flash(_('error occurred during deletion of repos '
                          'group %s' % gr.group_name), category='error')
Beispiel #9
0
 def delete(self, users_group_id):
     try:
         users_group = Group.get(users_group_id)
         self.sa.delete(users_group)
         self.__delete_group(users_group)
         self.sa.commit()
     except:
         log.error(traceback.format_exc())
         self.sa.rollback()
         raise
Beispiel #10
0
    def __load_data(self, group_id):
        """
        Load defaults settings for edit, and update

        :param group_id:
        """
        self.__load_defaults()

        repo_group = Group.get(group_id)

        data = repo_group.get_dict()

        return data
Beispiel #11
0
        def validate_python(self, value, state):
            # TODO WRITE VALIDATIONS
            group_name = value.get('group_name')
            group_parent_id = value.get('group_parent_id')

            # slugify repo group just in case :)
            slug = repo_name_slug(group_name)

            # check for parent of self
            parent_of_self = lambda: (
                old_data['group_id'] == int(group_parent_id)
                if group_parent_id else False
            )
            if edit and parent_of_self():
                    e_dict = {
                        'group_parent_id': _('Cannot assign this group as parent')
                    }
                    raise formencode.Invalid('', value, state,
                                             error_dict=e_dict)

            old_gname = None
            if edit:
                old_gname = Group.get(
                            old_data.get('group_id')).group_name

            if old_gname != group_name or not edit:

                # check group
                gr = Group.query()\
                      .filter(Group.group_name == slug)\
                      .filter(Group.group_parent_id == group_parent_id)\
                      .scalar()

                if gr:
                    e_dict = {
                        'group_name': _('This group already exists')
                    }
                    raise formencode.Invalid('', value, state,
                                             error_dict=e_dict)

                # check for same repo
                repo = Repository.query()\
                      .filter(Repository.repo_name == slug)\
                      .scalar()

                if repo:
                    e_dict = {
                        'group_name': _('Repository with this name already exists')
                    }
                    raise formencode.Invalid('', value, state,
                                             error_dict=e_dict)
Beispiel #12
0
        def to_python(self, value, state):

            repo_name = value.get('repo_name')

            slug = repo_name_slug(repo_name)
            if slug in [ADMIN_PREFIX, '']:
                e_dict = {'repo_name': _('This repository name is disallowed')}
                raise formencode.Invalid('', value, state, error_dict=e_dict)


            if value.get('repo_group'):
                gr = Group.get(value.get('repo_group'))
                group_path = gr.full_path
                # value needs to be aware of group name in order to check
                # db key This is an actual just the name to store in the
                # database
                repo_name_full = group_path + Group.url_sep() + repo_name
                
            else:
                group_path = ''
                repo_name_full = repo_name


            value['repo_name_full'] = repo_name_full
            rename = old_data.get('repo_name') != repo_name_full
            create = not edit
            if  rename or create:

                if group_path != '':
                    if RepoModel().get_by_repo_name(repo_name_full,):
                        e_dict = {'repo_name':_('This repository already '
                                                'exists in a group "%s"') %
                                  gr.group_name}
                        raise formencode.Invalid('', value, state,
                                                 error_dict=e_dict)
                elif Group.get_by_group_name(repo_name_full):
                        e_dict = {'repo_name':_('There is a group with this'
                                                ' name already "%s"') %
                                  repo_name_full}
                        raise formencode.Invalid('', value, state,
                                                 error_dict=e_dict)

                elif RepoModel().get_by_repo_name(repo_name_full):
                        e_dict = {'repo_name':_('This repository '
                                                'already exists')}
                        raise formencode.Invalid('', value, state,
                                                 error_dict=e_dict)

            return value
Beispiel #13
0
    def create(self, form_data):
        try:
            new_repos_group = Group()
            new_repos_group.group_description = form_data['group_description']
            new_repos_group.parent_group = Group.get(form_data['group_parent_id'])
            new_repos_group.group_name = new_repos_group.get_new_name(form_data['group_name'])

            self.sa.add(new_repos_group)

            self.__create_group(new_repos_group.group_name)

            self.sa.commit()
            return new_repos_group
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise
Beispiel #14
0
    def edit(self, id, format='html'):
        """GET /repos_groups/id/edit: Form to edit an existing item"""
        # url('edit_repos_group', id=ID)

        id_ = int(id)

        c.repos_group = Group.get(id_)
        defaults = self.__load_data(id_)

        # we need to exclude this group from the group list for editing
        c.repo_groups = filter(lambda x:x[0] != id_, c.repo_groups)

        return htmlfill.render(
            render('admin/repos_groups/repos_groups_edit.html'),
            defaults=defaults,
            encoding="UTF-8",
            force_defaults=False
        )
Beispiel #15
0
    def __create_group(self, group_name, parent_id):
        """
        makes repositories group on filesystem

        :param repo_name:
        :param parent_id:
        """

        if parent_id:
            paths = Group.get(parent_id).full_path.split(Group.url_sep())
            parent_path = os.sep.join(paths)
        else:
            parent_path = ''

        create_path = os.path.join(self.repos_path, parent_path, group_name)
        log.debug('creating new group in %s', create_path)

        if os.path.isdir(create_path):
            raise Exception('That directory already exists !')


        os.makedirs(create_path)
Beispiel #16
0
    def show(self, id, format='html'):
        """GET /repos_groups/id: Show a specific item"""
        # url('repos_group', id=ID)

        c.group = Group.get(id)

        if c.group:
            c.group_repos = c.group.repositories.all()
        else:
            return redirect(url('home'))

        #overwrite our cached list with current filter
        gr_filter = c.group_repos
        c.cached_repo_list = self.scm_model.get_repos(all_repos=gr_filter)

        c.repos_list = c.cached_repo_list

        c.repo_cnt = 0

        c.groups = self.sa.query(Group).order_by(Group.group_name)\
            .filter(Group.group_parent_id == id).all()

        return render('admin/repos_groups/repos_groups.html')
Beispiel #17
0
    def show(self, id, format='html'):
        """GET /repos_groups/id: Show a specific item"""
        # url('repos_group', id=ID)

        gr = c.group = Group.get(id)

        if c.group:
            c.group_repos = c.group.repositories.all()
        else:
            return redirect(url('home'))


        sortables = ['name', 'description', 'last_change', 'tip', 'owner']
        current_sort = request.GET.get('sort', 'name')
        current_sort_slug = current_sort.replace('-', '')

        if current_sort_slug not in sortables:
            c.sort_by = 'name'
            current_sort_slug = c.sort_by
        else:
            c.sort_by = current_sort
        c.sort_slug = current_sort_slug

        sort_key = current_sort_slug + '_sort'

        #overwrite our cached list with current filter
        gr_filter = c.group_repos
        c.cached_repo_list = self.scm_model.get_repos(all_repos=gr_filter)

        c.repos_list = c.cached_repo_list

        c.repo_cnt = 0

        c.groups = self.sa.query(Group).order_by(Group.group_name)\
            .filter(Group.group_parent_id == id).all()

        return render('admin/repos_groups/repos_groups.html')
Beispiel #18
0
    def __create_repo(self, repo_name, alias, new_parent_id, clone_uri=False):
        """
        makes repository on filesystem. It's group aware means it'll create
        a repository within a group, and alter the paths accordingly of
        group location

        :param repo_name:
        :param alias:
        :param parent_id:
        :param clone_uri:
        """
        from rhodecode.lib.utils import is_valid_repo,is_valid_repos_group

        if new_parent_id:
            paths = Group.get(new_parent_id).full_path.split(Group.url_sep())
            new_parent_path = os.sep.join(paths)
        else:
            new_parent_path = ''

        repo_path = os.path.join(*map(lambda x:safe_str(x),
                                [self.repos_path, new_parent_path, repo_name]))

        
        # check if this path is not a repository
        if is_valid_repo(repo_path, self.repos_path):
            raise Exception('This path %s is a valid repository' % repo_path)

        # check if this path is a group
        if is_valid_repos_group(repo_path, self.repos_path):
            raise Exception('This path %s is a valid group' % repo_path)
                
        log.info('creating repo %s in %s @ %s', repo_name, repo_path,
                 clone_uri)
        backend = get_backend(alias)

        backend(repo_path, create=True, src_url=clone_uri)
Beispiel #19
0
    def update(self, repo_name, form_data):
        try:
            cur_repo = self.get_by_repo_name(repo_name, cache=False)

            # update permissions
            for member, perm, member_type in form_data['perms_updates']:
                if member_type == 'user':
                    r2p = self.sa.query(RepoToPerm)\
                            .filter(RepoToPerm.user == User.get_by_username(member))\
                            .filter(RepoToPerm.repository == cur_repo)\
                            .one()

                    r2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.permission_name ==
                                                perm).scalar()
                    self.sa.add(r2p)
                else:
                    g2p = self.sa.query(UsersGroupRepoToPerm)\
                            .filter(UsersGroupRepoToPerm.users_group ==
                                    UsersGroup.get_by_group_name(member))\
                            .filter(UsersGroupRepoToPerm.repository ==
                                    cur_repo).one()

                    g2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.permission_name ==
                                                perm).scalar()
                    self.sa.add(g2p)

            # set new permissions
            for member, perm, member_type in form_data['perms_new']:
                if member_type == 'user':
                    r2p = RepoToPerm()
                    r2p.repository = cur_repo
                    r2p.user = User.get_by_username(member)

                    r2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.
                                                permission_name == perm)\
                                                .scalar()
                    self.sa.add(r2p)
                else:
                    g2p = UsersGroupRepoToPerm()
                    g2p.repository = cur_repo
                    g2p.users_group = UsersGroup.get_by_group_name(member)
                    g2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.
                                                permission_name == perm)\
                                                .scalar()
                    self.sa.add(g2p)

            # update current repo
            for k, v in form_data.items():
                if k == 'user':
                    cur_repo.user = User.get_by_username(v)
                elif k == 'repo_name':
                    pass
                elif k == 'repo_group':
                    cur_repo.group = Group.get(v)

                else:
                    setattr(cur_repo, k, v)

            new_name = cur_repo.get_new_name(form_data['repo_name'])
            cur_repo.repo_name = new_name

            self.sa.add(cur_repo)

            if repo_name != new_name:
                # rename repository
                self.__rename_repo(old=repo_name, new=new_name)

            self.sa.commit()
            return cur_repo
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise