Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathbackup.py
More file actions
Latest commit
103 lines (81 loc) · 2.7 KB
/
Copy pathbackup.py
File metadata and controls
103 lines (81 loc) · 2.7 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
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
importos
importre
importsys
importjson
importerrno
importargparse
importsubprocess
importurllib.parse
importrequests
defget_json(url, token):
whileTrue:
response=requests.get(
url, headers={"Authorization": "token {0}".format(token)}
)
response.raise_for_status()
yieldresponse.json()
if"next"notinresponse.links:
break
url=response.links["next"]["url"]
defcheck_name(name):
ifnotre.match(r"^\w[-\.\w]*$", name):
raiseRuntimeError("invalid name '{0}'".format(name))
returnname
defmkdir(path):
try:
os.makedirs(path, 0o770)
exceptOSErrorasose:
ifose.errno!=errno.EEXIST:
raise
returnFalse
returnTrue
defmirror(repo_name, repo_url, to_path, username, token):
parsed=urllib.parse.urlparse(repo_url)
modified=list(parsed)
modified[1] ="{username}:{token}@{netloc}".format(
username=username, token=token, netloc=parsed.netloc
)
repo_url=urllib.parse.urlunparse(modified)
repo_path=os.path.join(to_path, repo_name)
mkdir(repo_path)
# git-init manual:
# "Running git init in an existing repository is safe."
subprocess.call(["git", "init", "--bare", "--quiet"], cwd=repo_path)
# https://github.com/blog/1270-easier-builds-and-deployments-using-git-over-https-and-oauth:
# "To avoid writing tokens to disk, don't clone."
subprocess.call(
[
"git",
"fetch",
"--force",
"--prune",
"--tags",
repo_url,
"refs/heads/*:refs/heads/*",
],
cwd=repo_path,
)
defmain():
parser=argparse.ArgumentParser(description="Backup GitHub repositories")
parser.add_argument("config", metavar="CONFIG", help="a configuration file")
args=parser.parse_args()
withopen(args.config, "rb") asf:
config=json.loads(f.read())
owners=config.get("owners")
token=config["token"]
path=os.path.expanduser(config["directory"])
ifmkdir(path):
print("Created directory {0}".format(path), file=sys.stderr)
user=next(get_json("https://api.github.com/user", token))
forpageinget_json("https://api.github.com/user/repos", token):
forrepoinpage:
name=check_name(repo["name"])
owner=check_name(repo["owner"]["login"])
clone_url=repo["clone_url"]
ifownersandownernotinowners:
continue
owner_path=os.path.join(path, owner)
mkdir(owner_path)
mirror(name, clone_url, owner_path, user["login"], token)
if__name__=="__main__":
main()