Exemplo n.º 1
0
 def delete(self, name):
     """
     Given a name, delete the group. Idempotent.
     :param name: the group name to delete.
     :type name: str
     :returns: The deletion status.
     :rtype: bool
     """
     #1. Remove the conf file
     path = Path(
         os.path.join(self.path, 'conf', 'groups', '{}.conf'.format(name)))
     if not path.exists():  #Already exist
         return False
     path.remove()
     #2. Remove it from the repos file.
     for repo in Path(self.path, 'conf', 'repos').walk():
         if repo.isdir():
             continue
         with open(str(repo)) as f:
             if name in f.read():
                 Repository.get(
                     os.path.splitext(os.path.basename(repo))[0], self.path,
                     self.git).replace(r'.*= *@%s\n' % name, '')
     #3. Commit
     self.git.commit([str(path)], 'Deleted group {}.'.format(name))
     return True
Exemplo n.º 2
0
 def delete(self, name):
     user = User.get_by_name(name, self.path, self.git)
     if not user:
         return
     dest = Path(self.path, 'keydir/%s' % name)
     for repo in user.repos:
         Repository.get(repo, self.path, self.git).users.remove(user.name)
     for group in user.groups:
         GroupManager(self.path).user_delete(group, user.name)
     if dest.exists():
         dest.rmtree()
         self.git.commit([str(dest)], 'Deleted user %s.' % name)
Exemplo n.º 3
0
 def repo_delete(self, group, repo):
     """
     Remove a group from a repo.
     :param group: The group on which the operation occurs.
     :type group: str, pyolite.models.Group
     :param repo: the repo to delete the group from.
     :type repo: str, pyolite.models.Repository
     :returns: The deletion status.
     :rtype: bool
     """
     #1. Check for non existing objects
     try:
         group = self.get(group)
     except ValueError:
         return False
     try:
         repo = Repository.get(repo, self.path, self.git)
     except ValueError:
         return False
     #2. Idempotency
     if group.name not in repo.objects:
         return True
     #3. Delete
     repo.replace(r'.*= *@%s\n' % group.name, '')
     commit_message = 'Group %s deleted from repo %s' % (group.name,
                                                         repo.name)
     self.git.commit(['conf'], commit_message)
     return True
Exemplo n.º 4
0
 def repo_add(self, group, repo, permission):
     """
     Add a group to a repo.
     :param group: The group on which the operation occurs.
     :type group: str, pyolite.models.Group
     :param repo: the repo to add the group.
     :type repo: str, pyolite.models.Repository
     :param permission: The group permission
     :type permission: str
     :returns: The add status.
     :rtype: bool
     """
     #1. Check for non existing objects
     try:
         group = self.get(group)
     except ValueError:
         return False
     try:
         repo = Repository.get(repo, self.path, self.git)
     except ValueError:
         return False
     #2. Check for permissions
     permission = permission.upper()
     accepted = set('RW+CD')
     if set(i for i in permission) - accepted != set([]):
         return False
     #3. Idempotency (we don't give a ** about the permission for idempotency)
     if group.name in repo.objects:
         return True
     #4. Create
     repo.write("       %s         =        @%s\n" %
                (permission, group.name))
     commit_message = 'Group %s added to repo %s' % (group.name, repo.name)
     self.git.commit(['conf'], commit_message)
     return True
Exemplo n.º 5
0
 def delete(self, lookup_repo_name):
     repo = Repository(lookup_repo_name, self.path, self.git)
     if not repo:
         return
     dest = Path(self.path, 'conf/repos/%s.conf' % lookup_repo_name)
     if dest.exists():
         dest.remove()
         self.git.commit([str(dest)], 'Deleted repo %s.' % lookup_repo_name)
Exemplo n.º 6
0
    def create(self, entity):
        repo_file = Path(self.path, 'conf/repos/%s.conf' % entity)
        if repo_file.exists():
            raise ValueError('Repository %s already exists' % entity)
        # If there are missing parent paths in the repo path, create them so we don't get IOErrors
        # In the case of a repo having names with slashes (e.g. "username/reponame")
        elif repo_file.parent != Path(""):
            repo_file.parent.mkdir(parents=True)

        repo_file.write_file("repo %s\n" % entity)

        self.git.commit([str(repo_file)], 'Created repo %s' % entity)

        return Repository(entity, self.path, self.git)
Exemplo n.º 7
0
    def all(self):
        repos = []
        repo_dir = Path(self.path, 'conf/repos')

        for obj in repo_dir.walk():
            if obj.isdir():
                continue

            files = re.compile('(\w+.conf$)').findall(str(obj))
            if files:
                repos += files

        return [
            Repository.get_by_name(repo[:-5], self.path, self.git)
            for repo in set(repos)
        ]
Exemplo n.º 8
0
 def get(self, entity):
     return Repository.get_by_name(entity, self.path, self.git)