def step_12(self): self.klass.create_permissions() Session().commit() self.klass.populate_default_permissions() Session().commit() #fix all usergroups ug_model = UserGroupModel() for ug in UserGroup.get_all(): perm_obj = ug_model._create_default_perms(ug) Session().add(perm_obj) Session().commit() adm = User.get_first_admin() # fix owners of UserGroup for ug in Session().query(UserGroup).all(): ug.user_id = adm.user_id Session().add(ug) Session().commit() # fix owners of RepoGroup for ug in Session().query(RepoGroup).all(): ug.user_id = adm.user_id Session().add(ug) Session().commit()
def map_groups(path): """ Given a full path to a repository, create all nested groups that this repo is inside. This function creates parent-child relationships between groups and creates default perms for all new groups. :param paths: full path to repository """ sa = meta.Session() groups = path.split(Repository.url_sep()) parent = None group = None # last element is repo in nested groups structure groups = groups[:-1] rgm = ReposGroupModel(sa) owner = User.get_first_admin() for lvl, group_name in enumerate(groups): group_name = '/'.join(groups[:lvl] + [group_name]) group = RepoGroup.get_by_group_name(group_name) desc = '%s group' % group_name # skip folders that are now removed repos if REMOVED_REPO_PAT.match(group_name): break if group is None: log.debug('creating group level: %s group_name: %s' % (lvl, group_name)) group = RepoGroup(group_name, parent) group.group_description = desc group.user = owner sa.add(group) perm_obj = rgm._create_default_perms(group) sa.add(perm_obj) sa.flush() parent = group return group
def repo2db_mapper(initial_repo_list, remove_obsolete=False, install_git_hook=False): """ maps all repos given in initial_repo_list, non existing repositories are created, if remove_obsolete is True it also check for db entries that are not in initial_repo_list and removes them. :param initial_repo_list: list of repositories found by scanning methods :param remove_obsolete: check for obsolete entries in database :param install_git_hook: if this is True, also check and install githook for a repo if missing """ from rhodecode.model.repo import RepoModel from rhodecode.model.scm import ScmModel sa = meta.Session() rm = RepoModel() user = User.get_first_admin() added = [] ##creation defaults defs = RhodeCodeSetting.get_default_repo_settings(strip_prefix=True) enable_statistics = defs.get('repo_enable_statistics') enable_locking = defs.get('repo_enable_locking') enable_downloads = defs.get('repo_enable_downloads') private = defs.get('repo_private') for name, repo in initial_repo_list.items(): group = map_groups(name) db_repo = rm.get_by_repo_name(name) # found repo that is on filesystem not in RhodeCode database if not db_repo: log.info('repository %s not found, creating now' % name) added.append(name) desc = (repo.description if repo.description != 'unknown' else '%s repository' % name) new_repo = rm.create_repo(repo_name=name, repo_type=repo.alias, description=desc, repos_group=getattr( group, 'group_id', None), owner=user, just_db=True, enable_locking=enable_locking, enable_downloads=enable_downloads, enable_statistics=enable_statistics, private=private) # we added that repo just now, and make sure it has githook # installed if new_repo.repo_type == 'git': ScmModel().install_git_hook(new_repo.scm_instance) new_repo.update_changeset_cache() elif install_git_hook: if db_repo.repo_type == 'git': ScmModel().install_git_hook(db_repo.scm_instance) sa.commit() removed = [] if remove_obsolete: # remove from database those repositories that are not in the filesystem for repo in sa.query(Repository).all(): if repo.repo_name not in initial_repo_list.keys(): log.debug("Removing non-existing repository found in db `%s`" % repo.repo_name) try: removed.append(repo.repo_name) RepoModel(sa).delete(repo, forks='detach', fs_remove=False) sa.commit() except Exception: #don't hold further removals on error log.error(traceback.format_exc()) sa.rollback() return added, removed
def repo2db_mapper(initial_repo_list, remove_obsolete=False, install_git_hook=False): """ maps all repos given in initial_repo_list, non existing repositories are created, if remove_obsolete is True it also check for db entries that are not in initial_repo_list and removes them. :param initial_repo_list: list of repositories found by scanning methods :param remove_obsolete: check for obsolete entries in database :param install_git_hook: if this is True, also check and install githook for a repo if missing """ from rhodecode.model.repo import RepoModel from rhodecode.model.scm import ScmModel sa = meta.Session() rm = RepoModel() user = User.get_first_admin() added = [] ##creation defaults defs = RhodeCodeSetting.get_default_repo_settings(strip_prefix=True) enable_statistics = defs.get('repo_enable_statistics') enable_locking = defs.get('repo_enable_locking') enable_downloads = defs.get('repo_enable_downloads') private = defs.get('repo_private') for name, repo in initial_repo_list.items(): group = map_groups(name) db_repo = rm.get_by_repo_name(name) # found repo that is on filesystem not in RhodeCode database if not db_repo: log.info('repository %s not found, creating now' % name) added.append(name) desc = (repo.description if repo.description != 'unknown' else '%s repository' % name) new_repo = rm.create_repo( repo_name=name, repo_type=repo.alias, description=desc, repos_group=getattr(group, 'group_id', None), owner=user, just_db=True, enable_locking=enable_locking, enable_downloads=enable_downloads, enable_statistics=enable_statistics, private=private ) # we added that repo just now, and make sure it has githook # installed if new_repo.repo_type == 'git': ScmModel().install_git_hook(new_repo.scm_instance) new_repo.update_changeset_cache() elif install_git_hook: if db_repo.repo_type == 'git': ScmModel().install_git_hook(db_repo.scm_instance) sa.commit() removed = [] if remove_obsolete: # remove from database those repositories that are not in the filesystem for repo in sa.query(Repository).all(): if repo.repo_name not in initial_repo_list.keys(): log.debug("Removing non-existing repository found in db `%s`" % repo.repo_name) try: removed.append(repo.repo_name) RepoModel(sa).delete(repo, forks='detach', fs_remove=False) sa.commit() except Exception: #don't hold further removals on error log.error(traceback.format_exc()) sa.rollback() return added, removed