# HG changeset patch # User Peter Sanchez # Date 1576862267 28800 # Fri Dec 20 09:17:47 2019 -0800 # Node ID 2baf72a7cb5aa87dbecdcdf3398b07f01a491c72 # Parent de0f6fe1cfe2b57813a89664a48790cc201d22e4 Updating repoaccess.py to support multiple users per repo diff --git a/README.md b/README.md --- a/README.md +++ b/README.md @@ -67,15 +67,16 @@ ``` $ root:code ~ # repoaccess.py -h -usage: repoaccess.py [-h] repo_type repository username perms +usage: repoaccess.py [-h] repo_type repository user_perm [user_perm ...] Give access to hg or git repositories. positional arguments: repo_type Repository type (hg or git) repository Repository name (~username/repo_name) - username Username of user you're giving access to. - perms Permissions being granted (ro or rw) + user_perm Username and permission of the user you're giving access to. You + can specify multiple users/perms at once. Permissions are: ro + (read-only), rw (read-write) i.e., username1:rw username2:ro optional arguments: -h, --help show this help message and exit diff --git a/repoaccess.py b/repoaccess.py --- a/repoaccess.py +++ b/repoaccess.py @@ -2,7 +2,7 @@ """ Simple script to give repo access to a specific user. - $ python3 repoaccess.py <~username/repo_name> + $ python3 repoaccess.py <~username/repo_name> [ ...] repo types are "hg" and "git" @@ -10,6 +10,8 @@ This needs to be run on the same system that the particular service (repo_type) is being served from. + + You can give multiple user:perm's """ import os import re @@ -26,10 +28,11 @@ access_model = None repo_model = None - def __init__(self, repo_type): + def __init__(self, repo_type, verbose=True): _db = DbSession(cfg(f'{repo_type}.sr.ht', 'connection-string')) _db.init() getattr(self, f'set_models_{repo_type}')() + self.verbose = verbose def set_models_hg(self): from hgsrht.types import User, Access, Repository @@ -58,20 +61,9 @@ name=repo_name, ).first() - def grant_access(self, repo_full, to_user, perm): - """ repo_full should be in format: ~username/repo_name - to_user is the user to give access to - """ - owner, repo = repo_full.split('/') - repo = self.get_repo(owner, repo) - if not repo: - sys.stderr.write(f'Repository {repo_full} not found.\n') - return 1 - user = self.get_user(to_user) - if not user: - sys.stderr.write(f'User {to_user} not found.\n') - return 1 - + def grant_access(self, repo, user, perm): + to_user = user.username + repo_full = '/'.join(repo.path.split('/')[-2:]) # Check for existing access access = self.access_model.query.filter_by( repo_id=repo.id, @@ -80,11 +72,16 @@ if access: # scmsrht.repos.access.AccessMode - Enum if access.mode.value == perm: - print(f'{to_user} already has {perm} access to {repo_full}.') - return 0 + if self.verbose: + print( + f'{to_user} already has {perm} access to {repo_full}.' + ) else: access.mode = getattr(AccessMode, perm) - print(f'Updated {repo_full} access for {to_user} to {perm}.') + if self.verbose: + print( + f'Updated {repo_full} access for {to_user} to {perm}.' + ) else: # Grant access grant = self.access_model() @@ -92,10 +89,29 @@ grant.user_id = user.id grant.mode = getattr(AccessMode, perm) db.session.add(grant) - print(f'Granted {perm} access to user {to_user} for {repo_full}.') + if self.verbose: + print( + f'Granted {perm} access to user {to_user} for {repo_full}.' + ) + - db.session.commit() - return 0 +def process_users(repo_helper, repo_full, users): + owner, repo = repo_full.split('/') + repo = repo_helper.get_repo(owner, repo) + if not repo: + sys.stderr.write(f'Repository {repo_full} not found.\n') + return 1 + + for _user in users: + to_user, perm = _user.split(':') + user = repo_helper.get_user(to_user) + if not user: + sys.stderr.write(f'User {to_user} not found.\n') + return 1 + repo_helper.grant_access(repo, user, perm) + + db.session.commit() + return 0 def main(): @@ -109,8 +125,8 @@ raise ValueError return value - def _perms(value): - if value not in ['ro', 'rw']: + def _user_perm(value): + if not re.search(r'^[-\w]+:ro|rw$', value): raise ValueError return value @@ -128,19 +144,20 @@ help='Repository name (~username/repo_name)', ) parser.add_argument( - 'username', - type=str, - help='Username of user you\'re giving access to.', - ) - parser.add_argument( - 'perms', - type=_perms, - help='Permissions being granted (ro or rw)', + 'user_perm', + help=( + 'Username and permission of the user you\'re giving access to. ' + 'You can specify multiple users/perms at once. ' + 'Permissions are: ro (read-only), rw (read-write) ' + 'i.e., username1:rw username2:ro' + ), + nargs='+', + type=_user_perm, ) args = parser.parse_args() repo = RepoHelper(args.repo_type) - sys.exit(repo.grant_access(args.repository, args.username, args.perms)) + sys.exit(process_users(repo, args.repository, args.user_perm)) if __name__ == '__main__':