-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackupRepos.py
More file actions
65 lines (57 loc) · 1.9 KB
/
backupRepos.py
File metadata and controls
65 lines (57 loc) · 1.9 KB
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
import urllib.request
import base64
import json
import io
import os
import sys
# personal public access token
USER=''
TOKEN=''
GIT_API_URL='https://api.github.com'
def main(argv):
if len(argv) > 1:
user=argv[1]
else:
user=input("enter username: ").strip()
if not user:
print("username can't be null")
sys.exit(1)
print("\n================================")
print("list public repos for the user: %s" % user)
repos=getRepos(user)
for name,url in repos:
print("name: %-30s url: %s" % (name,url))
print("================================\n")
input("press enter to clone or update all repos...")
cloneOrUpdateRepos(repos)
def cloneOrUpdateRepos(repos):
for name,url in repos:
print("\n================================")
if os.path.exists(name):
print("repo: %s has exists, will update" % name)
os.system("cd %s & git pull" % name)
else:
print("repo: %s not exists, will clone" % name)
os.system("git clone %s" % url)
print("================================\n")
def getRepos(user):
resp = getApi('/users/%s/repos' % user)
return [(x["name"],x["clone_url"]) for x in json.load(io.StringIO(resp))]
def getApi(url):
try:
req=urllib.request.Request(GIT_API_URL+url)
if USER and TOKEN:
print("authorization with %s-%s" % (USER, TOKEN))
b64str=base64.encodestring(bytes('%s/token:%s' % (USER, TOKEN), 'utf-8')).decode('utf-8').replace('\n', '')
req.add_header("Authorization", "Basic %s" % b64str)
resp=urllib.request.urlopen(req)
cnt=resp.read().decode('utf-8')
resp.close()
return cnt
except:
print('failed to get api request from %s' % url)
if __name__ == "__main__":
try:
main(sys.argv)
except KeyboardInterrupt:
sys.exit(2)