-
Notifications
You must be signed in to change notification settings - Fork 14
/
update.py
executable file
·123 lines (101 loc) · 3.2 KB
/
update.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
#!/usr/bin/env python3
"""Script for CI update. """
import argparse
import base64
import json
import os
import subprocess
from datetime import datetime
from itertools import chain
from tempfile import mkstemp
from typing import List
from gfwlist2acl import ChinaTimezone, get_acl_rules, ACL_TEMPLATE
DOWNLOAD_URL = "https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"
__dirname__ = os.path.abspath(os.path.dirname(__file__))
def _file_path(*other):
return os.path.join(__dirname__, *other)
HASH_FILE = _file_path("hash.txt")
def download() -> List[str]:
"""Download gfwlist
Returns:
List[str]
"""
return base64.b64decode(
subprocess.run(
["curl", DOWNLOAD_URL], encoding="utf-8", stdout=subprocess.PIPE, check=True
).stdout
).decode("utf-8")
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--release",
action="store_true",
help="create new release if repository data is not up to date",
)
args = parser.parse_args()
is_release = args.release
now = datetime.now(ChinaTimezone())
blacklist, whitelist = get_acl_rules(download().splitlines())
filenames = []
with open(_file_path("gfwlist.acl"), "w", encoding="utf-8", newline="\n") as f:
f.write(
ACL_TEMPLATE.format(
date=now.isoformat(),
filename="gfwlist.acl",
default_action="bypass_all",
blacklist="\n".join(blacklist),
whitelist="\n".join(whitelist),
)
)
filenames.append("gfwlist.acl")
with open(
_file_path("gfwlist.white.acl"), "w", encoding="utf-8", newline="\n"
) as f:
f.write(
ACL_TEMPLATE.format(
date=now.isoformat(),
filename="gfwlist.white.acl",
default_action="proxy_all",
blacklist="\n".join(blacklist),
whitelist="\n".join(whitelist),
)
)
filenames.append("gfwlist.white.acl")
with open(_file_path("gfwlist.acl.json"), "w", encoding="utf-8", newline="\n") as f:
json.dump(
{"blacklist": blacklist, "whitelist": whitelist},
f,
indent=4,
)
filenames.append("gfwlist.acl.json")
if not is_release:
print("Updated repository data, skip release since not specified `--release`")
return
subprocess.run(["git", "add", *filenames], check=True)
diff = subprocess.run(
["git", "diff", "--cached", "gfwlist.acl.json"],
encoding="utf-8",
stdout=subprocess.PIPE,
check=True,
).stdout
if not diff:
print("Already up to date")
return
subprocess.run(["git", "commit", "-m", "Update acl files [skip ci]"], check=True)
subprocess.run(["git", "push"], check=True)
note = now.strftime("%Y.%m.%d") + "\n\n```diff\n" + diff + "\n```"
subprocess.run(
[
"gh",
"release",
"create",
now.strftime("%Y.%m.%d"),
*filenames,
"-F",
"-",
],
check=True,
input=note,
)
if __name__ == "__main__":
main()