-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
104 lines (86 loc) · 3.22 KB
/
run.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
import os
import subprocess
import requests
import os.path as p
import datetime
import string as s
import configparser
import jinja2
import re
REPO_PATH = p.join(p.dirname(p.realpath(__file__)), "data")
try:
c = configparser.ConfigParser()
c.read(p.join(REPO_PATH, "runner.cfg"))
config = c["runner"]
except:
config = {}
announce_template = s.Template(
"""
New Unity libraries available:
$libs
View all libraries at $lib_url.
""")
def push_discord(url, color, title, msg):
if not url:
return
requests.post(url, json={
"embeds": [
{
"title": title,
"color": int(color, 16),
"description": msg,
"timestamp": datetime.datetime.utcnow().isoformat(),
}
],
})
def split_num(s):
# Split strings like
# "0a1" -> (0, "a", 1)
# "0b1" -> (0, "b", 1)
# "0b12" -> (0, "b", 12)
# "0b123" -> (0, "b", 123)
pattern = r"(?P<num>\d+)(?P<letter>[a-zA-Z])?(?P<rest>\d*)?"
m = re.match(pattern, s)
if not m:
return (0, s, 0)
num = int(m.group("num"))
letter = m.group("letter") or ""
rest = int(m.group("rest") or 0)
return (num, letter, rest)
def update_index(repo_path, new_libs):
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(repo_path),
autoescape=jinja2.select_autoescape(['html', 'xml']),
)
index_template = env.get_template("index.jinja2")
now = datetime.datetime.utcnow().strftime("%b %d, %Y at %I:%M:%S %p %Z")
libs = [p.splitext(p.basename(lib))[0] for lib in new_libs]
libs = sorted(libs, key=lambda x: split_num(x.split(".")[2])[2])
libs = sorted(libs, key=lambda x: split_num(x.split(".")[2])[1])
libs = sorted(libs, key=lambda x: split_num(x.split(".")[2])[0])
libs = sorted(libs, key=lambda x: int(x.split(".")[1]))
libs = sorted(libs, key=lambda x: int(x.split(".")[0]))
index_html = index_template.render(libs=libs, now=now)
with open(p.join(repo_path, "index.html"), "w") as f:
f.write(index_html)
try:
print(f"Started at {datetime.datetime.utcnow().isoformat()}")
repo_path = p.join(REPO_PATH, "repo")
unity_libs_path = p.join(repo_path, "libraries")
old_libs = set(os.listdir(unity_libs_path))
network = config.get("docker_network", None)
result = subprocess.run(["docker", "run", "--rm", f"--network={network}" if network else "", "-v", f"{repo_path}:/data",
"-v", f"{p.join(REPO_PATH, 'config.toml')}:/app/config.toml", "unity-miner"], check=True)
new_libs = set(os.listdir(unity_libs_path))
diff = new_libs - old_libs
if diff:
push_discord(config.get("announce_discord_webhook", None), "30b563", "New Unity Libs",
announce_template.substitute({
"libs": "\n".join([f"* {v[:-3]}" for v in sorted(diff)]),
"lib_url": config.get("listing_url", "")
}))
update_index(repo_path, new_libs)
except subprocess.CalledProcessError as ex:
if ex.returncode != -1:
push_discord(config.get("error_discord_webhook", None), "bd3d3d", "Error",
f"Unexpected error while running script: `{ex.returncode}`")