Example #1
0
 def allow_push(self):
     hgrc_file = hg_util.get_hgrc_path(self.repo_path())
     with open(hgrc_file) as fh:
         for line in fh.read().splitlines():
             if line.startswith('allow_push = '):
                 return line[len('allow_push = '):]
     return ''
Example #2
0
 def set_allow_push(self, usernames, remove_auth=''):
     allow_push = util.listify(self.allow_push())
     if remove_auth:
         allow_push.remove(remove_auth)
     else:
         for username in util.listify(usernames):
             if username not in allow_push:
                 allow_push.append(username)
     allow_push = '%s\n' % ','.join(allow_push)
     # Why doesn't the following work?
     # repo.ui.setconfig('web', 'allow_push', allow_push)
     repo_dir = self.repo_path()
     hgrc_file = hg_util.get_hgrc_path(repo_dir)
     with open(hgrc_file) as fh:
         lines = fh.readlines()
     with open(hgrc_file, 'w') as fh:
         for line in lines:
             if line.startswith('allow_push'):
                 fh.write('allow_push = %s' % allow_push)
             else:
                 fh.write(line)
Example #3
0
def update_repository(app, trans, id, **kwds):
    """Update an existing ToolShed repository"""
    message = None
    flush_needed = False
    sa_session = app.model.context.current
    repository = sa_session.query(app.model.Repository).get(
        app.security.decode_id(id))
    if repository is None:
        return None, "Unknown repository ID"

    if not (trans.user_is_admin
            or trans.app.security_agent.user_can_administer_repository(
                trans.user, repository)):
        message = "You are not the owner of this repository, so you cannot administer it."
        return None, message

    # Allowlist properties that can be changed via this method
    for key in ('type', 'description', 'long_description',
                'remote_repository_url', 'homepage_url'):
        # If that key is available, not None and different than what's in the model
        if key in kwds and kwds[key] is not None and kwds[key] != getattr(
                repository, key):
            setattr(repository, key, kwds[key])
            flush_needed = True

    if 'category_ids' in kwds and isinstance(kwds['category_ids'], list):
        # Get existing category associations
        category_associations = sa_session.query(app.model.RepositoryCategoryAssociation) \
                                          .filter(app.model.RepositoryCategoryAssociation.table.c.repository_id == app.security.decode_id(id))
        # Remove all of them
        for rca in category_associations:
            sa_session.delete(rca)

        # Then (re)create category associations
        for category_id in kwds['category_ids']:
            category = sa_session.query(app.model.Category) \
                                 .get(app.security.decode_id(category_id))
            if category:
                rca = app.model.RepositoryCategoryAssociation(
                    repository, category)
                sa_session.add(rca)
            else:
                pass
        flush_needed = True

    # However some properties are special, like 'name'
    if 'name' in kwds and kwds[
            'name'] is not None and repository.name != kwds['name']:
        if repository.times_downloaded != 0:
            message = "Repository names cannot be changed if the repository has been cloned."
        else:
            message = validate_repository_name(trans.app, kwds['name'],
                                               trans.user)
        if message:
            return None, message

        repo_dir = repository.repo_path(app)
        # Change the entry in the hgweb.config file for the repository.
        old_lhs = f"repos/{repository.user.username}/{repository.name}"
        new_lhs = "repos/{}/{}".format(repository.user.username, kwds['name'])
        trans.app.hgweb_config_manager.change_entry(old_lhs, new_lhs, repo_dir)

        # Change the entry in the repository's hgrc file.
        hgrc_file = get_hgrc_path(repo_dir)
        change_repository_name_in_hgrc_file(hgrc_file, kwds['name'])

        # Rename the repository's admin role to match the new repository name.
        repository_admin_role = repository.admin_role
        repository_admin_role.name = get_repository_admin_role_name(
            str(kwds['name']), str(repository.user.username))
        trans.sa_session.add(repository_admin_role)
        repository.name = kwds['name']
        flush_needed = True

    if flush_needed:
        trans.sa_session.add(repository)
        trans.sa_session.flush()
        message = "The repository information has been updated."
    else:
        message = None
    return repository, message