-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_manager.py
330 lines (293 loc) · 11.2 KB
/
github_manager.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python
from github3 import login
from github3.repos.contents import Contents
from getpass import getpass
import os
import argparse
import yaml
import requests
import time
from subprocess import Popen, CalledProcessError
from rosdistro import get_distribution_file, get_index, get_index_url
class github_manager:
_gh = None
__password = None
_user = None
__distribution = None
_jenkins_prefix = 'http://lcas.lincoln.ac.uk/jenkins/'
_ros_dist = ['hydro', 'indigo']
@staticmethod
def config_argparse(parser):
parser.add_argument('--user',
help='the github user name', default=None)
parser.add_argument('--token',
help='the github user name', default=None)
# can also be used like this:
# tags = call(
# repo_path,
# ('git', 'tag', '-l', 'debian/*'),
# pipe=subprocess.PIPE)
def call(self, working_dir, command, pipe=None):
print('+ cd %s && ' % working_dir + ' '.join(command))
if not self.pretend:
process = Popen(command, stdout=pipe, stderr=pipe, cwd=working_dir)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
raise CalledProcessError(retcode, command)
if pipe:
return output
else:
return ''
def get_distro(self, distro):
if self.__distribution is None:
try:
index = get_index(get_index_url())
self.__distribution = get_distribution_file(
index,
distro
)
except:
print "failed to get data about repo %s in distribution %s" % (self.repo_name, self.distro_name)
raise
return self.__distribution
def __init__(self, args):
if args.user is not None:
self._user = args.user
while not self.__password:
self.__password = getpass(
'Password for {0}: '.format(args.user)
)
self._gh = login(args.user, password=self.__password)
return
if args.token is not None:
self._gh = login(token=args.token)
return
raise Exception('neither user name nor token succeeded')
def jenkins_job_url(self, repo_name, ros_distro):
return self._jenkins_prefix+'job/'+'devel-'+ros_distro+'-'+repo_name
def jenkins_job_exists(self, repo_name, ros_distro):
r = requests.get(self.jenkins_job_url(repo_name, ros_distro))
return r.status_code == 200
def query_orga_repos(self, organisation, filter='all'):
org = self._gh.organization(organisation)
repos = org.iter_repos(filter)
rosinstall = []
for r in repos:
entry = {'git': {'local-name': str(r.name),
'uri': str(r.html_url),
'version': str(r.default_branch)}}
rosinstall.append(entry)
return rosinstall
def search_file(self, repo, content, depth, max_depth, fname):
res = []
if isinstance(content, Contents):
return []
if isinstance(content, dict):
for n, c in content.iteritems():
if n == fname:
res.append(str(c.path))
if c.type == 'dir' and depth < max_depth:
res = res + (self.search_file(repo,
repo.contents(c.path),
depth+1, max_depth, fname))
return res
return []
def get_package_xmls_from_repo(self, repo, depth):
top_level_content = repo.contents('/')
return self.search_file(repo, top_level_content,
0, depth, 'package.xml')
def get_package_xmls(self, organisation, depth):
org = self._gh.organization(organisation)
repos = org.iter_repos('all')
res = {}
for r in repos:
print r.name
k = str(r.name)
res[k] = self.get_package_xmls_from_repo(r, depth)
print res[k]
return res
def _checkout_text_files(self, repo, files_list, dest_dir='.'):
for fname in files_list:
c = repo.contents(fname)
dn = os.path.join(dest_dir, os.path.dirname(c.path))
if not os.path.exists(dn):
os.makedirs(dn)
with open(os.path.join(dest_dir, fname), "w") as text_file:
text_file.write(c.decoded)
def generate_app_token(self,
note='github_manager',
note_url='https://github.com/strands-project/github_manager',
scopes=['user', 'repo']):
if self._user is None:
raise Exception('new tokens can only be generated'
+ 'when logged in using user/passwd credentials')
auth = self._gh.authorize(self._user,
self.__password,
scopes,
note=note,
note_url=note_url)
return auth
def checkout_package_xml(self, repo, workspace):
pxml = self.get_package_xmls_from_repo(repo, 1)
ghm._checkout_text_files(repo, pxml, workspace)
def checkout_all_package_xml(self, orga, workspace, filter='all'):
org = self._gh.organization(orga)
repos = org.iter_repos(filter)
for repo in repos:
print "checking out package.xmls from repository " + repo.name
pxml = ghm.get_package_xmls_from_repo(repo, 1)
ghm._checkout_text_files(repo, pxml,
os.path.join(workspace, repo.name))
time.sleep(0.2)
def create_repo(
self,
name,
organisation=None,
owners=None,
description=None
):
if organisation is not None:
org = self._gh.organization(organisation)
else:
org = self._gh
repo = {}
repo['name'] = name
repo['description'] = description
repo['has_issues'] = False
repo['has_wiki'] = False
repo['has_downloads'] = False
repo['private'] = False
repo = org.create_repo(
name,
description=description,
has_issues=False,
has_wiki=False,
auto_init=True
)
if organisation is not None:
team = org.create_team(
name+'_admins',
repo_names=[organisation + '/' + name],
permission='admin'
)
for o in owners:
team.add_member(o)
else:
for o in owners:
repo.add_collaborator(o)
#, 'description', 'homepage', 'private', 'has_issues','has_wiki', 'has_downloads']
def generate_html_report(self, organisation=None, filter='all'):
if organisation is not None:
org = self._gh.organization(organisation)
else:
org = self._gh
repos = org.iter_repos(filter)
out = '<html><body><table>'
for repo in repos:
out += '<tr>'
out += '<td><a href="' + str(repo.html_url) + '">' + repo.name + '</a></td>'
out += '<td>' + repo.default_branch + '</td>'
for rd in self._ros_dist:
if self.jenkins_job_exists(str(repo.name), rd):
url = str(self.jenkins_job_url(str(repo.name), rd))
out += '<td><a href="' + url + '">'
out += '<img src="'+url+'/badge/icon"></a></td>'
else:
out += '<td>---</td>'
out += '</tr>'
out += '</table></body></html>'
return out
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='generate new app token for later authentication',
epilog='(c) Marc Hanheide 2014, see https://github.com/marc-hanheide/ros_gh_mgr'
)
subparsers = parser.add_subparsers(help='commands', dest='command')
#####
gen_token_parser = subparsers.add_parser(
'gen-token',
help='generate a rosinstall output for all repositories of an organisation')
gen_token_parser.add_argument(
'--note',
default='github_manager',
help='note string for the app token. Default: github_manager')
gen_token_parser.add_argument(
'--note-url',
default='github_manager',
help='note url for the app token. Default: https://github.com/strands-project/github_manager')
gen_token_parser.add_argument(
'--scopes',
default=['user', 'repo'],
nargs='+',
help='permission scopes for the app token. Default: [\'user\', \'repo\']')
#####
rosinstall_parser = subparsers.add_parser(
'rosinstall',
help='generate a rosinstall output for all repositories of an organisation')
rosinstall_parser.add_argument(
'organisation',
help='organisation to look for')
rosinstall_parser.add_argument(
'--filter',
default='all',
help='filter repos (either "all", "public", "member", "private", "forks", "sources")')
#####
checkout_parser = subparsers.add_parser(
'package-xml',
help='checkout all package.xml in all repos of an organisation')
checkout_parser.add_argument(
'organisation',
help='organisation to look for')
checkout_parser.add_argument(
'workspace',
help='where to check out')
#####
repo_parser = subparsers.add_parser(
'create-repo',
help='checkout all package.xml in all repos of an organisation')
repo_parser.add_argument(
'--organisation',
default=None,
help='organisation to create in')
repo_parser.add_argument(
'--description', '-d',
help='description of the repository')
repo_parser.add_argument(
'--owners', '-o',
default=[],
nargs='+',
help='member with access to the repository')
repo_parser.add_argument(
'name',
help='name of repository')
#####
report_parser = subparsers.add_parser(
'report',
help='generate HTML report about repositories')
report_parser.add_argument(
'organisation',
help='organisation to look for')
github_manager.config_argparse(parser)
args = parser.parse_args()
ghm = github_manager(args)
if args.command == 'gen-token':
token = ghm.generate_app_token(
note=args.note,
note_url=args.note_url,
scopes=args.scopes)
print token.token
if args.command == 'rosinstall':
ri = ghm.query_orga_repos(args.organisation, filter=args.filter)
print yaml.dump(ri)
if args.command == 'package-xml':
ghm.checkout_all_package_xml(args.organisation, args.workspace)
if args.command == 'create-repo':
ghm.create_repo(
args.name,
args.organisation,
args.owners,
args.description)
if args.command == 'report':
report = ghm.generate_html_report(args.organisation)
print report