-
Notifications
You must be signed in to change notification settings - Fork 32
/
gom.py
executable file
·207 lines (163 loc) · 5.59 KB
/
gom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#! /usr/bin/python2
# coding=utf8
# gom.py -- Command line tool for managing a Github organization
import sys
from github_org import GithubOrganizationManager as OrgMgr
#
# Helper functions
#
def usage():
print"""
Usage %s ORGANIZATION ACTION [OPTION]...
ORGANIZATION is the name of the Github organization to be managed. You should
have a configuration file named 'ORGANIZATION-conf.yml' in the working
directory.
ACTIONS
c, create-teams CSV creates teams and members from the specified CSV file
p, purge-teams PREFIX delete all teams and associated repos that have a
name starting with PREFIX
delete-teams TXT deletes all teams enumerated in the specified TXT file
delete-repos TXT deletes all repos enumerated in the specified TXT file
l, list-repos prints all repositories in the organization
t, list-teams prints all teams
x, export-teams PREFIX export repositories starting with PREFIX and members
as a CSV file
"""[1:-1] % sys.argv[0]
def add_members_to_team(manager, options):
"""Adds members from a text file to a team"""
if len(options) < 2:
print "No team/member list specified!"
usage
sys.exit(1)
team_name = options[0]
user_file = options[1]
print "Fetching users from %s." % user_file
user_names = read_users_from_file(user_file)
team = manager.get_team_by_name(team_name)
manager.add_members_to_team(team, user_names)
def create_teams(manager, options):
"""Create new teams and repositories"""
if len(options) < 1:
print "No user file specified!"
usage()
sys.exit(1)
user_file = options[0]
print "Fetching users and teams from %s. This may take a while." \
% user_file
print "Failed users (if any):"
teams = manager.read_teams_from_csv(user_file)
print "Adding teams to organization"
manager.add_teams_to_org(teams)
def delete_repos(manager, options):
"""Delete repositories enumerated in the specified text file"""
if len(options) < 1:
print "No file containing repo names specified!"
usage()
sys.exit(1)
manager.delete_repos_in_file(options[0])
def delete_teams(manager, options):
"""Delete teams enumerated in the specified text file"""
if len(options) < 1:
print "No file containing team names specified!"
usage()
sys.exit(1)
manager.delete_teams_in_file(options[0])
def export_repos(manager, options):
"""
Export repos starting with the specified prefix and their contributors as a
CSV file
"""
if len(options) < 1:
print "No name prefix of teams to export specified!"
usage()
sys.exit(1)
prefix = options[0]
manager.export_repos_and_contributors(prefix)
def invite(manager, options):
"""
Invite the users specified in a text file to the organization
"""
if len(options) < 1:
print "No file containing user list specified!"
usage
sys.exit(1)
user_file = options[0]
print "Fetching users from %s." % user_file
users = read_users_from_file(user_file)
manager.invite(users)
def list_repos(manager):
"""
List repositories in the organization.
"""
repos = manager._organization.get_repos()
print "name,size,updated_at,pushed_at"
for repo in repos:
print "%s,%s,%s,%s" \
% (repo.name, repo.size, repo.updated_at, repo.pushed_at)
def list_teams(manager):
"""
List teams in the organization.
"""
teams = manager._organization.get_teams()
print "team,id,members_count,repos_count"
for team in teams:
print "%s,%s,%s,%s" % (team.name, team.id,
team.members_count, team.repos_count)
def purge_teams(manager, options):
"""
Delete teams and their repos starting with the specified prefix
"""
if len(options) < 1:
print "No name prefix of teams to delete specified!"
usage()
sys.exit(1)
prefix = options[0]
manager.delete_teams_and_repos(prefix)
def read_users_from_file(file_name):
"""
Reads user names from a file and returns them as a list of Strings
:param str file_name: the name of a file containing one username per line
:rtype: list of strings
"""
try:
with open(file_name) as user_file:
users = [name.rstrip() for name in user_file.readlines()]
except IOError:
print "Couldn't find file %s" % file_name
return users
#
# Script proper
#
if len(sys.argv) < 3:
print "Not enough arguments, expected at least 2"
usage()
sys.exit(2)
organization_name = sys.argv[1]
action = sys.argv[2]
options = sys.argv[3:]
manager = OrgMgr(organization_name)
# print "Org : %s" % manager._organization_name
# print "Action : %s" % action
# print "Options: %s" % ', '.join(options)
if action == "add-members" or action == "a":
add_members_to_team(manager, options)
elif action == "create-teams" or action == "c":
create_teams(manager, options)
elif action == "delete-repos":
delete_repos(manager, options)
elif action == "delete-teams" or action == "d":
delete_teams(manager, options)
elif action == "invite" or action == "i":
invite(manager, options)
elif action == "export-teams" or action == "x":
export_repos(manager, options)
elif action == "list-repos" or action == "l":
list_repos(manager)
elif action == "list-teams" or action == "t":
list_teams(manager)
elif action == "purge-teams" or action == "p":
purge_teams(manager, options)
else:
print "Unknown action: %s" % action
usage()
sys.exit(1)