-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgeo-update.py
More file actions
96 lines (77 loc) · 2.82 KB
/
geo-update.py
File metadata and controls
96 lines (77 loc) · 2.82 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
import shutil
import subprocess
from pathlib import Path
REPO_URL = "https://github.com/MetaCubeX/meta-rules-dat"
REPO_BRANCH = "meta"
BASE_DIR = Path(__file__).resolve().parent
REPO_DIR = BASE_DIR / "meta-rules-dat"
GEO_DIR = BASE_DIR / "geo"
GEOIP_FOLDER = REPO_DIR / "geo" / "geoip"
GEOSITE_FOLDER = REPO_DIR / "geo" / "geosite"
STRIP_EXTS = {".yaml", ".mrs", ".list", ".dat", ".txt"}
DIR_EXCLUDE = {"classical"}
def run(cmd, cwd=None):
print(f"> {' '.join(cmd)}")
subprocess.run(cmd, cwd=cwd, check=True)
def remove_dir_if_exists(path: Path):
if path.exists():
shutil.rmtree(path)
def clone_repo(repo_url: str, target_dir: Path, branch: str = None):
cmd = ["git", "clone"]
if branch:
cmd += ["--branch", branch, "--single-branch"]
cmd += [repo_url, str(target_dir)]
run(cmd)
def strip_known_exts(name: str, exts):
changed = True
while changed:
changed = False
for ext in exts:
if name.endswith(ext):
name = name[:-len(ext)]
changed = True
return name.rstrip(".")
def write_names_from_folder(folder: Path, out_file: Path, strip_exts=None, include_dirs=True, dir_exclude=None):
if not folder.exists():
raise FileNotFoundError(f"Folder not found: {folder}")
strip_exts = set(strip_exts or [])
dir_exclude = set(dir_exclude or [])
names = []
for p in folder.iterdir():
if p.is_dir():
if include_dirs and p.name not in dir_exclude:
names.append(p.name)
continue
if p.is_file():
name = p.name
if strip_exts:
name = strip_known_exts(name, strip_exts)
else:
name = p.stem
if name:
names.append(name)
names = sorted(set(names))
out_file.write_text("\n".join(names) + "\n", encoding="utf-8")
print(f"Wrote {len(names)} names to {out_file}")
def move_to_geo_folder(*files: Path):
GEO_DIR.mkdir(parents=True, exist_ok=True)
for f in files:
if f.exists():
dest = GEO_DIR / f.name
if dest.exists():
dest.unlink()
shutil.move(str(f), str(dest))
print(f"Moved {f} -> {dest}")
else:
print(f"Skip moving, file not found: {f}")
def main():
remove_dir_if_exists(REPO_DIR)
clone_repo(REPO_URL, REPO_DIR, branch=REPO_BRANCH)
geoip_txt = BASE_DIR / "geoip.txt"
geosite_txt = BASE_DIR / "geosite.txt"
write_names_from_folder(GEOIP_FOLDER, geoip_txt, strip_exts=STRIP_EXTS, include_dirs=True, dir_exclude=DIR_EXCLUDE)
write_names_from_folder(GEOSITE_FOLDER, geosite_txt, strip_exts=STRIP_EXTS, include_dirs=True, dir_exclude=DIR_EXCLUDE)
move_to_geo_folder(geoip_txt, geosite_txt)
remove_dir_if_exists(REPO_DIR)
if __name__ == "__main__":
main()