-
Notifications
You must be signed in to change notification settings - Fork 3
/
github-backup-helper
executable file
·119 lines (104 loc) · 3.66 KB
/
github-backup-helper
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
#!/usr/bin/env python
# Requires: https://github.com/joeyh/github-backup
import datetime, getpass, json, os, subprocess, time, sys
try:
import configparser
except ImportError:
import ConfigParser
def expandpath(path, pwd=None):
if path is not None:
path = os.path.expandvars(os.path.expanduser(path))
if pwd is not None:
path = os.path.join(pwd, path)
return path
def mkdirs(path):
import os
try:
os.makedirs(path)
except OSError:
pass
def curl(url, username=None, password=None):
args = ["curl", "-fsLS"]
if username:
args.extend(["-u", username +
(":" + password if password else "")])
try:
result = subprocess.check_output(args + [url])
except subprocess.CalledProcessError as e:
sys.stderr.write("{0}\n".format(e))
sys.stderr.flush()
return
return json.loads(result.decode("utf8"))
def github_backup(account, username=None, password=None, env=None):
env = dict(os.environ if env is None else env)
if password:
env["GITHUB_USER"] = username or account
env["GITHUB_PASSWORD"] = password
subprocess.check_call(["github-backup", account], env=env)
def get_reset_time(resource="core", username=None, password=None):
result = curl("https://api.github.com/rate_limit",
username=username, password=password)
if not result:
sys.stderr.write("Could not get reset time.\n")
sys.stderr.flush()
return
resource = result["resources"][resource]
if resource["remaining"] > 0:
sys.stderr.write("Failure was not due to API limit.\n")
sys.stderr.flush()
return
reset_time = datetime.datetime.fromtimestamp(resource["reset"])
return reset_time
def backup_account(account, username=None, password=None, env=None):
mkdirs(account)
prev_dir = os.getcwd()
os.chdir(account)
interval = datetime.timedelta(minutes=1)
while True:
try:
github_backup(account, username, password, env)
except subprocess.CalledProcessError as e:
print("Error: " + str(e.returncode))
else:
break
reset_time = get_reset_time(username=username, password=password)
if reset_time:
next_attempt = reset_time
else:
next_attempt = datetime.datetime.now() + interval
interval *= 1.2
print("Sleeping until {0} ...".format(next_attempt))
time.sleep((next_attempt - datetime.datetime.now()).total_seconds())
os.chdir(prev_dir)
def main(prog, *args):
pwd = os.path.dirname(__file__)
config = configparser.RawConfigParser()
config.read(os.path.splitext(__file__)[0] + ".conf")
accounts = config["config"]["accounts"].split()
location = expandpath(config["config"]["location"], pwd)
home = expandpath(config["config"].get("home", None), pwd)
login = config["config"].get("login", "").split(maxsplit=1)
if len(login) == 2:
username, password = login
elif len(login) == 1:
username = login[0]
password = getpass.getpass("Password for {0}: ".format(username))
else:
username = None
password = None
env = dict(os.environ)
if home is not None:
env["HOME"] = home
if args and args[0]:
account0 = args[0]
print("Resuming on " + account0 + " ...")
accounts = accounts[accounts.index(account0):]
mkdirs(location)
os.chdir(location)
for account in accounts:
backup_account(account, username, password, env)
if __name__ == "__main__":
try:
main(*sys.argv)
except KeyboardInterrupt:
exit(2)