forked from richgilbank/git-clone-organization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_clone_org
executable file
·90 lines (71 loc) · 2.2 KB
/
git_clone_org
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
#!/usr/bin/env python2
"""
Github Org-wide repo cloner utillity.
"""
from __future__ import print_function
import argparse
import json
import os
import subprocess
import urllib2
ARGP = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawTextHelpFormatter,
)
ARGP.add_argument('org', help='Github Organization Name')
ARGP.add_argument('target', nargs='?', help='Target directory for new repos.')
ARGP.add_argument('--oauth', '-a', help='Github Personal OAuth Token.')
class OrgClone(object):
"""
Github Org-wide clone multiplexer.
"""
github_org_repos_fmt = 'https://api.github.com/orgs/{org}/repos'
headers = None
def __init__(self, org, targetpath=None, oauth_token=None, git_url=None):
super(OrgClone, self).__init__()
self.org = org
self.targetpath = (targetpath if targetpath else org)
self.oauth_token = oauth_token
self.git_url = (git_url if git_url else 'git_url')
self.headers = dict()
if self.oauth_token:
self.headers['Authorization'] = 'token: {}'.format(self.oauth_token)
def fetch_org_repos(self):
"""
Query Github api for list of repos to clone.
"""
request = urllib2.Request(
self.github_org_repos_fmt.format(org=self.org),
headers=self.headers
)
org_repos = json.loads(urllib2.urlopen(request).read())
return org_repos
def clone_all(self):
"""
Multiplex repos
"""
org_repos = self.fetch_org_repos()
for repo in org_repos:
self.clone_repo(repo)
def clone_repo(self, repo):
"""
Shell out the `git clone` command.
"""
try:
os.makedirs(self.targetpath)
print('Directory Created: `{}`'.format(self.targetpath))
except OSError:
pass
subprocess.call(
['git', 'clone', repo[self.git_url]],
cwd=self.targetpath
)
def main(argp=None):
"""
CLI Main
"""
if argp is None:
argp = ARGP.parse_args()
exit(OrgClone(argp.org, argp.target, argp.oauth).clone_all())
if __name__ == '__main__':
main()