-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathghorgsync.py
executable file
·323 lines (302 loc) · 14.8 KB
/
ghorgsync.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Module to clone and update (pull) repositories at a GitHub organization
site to local repository directories. Connects to GitHub as the current
user. Private repositories can be seen and cloned/updated if appropriate
conditions are met.
This package was developed at the NOAA's Pacific Marine Environmental Lab
(PMEL), by Karl Smith, inspired by Kenneth Reitz's GitHub Syncer.
This repository is a software product and is not official communication
of the National Oceanic and Atmospheric Administration (NOAA), or the United
States Department of Commerce (DOC). All NOAA GitHub project code is provided
on an 'as is' basis and the user assumes responsibility for its use. Any
claims against the Department of Commerce or Department of Commerce bureaus
stemming from the use of this GitHub project will be governed by all
applicable Federal law. Any reference to specific commercial products,
processes, or services by service mark, trademark, manufacturer, or
otherwise, does not constitute or imply their endorsement, recommendation
or favoring by the Department of Commerce. The Department of Commerce
seal and logo, or the seal and logo of a DOC bureau, shall not be used
in any manner to imply endorsement of any commercial product or activity
by DOC or the United States Government.
'''
from __future__ import print_function
import sys
import os
import os.path
from datetime import datetime
import json
import re
import subprocess
import traceback
import urllib2
class GHOrgSync(object):
'''
Class to clone and update (pull) repositories at an origanization's
GitHub site to local repository directories. Connects to GitHub
as the current user.
If the environment variable GITUSERTOKEN is given and its value
is not blank, authentication is added to the requests for repository
information using this value as the personal access token. If
successful, information about both private and public repositories
will be obtained. If the GITUSERTOKEN environment variable is not
given or its value is blank, only information about public repositories
will be obtained.
Uses SSH URLs (i.e., [email protected]/...) to clone the repositories.
In order to clone private repositories, the current user's GitHub
account must be configured with the user's SSH key.
See:
https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
https://help.github.com/articles/connecting-to-github-with-ssh/
'''
def __init__(self, orgname, localdir, nameregex=r'[\w\.-]+'):
'''
Clone and update (pull) acceptable repositories at the given
GitHub organization site under the given local directory.
Arguments:
orgname : (str) GitHub organization name (e.g., 'NOAA-PMEL')
localdir : (str) local directory under which the repositories
will be, or are already, cloned
nameregex : (str) regular expression to use for checking
acceptable repository names; r'[\w\.-]+' allows
one or more alphanumeric, underscore, period,
and dash characters
'''
self.__orgname = orgname;
self.__localdir = localdir
self.__nameregex = nameregex
def getrepos(self):
'''
Returns a list of information about GitHub repositories associated
with the organization specified by this instance.
If the environment variable GITUSERTOKEN is given and its value
is not blank, authentication is added to the requests for repository
information using this value as the personal access token. If
successful, information about both private and public repositories
will be returned. If the GITUSERTOKEN environment variable is not
given or its value is blank, only information about public repositories
will be returned.
Checks that the repository name matches the name regular expression.
Also checks the SSH URL is the expected URL for the organization and
repository name. An error message is printed to sys.stderr about any
repositories that were ignored.
Each entry is the list returned is a dictionary with key-value pairs:
'name' : (str) repository name; e.g., PyFerret
'private' : (bool) is this a private repository?
'sshurl' : (str) SSH URL of the repository; e.g.,
[email protected]:NOAA-PMEL/PyFerret.git
'giturl' : (str) git URL of the repository; e.g.,
git://github.com/NOAA-PMEL/PyFerret.git
'parenturl' : (str) SSH URL of the parent GitHub repository, or
an empty string if this repository is not
a fork of another GitHub repository
'''
repos = [ ]
# regex to check the URL
urlregex = re.compile('[email protected]:{org}/({regex}).git'.format(
org=self.__orgname, regex=self.__nameregex))
# authentication information
try:
token = os.getenv('GITUSERTOKEN', '').strip()
except Exception:
token = ''
# Get the info about all the repos at the organization that can be seen.
pagenum = 1
while pagenum > 0:
reposreq = urllib2.Request('https://api.github.com/orgs/{org}/repos?page={pnum}'.format(
org=self.__orgname, pnum=pagenum))
if token:
reposreq.add_header('Authorization', 'token ' + token)
conn = urllib2.urlopen(reposreq)
repolist = json.load(conn)
conn.close()
atend = True
for repo in repolist:
atend = False
name = str(repo[u'name'])
private = bool(repo[u'private'])
haswiki = bool(repo[u'has_wiki'])
sshurl = str(repo[u'ssh_url'])
giturl = str(repo[u'git_url'])
match = urlregex.match(sshurl)
if match and (match.group(1) == name):
if bool(repo[u'fork']):
inforeq = urllib2.Request('https://api.github.com/repos/{org}/{rname}'.format(
org=self.__orgname, rname=name))
if token:
inforeq.add_header('Authorization', 'token ' + token)
conn = urllib2.urlopen(inforeq)
repoinfo = json.load(conn)
conn.close()
parenturl = str(repoinfo[u'parent'][u'ssh_url'])
else:
parenturl = ''
repos.append({'name': name,
'private': private,
'haswiki': haswiki,
'sshurl': sshurl,
'giturl': giturl,
'parenturl': parenturl})
else:
timestamp = datetime.today().isoformat(' ')
if not match:
explanation = 'no match'
else:
explanation = 'mismatch'
print('{ts} :: repo ignored ({expl}) {rname} : {url}'.format(
ts=timestamp, expl=explanation, rname=name, url=sshurl), file=sys.stderr)
if atend:
pagenum = 0
else:
pagenum += 1
return repos
def syncrepo(self, repo):
'''
Create or update (pull) a local clone of the given repository.
If a problem occurs, a message is printed to sys.stderr
Arguments:
repo : (dict) repository information dictionary with key-value pairs:
'name' : (str) repository name; e.g., PyFerret
'private' : (bool) is this a private repository?
'sshurl' : (str) SSH URL of the repository; e.g.,
[email protected]:NOAA-PMEL/PyFerret.git
'giturl' : (str) git URL of the repository; e.g.,
git://github.com/NOAA-PMEL/PyFerret.git
'parenturl' : (str) SSH URL of the parent GitHub repository to set
as the upstream repository when creating the
local clone. If empty or None, or if the local
clone already exists, this value is ignored.
Returns: (bool) if successful
'''
# Get the info about the repo
name = repo['name']
private = repo['private']
sshurl = repo['sshurl']
giturl = repo['giturl']
parenturl = repo['parenturl']
# Get the local clone location for this repo
if private:
basedir = os.path.join(self.__localdir, 'private')
else:
basedir = os.path.join(self.__localdir, 'public')
clonedir = os.path.join(basedir, name)
# Deal with this repo
if not os.path.exists(clonedir):
cloneurl = sshurl
if not private:
# use Git protocol for public repos, per GitHub docs
cloneurl = giturl
# New repo - clone it
os.chdir(basedir)
retval = subprocess.call(['git', 'clone', '--quiet', cloneurl])
if retval != 0:
timestamp = datetime.today().isoformat(' ')
print('{ts} :: cannot clone repository {rname} : {url}'.format(
ts=timestamp, rname=name, url=cloneurl), file=sys.stderr)
return False
if parenturl:
# Fork - record its upstream
os.chdir(clonedir)
retval = subprocess.call(['git', 'remote', 'add', 'upstream', parenturl])
if retval != 0:
timestamp = datetime.today().isoformat(' ')
print('{ts} :: cannot add to {rname} the upstream {url}'.format(
ts=timestamp, rname=name, url=parenturl), file=sys.stderr)
return False
elif os.path.isdir(clonedir):
# Verify it is a git repo
dotgit = os.path.join(clonedir, '.git')
if os.path.isdir(dotgit):
# Existing local clone - update it (pull)
os.chdir(clonedir)
retval = subprocess.call(['git', 'pull', '--quiet'])
if retval != 0:
timestamp = datetime.today().isoformat(' ')
print('{ts} :: cannot update (pull) repository {rname}'.format(
ts=timestamp, rname=name), file=sys.stderr)
return False
else:
timestamp = datetime.today().isoformat(' ')
print('{ts} :: not a git repository: {dname}'.format(
ts=timestamp, dname=clonedir), file=sys.stderr)
return False
else:
# Not a directory
timestamp = datetime.today().isoformat(' ')
print('{ts} :: not a directory: {dname}'.format(
ts=timestamp, dname=clonedir), file=sys.stderr)
return False
# Deal with the wiki of this repo, if there is one.
if repo['haswiki']:
# Assumes wiki name is repo name with '.wiki' appended
wikiname = name + '.wiki'
wikidir = os.path.join(basedir, wikiname)
if not os.path.exists(wikidir):
wikiurl = sshurl[:-4] + '.wiki.git'
# New wiki - clone it
# Unfortunately, has_wiki only means a wiki is allowed and
# does not mean there actually is any content. If no content,
# an error message and error value is returned.
os.chdir(basedir)
devnull = open('/dev/null', 'w')
try:
retval = subprocess.call(['git', 'clone', '--quiet', wikiurl], stderr=devnull)
finally:
devnull.close()
# Ignore the error value
elif os.path.isdir(wikidir):
# Verify it is a git repo
dotgit = os.path.join(wikidir, '.git')
if os.path.isdir(dotgit):
# Existing local clone - update it (pull)
os.chdir(wikidir)
retval = subprocess.call(['git', 'pull', '--quiet'])
if retval != 0:
timestamp = datetime.today().isoformat(' ')
print('{ts} :: cannot update (pull) wiki {wname}'.format(
ts=timestamp, wname=wikiname), file=sys.stderr)
return False
else:
timestamp = datetime.today().isoformat(' ')
print('{ts} :: not a git repository: {dname}'.format(
ts=timestamp, dname=wikidir), file=sys.stderr)
return False
else:
# Not a directory
timestamp = datetime.today().isoformat(' ')
print('{ts} :: not a directory: {dname}'.format(
ts=timestamp, dname=wikidir), file=sys.stderr)
return False
return True
if __name__ == '__main__':
if len(sys.argv) != 3:
print("\n" + \
"Clones and updates (pulls) repositories at an organization's GitHub site\n" + \
"under a local directory.\n" + \
"\n" + \
" Usage: {0} orgname localdir\n".format(sys.argv[0]) + \
" where:\n" + \
" orgname is the GitHub organization name (e.g., NOAA-PMEL)\n" + \
" localdir is the full-path of the directory containing a 'private'\n" + \
" and a 'public' subdirectory which will contain the cloned\n" + \
" private and public, respectively, repository subdirectories\n" + \
"\n", file=sys.stderr)
sys.exit(1)
orgname = sys.argv[1]
localdir = sys.argv[2]
retval = 0
try:
cloner = GHOrgSync(orgname, localdir)
repos = cloner.getrepos()
if len(repos) < 1:
timestamp = datetime.today().isoformat(' ')
print('{ts} :: no repositories found for {site}'.format(ts=timestamp, site=orgname), file=sys.stderr)
sys.exit(2)
for repoinfo in repos:
if not cloner.syncrepo(repoinfo):
retval = 3
except Exception:
traceback.print_exc()
sys.exit(-1)
sys.exit(retval)