Created
July 17, 2015 04:04
-
-
Save benley/1421022a35b9e63e322f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Generate authorized_keys for Github users/teams/organizations.""" | |
from __future__ import print_function | |
import github | |
from pyglib import app | |
from pyglib import gflags | |
from pyglib import log | |
gflags.DEFINE_string( | |
'org', None, 'Name of Github organization.') | |
gflags.DEFINE_string( | |
'team', None, 'Limit to this team. (meaningless unless --org is set)') | |
gflags.DEFINE_spaceseplist( | |
'users', None, 'Individual users to include.') | |
gflags.DEFINE_boolean( | |
'skipmissing', False, | |
'Skip users/orgs/teams that are not found on github and continue anyway.') | |
gflags.DEFINE_string( | |
'token', None, | |
'Github auth token. If omitted, only publicly-visible' | |
' members can be included, and team memberships probably cannot be' | |
' retrieved.') | |
FLAGS = gflags.FLAGS | |
GithubException = github.GithubException | |
class MissingThingError(Exception): | |
"""Some sort of thing is not found on Github.""" | |
def __init__(self, thing): | |
Exception.__init__(self, '%s not found: %s' % (self.thingtype, thing)) | |
class UserNotFoundError(MissingThingError): | |
"""User not found!""" | |
thingtype = 'User' | |
class TeamNotFoundError(MissingThingError): | |
"""Team not found!""" | |
thingtype = 'Team' | |
class OrgNotFoundError(MissingThingError): | |
"""Organization not found!""" | |
thingtype = 'Organization' | |
def get_user_keys(users): | |
"""Get public keys for a list of github usernames.""" | |
_gh = github.Github(FLAGS.token) | |
for username in users: | |
try: | |
user = _gh.get_user(username) | |
for key in user.get_keys(): | |
yield '%s %s' % (key.key, user.login) | |
except GithubException as err: | |
if err.status == 404: | |
raise UserNotFoundError(username) | |
else: | |
raise | |
def get_org_keys(org, team=None): | |
"""Get authorized keys for a github org (and optional team restriction)""" | |
_gh = github.Github(FLAGS.token) | |
try: | |
org = _gh.get_organization(FLAGS.org) | |
except GithubException as err: | |
if err.status == 404: | |
raise OrgNotFoundError(org) | |
teamname = FLAGS.team | |
if teamname: | |
teams = org.get_teams() | |
team = [x for x in teams if x.name.lower() == FLAGS.team.lower()] | |
if not team: | |
raise TeamNotFoundError(team) | |
members = team[0].get_members() | |
else: | |
members = org.get_members() | |
for user in members: | |
username = user.login | |
for key in user.get_keys(): | |
yield '%s %s' % (key.key, username) | |
def main(_): | |
"""Main.""" | |
if FLAGS.team and not FLAGS.org: | |
log.error('--team makes no sense without --org') | |
return 1 | |
elif not (FLAGS.org or FLAGS.users): | |
log.error('Must specify --org and/or --users') | |
return 1 | |
try: | |
if FLAGS.org: | |
print('\n'.join(get_org_keys(FLAGS.org, FLAGS.team))) | |
if FLAGS.users: | |
print('\n'.join(get_user_keys(FLAGS.users))) | |
except github.GithubException as err: | |
log.error('Github error %s: %s', err.status, err.data.get('message')) | |
return 1 | |
except MissingThingError as err: | |
if not FLAGS.skipmissing: | |
log.error(err) | |
return 1 | |
log.warn('%s; skipping.', err) | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requirements.txt: