-
Notifications
You must be signed in to change notification settings - Fork 6
/
git_update_all.py
249 lines (169 loc) · 7.02 KB
/
git_update_all.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
## USAGE
# Before running this
# ...
# Command line
# python git_update_all.py
import getpass
import glob
import json
import os
import re
import requests
from urlparse import urljoin
from subprocess import call
SCRIPT_VERSION = "0.0.1"
GITHUB_API = "https://api.github.com"
path = ""
username = ""
password = ""
def userInput():
global path
global username
global password
path = raw_input("[git_update_all] > Environment path: ")
username = raw_input("[git_update_all] > Github username: ")
password = getpass.getpass("[git_update_all] > Github password: ")
def buildBaseDirectory(path):
print "[git_update_all] BUILD BASE DIRECTORY"
path = os.path.abspath(path)
os.chdir(path)
baseDirectory = os.path.join(path, "dev")
if not os.path.exists(baseDirectory):
os.makedirs(baseDirectory)
return baseDirectory
def buildUserDirectory(baseDirectory):
print "[git_update_all] BUILD USER DIRECTORY"
os.chdir(baseDirectory)
userDirectory = os.path.join(baseDirectory, username)
if not os.path.exists(userDirectory):
os.makedirs(userDirectory)
return userDirectory
def buildOrganizationDirectory(baseDirectory, organization):
print "[git_update_all] BUILD ORGANIZATION DIRECTORY (%s)" % organization
os.chdir(baseDirectory)
organizationDirectory = os.path.join(baseDirectory, organization)
if not os.path.exists(organizationDirectory):
os.makedirs(organizationDirectory)
return organizationDirectory
def buildStarredDirectory(baseDirectory):
print "[git_update_all] BUILD STARRED DIRECTORY"
os.chdir(baseDirectory)
starredDirectory = os.path.join(baseDirectory, "starred")
if not os.path.exists(starredDirectory):
os.makedirs(starredDirectory)
return starredDirectory
def getAuthorization():
print "[git_update_all] GET GITHUB AUTHORIZATION"
url = urljoin(GITHUB_API, "authorizations")
payload = {"scopes": ["repo"], "note": "git_update_all.py"}
response = requests.post(url, auth=(username, password), data=json.dumps(payload))
j = json.loads(response.text)
if response.status_code == 401 and response.headers["X-GitHub-OTP"]:
code = raw_input("[git_update_all] > Github authentication code: ")
headers = {"X-GitHub-OTP": code}
response = requests.post(url, auth=(username, password), data=json.dumps(payload), headers=headers)
j = json.loads(response.text)
if response.status_code >= 400:
msg = j.get("message", "UNDEFINED ERROR (no error description from server)")
print "[!!!!!!!!!!!!!!] ERROR: %s" % msg
return
return j
def deleteAuthorization(authorization):
print "[git_update_all] DELETE GITHUB AUTHORIZATION"
payload = {"access_token": authorization["token"]}
url = authorization["url"]
response = requests.delete(url, data=json.dumps(payload))
j = json.loads(response.text)
if response.status_code >= 400:
msg = j.get("message", "UNDEFINED ERROR (no error description from server)")
print "[!!!!!!!!!!!!!!] ERROR: %s" % msg
return
def getUserRepositories(token, userDirectory, link=None):
if link is None:
print "[git_update_all] GET USER'S REPOSITORIES"
url = urljoin(GITHUB_API, "user/repos")
else:
url = link
payload = {"access_token": token}
response = requests.get(url, params=payload)
j = json.loads(response.text)
if response.status_code >= 400:
msg = j.get("message", "UNDEFINED ERROR (no error description from server)")
print "[!!!!!!!!!!!!!!] ERROR: %s" % msg
return
for repo in j:
cloneOrPullRepository(repo, userDirectory)
if "next" in response.links:
getUserRepositories(token, userDirectory, response.links["next"]["url"])
def getUserOrganizations(token):
print "[git_update_all] GET USER'S ORGANIZATIONS"
url = urljoin(GITHUB_API, "user/orgs")
payload = {"access_token": token}
response = requests.get(url, params=payload)
j = json.loads(response.text)
if response.status_code >= 400:
msg = j.get("message", "UNDEFINED ERROR (no error description from server)")
print "[!!!!!!!!!!!!!!] ERROR: %s" % msg
return
return j
def getOrganizationRepositories(token, organization, organizationDirectory, link=None):
if link is None:
print "[git_update_all] GET ORGANIZATION'S REPOSITORIES (%s)" % organization["login"]
url = urljoin(GITHUB_API, organization["repos_url"])
else:
url = link
payload = {"access_token": token}
response = requests.get(url, params=payload)
j = json.loads(response.text)
if response.status_code >= 400:
msg = j.get("message", "UNDEFINED ERROR (no error description from server)")
print "[!!!!!!!!!!!!!!] ERROR: %s" % msg
return
for repo in j:
cloneOrPullRepository(repo, organizationDirectory)
if "next" in response.links:
getOrganizationRepositories(token, organization, organizationDirectory, response.links["next"]["url"])
def getStarredRepositories(token, starredDirectory, link=None):
if link is None:
print "[git_update_all] GET USER'S STARRED REPOSITORIES"
url = urljoin(GITHUB_API, "user/starred")
else:
url = link
payload = {"access_token": token}
response = requests.get(url, params=payload)
j = json.loads(response.text)
if response.status_code >= 400:
msg = j.get("message", "UNDEFINED ERROR (no error description from server)")
print "[!!!!!!!!!!!!!!] ERROR: %s" % msg
return
for repo in j:
cloneOrPullRepository(repo, starredDirectory)
if "next" in response.links:
getStarredRepositories(token, starredDirectory, response.links["next"]["url"])
def cloneOrPullRepository(repo, currentDirectory):
print "[git_update_all] CLONE OR PULL REPOSITORY (%s)" % repo["name"]
os.chdir(currentDirectory)
repoDirectory = os.path.join(currentDirectory, repo["name"])
if os.path.exists(repoDirectory):
os.chdir(repoDirectory)
call(["git", "pull"])
else:
call(["git", "clone", repo["ssh_url"]])
def main():
print "[git_update_all] GIT UPDATE ALL"
userInput()
authorization = getAuthorization()
baseDirectory = buildBaseDirectory(path)
userDirectory = buildUserDirectory(baseDirectory)
getUserRepositories(authorization["token"], userDirectory)
organizations = getUserOrganizations(authorization["token"])
for organization in organizations:
clone = raw_input("[git_update_all] > Clone organization \"%s\" ? (y/n): " % organization["login"])
if clone == "y":
organizationDirectory = buildOrganizationDirectory(baseDirectory, organization["login"])
getOrganizationRepositories(authorization["token"], organization, organizationDirectory)
starredDirectory = buildStarredDirectory(baseDirectory)
getStarredRepositories(authorization["token"], starredDirectory)
deleteAuthorization(authorization)
if __name__ == "__main__":
main()