-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGH_backup.py
145 lines (107 loc) · 4.3 KB
/
GH_backup.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
import os
import sys
import json
import time
import subprocess
import pycurl
def token(filename='token'):
with open(filename, 'r') as f:
aux = f.read().strip()
return aux
def request(url, multipage=True):
# Request information to GitHub API
dump = []
page = 1
while True:
c = pycurl.Curl()
c.setopt(c.USERPWD, ':'.join([__user__, __token__]))
c.setopt(c.URL, url + '&page=%d' %page if multipage else url)
# Load the output as a JSON object
aux = json.loads(c.perform_rb())
c.close()
page += 1
dump.extend(aux)
if not aux or not multipage:
break
return dump
if __name__ == '__main__':
start = time.time()
print('Session started @ %s\n' % time.strftime('%a %d %b %Y - %H:%M'))
__user__ = sys.argv[1]
__token__ = token(filename=sys.argv[2])
url = 'https://api.github.com/%s/repos?%s' % (sys.argv[3],
'&affiliation=owner'
if sys.argv[3] == 'user'
else '')
repos = request(url)
for repo in repos:
name = repo['name']
msg = 'Working on %s repository' % name
print(msg)
print('=' * len(msg) + '\n')
if os.path.exists(name): # Repository already cloned
print('- Directory "%s/" already exists' % name)
cloned = False
else: # Clone the repository
print('- Directory "%s/" not found' % name)
print('- Cloning repository "%s"' % name)
subprocess.call(('git clone --mirror %s %s/.git'
% (repo['ssh_url'], name)).split(' '),
stdout=sys.stdout)
cloned = True
print('>>> Entering subdirectory "%s/"' % name)
os.chdir(name)
if cloned:
subprocess.call('git config --bool core.bare false'.split(' '),
stdout=sys.stdout)
print('- Checking branches...')
# Check remote branches
branch_url = repo['branches_url'].split('{')[0]
aux = request(branch_url, multipage=False)
remote = set([n['name'] for n in aux])
if not cloned:
# Check local branches
aux = subprocess.check_output('git branch'.split(' '), encoding='utf-8')
local = set([r.strip('* ') for r in aux.strip().split('\n')])
common = remote & local
local_only = local - remote
remote_only = remote - local
#print 'REMOTE:', remote
#print 'LOCAL:', local
#print 'COMMON:', common
for branch in remote_only:
print('- Found new branch in remote: %s' % branch)
subprocess.call(('git fetch origin %s' % branch).split(' '),
stdout=sys.stdout)
for branch in local_only:
print('- Local branch %s not in remote anymore' % branch)
a = input('Do you want to delete local branch %s?\n'
% branch)
if a.lower()[0] == 'y':
print('- Deleting branch %s' % branch)
subprocess.call(('git branch -D %s' % branch).split(' '),
stdout=sys.stdout)
else:
print('- Branch %s is has been kept locally' % branch)
if cloned:
common = remote
# Always keep master last:
common = list(common)
common.remove(u'master')
common.append(u'master')
for branch in common:
print('- Checking out branch %s' % branch)
subprocess.call(('git checkout %s' % branch).split(' '),
stdout=sys.stdout)
if not cloned:
print('- Pulling branch %s' % branch)
subprocess.call(('git pull origin %s' % branch).split(' '),
stdout=sys.stdout)
print('<<< Leaving subdirectory "%s/"' % name)
os.chdir('../')
print('\n')
print('Backed up %d repositories' % len(repos))
print('Time elapsed %d sec.' %(time.time() - start))
msg = 'Backup finished!'
print('\n' + msg)
print('=' * len(msg))