Skip to content

Instantly share code, notes, and snippets.

@dansondergaard
Created January 18, 2015 17:17
Show Gist options
  • Select an option

  • Save dansondergaard/a9c5c297ed76922104a5 to your computer and use it in GitHub Desktop.

Select an option

Save dansondergaard/a9c5c297ed76922104a5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import os
import os.path
import argparse
import subprocess
DEVNULL = open(os.devnull, 'wb')
class whip(object):
def __init__(self, path, assign, revoke, verbose):
self.verbose = verbose
normalized_path = self._normalize_path(path)
if not self._repo_initialized(normalized_path):
self._create_repo(normalized_path)
for user, perm in self._parse_perms(assign).items():
self._grant_perms(normalized_path, user, perm)
for user in revoke:
self._grant_perms(normalized_path, user)
def _normalize_path(self, path):
return os.path.normpath(os.path.join(os.getcwd(), path))
def _create_repo(self, path):
os.chmod(path, 0o0700)
try:
self._silent_call('git init --bare {0}'.format(path))
except subprocess.CalledProcessError:
sys.stderr.write('error: could not initialize repository at {0}\n'.format(path))
else:
if self.verbose > 0:
sys.stdout.write('> repository was initialized at {path}\n'.format(path=path))
def _grant_perms(self, path, user, perm):
try:
self._silent_call('setfacl -R -m {user}:{perm} {repo}'.format(user=user, perm=perm, repo=path))
self._silent_call('setfacl -d -R -m {user}:{perm} {repo}'.format(user=user, perm=perm, repo=path))
except subprocess.CalledProcessError:
sys.stderr.write('error: could not set permissions for user {user}\n'.format(user=user))
else:
if self.verbose > 0:
sys.stdout.write('+ {user} gained {perm} permissions on {repo}\n').format(user=user, perm=perm, repo=path)
def _revoke_perms(self, path, user):
try:
self._silent_call('setfacl -R -x {user} {repo}'.format(user=user, repo=path))
self._silent_call('setfacl -d -R -x {user} {repo}'.format(user=user, repo=path))
except subprocess.CalledProcessError:
sys.stderr.write('error: could not revoke permissions for user {user}\n'.format(user=user))
else:
if self.verbose > 0:
sys.stdout.write('- {user} lost all permissions to {repo}\n'.format(user=user, repo=path))
def _parse_perms(self, assign):
return dict(perm.split(':', 2) if ':' in perm else (perm, 'rwX') for perm in assign)
def _repo_initialized(self, path):
return os.path.exists(os.path.join(path, '.git/'))
def _silent_call(self, command):
subprocess.check_call(command, shell=True, stdout=DEVNULL, stderr=DEVNULL)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='A tool for whipping up shared repositories.')
parser.add_argument('path', default=os.getcwd(), nargs='?',
help='path to repository')
parser.add_argument('-a', '--assign', nargs='*', default=[],
help='one or more permission strings separated by spaces')
parser.add_argument('-r', '--revoke', nargs='*', default=[],
help='one or more user names separated by spaces')
parser.add_argument('-v', '--verbose', action='count', default=0,
help='how chatty to be')
args = parser.parse_args()
whip(args.path, args.assign, args.revoke, args.verbose)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment