-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.py
More file actions
executable file
·90 lines (60 loc) · 2.42 KB
/
sync.py
File metadata and controls
executable file
·90 lines (60 loc) · 2.42 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
#!/usr/bin/python3
import sys
import os.path
import configparser
import argparse
from pathlib import Path
import requests
config = configparser.ConfigParser()
config.read(os.path.expanduser("~/.screeps_sync_config"))
login = config["credentials"]["login"]
password = config["credentials"]["password"]
parser = argparse.ArgumentParser(description="Synchronize a directory with a screeps account")
parser.add_argument("-b", "--branch", action="store", dest="branch", default="default", help="Screeps branch")
parser.add_argument("-d", "--download", action="store_true", dest="download", help="Fetch file/modules from Screeps server")
parser.add_argument("-k", "--keep", action="store_true", dest="keep", help="keep previous files in dir when downloading")
parser.add_argument("path", action="store", help="local path to fetch files/modules from (or store to in the download case")
args = parser.parse_args()
def upload(path):
filelist = {f.name: f.open().read() for f in path.iterdir() if f.is_file()}
document = {
"branch": args.branch,
"modules": filelist
}
r = requests.post(
'https://screeps.com/api/user/code',
auth=(login, password),
json=document,
)
if r.status_code != 200:
sys.exit("%s: HTTP error %s\n%s" % (sys.argv[0], r.status_code, r.json()))
else:
print(r.json(), file=sys.stderr)
def download(root):
r = requests.get(
'https://screeps.com/api/user/code',
auth=(login, password),
params={"branch": args.branch},
)
if r.status_code != 200:
sys.exit("%s: HTTP error %s\n%s" % (sys.argv[0], r.status_code, r.json()))
else:
document = r.json()
if "error" in document:
sys.exit("%s: Server error: %s\nHint: check branch name" % (sys.argv[0], document["error"]))
if document["branch"] != args.branch:
sys.exit("%s: %s" % (sys.argv[0], "unknown branch %s (received branch: %s)" % (args.branch, document["branch"])))
root.mkdir(parents=True, exist_ok=True)
if not args.keep:
for file in root.iterdir():
file.unlink()
for fn, content in document["modules"].items():
path = root / fn
print("Updating", path, file=sys.stderr)
with path.open(mode="w") as f:
f.write(content)
path = Path(args.path)
if args.download:
download(path)
else:
upload(path)