1
0
Fork 0
mirror of https://github.com/NeoCloud/NeoNetwork synced 2024-12-23 20:19:24 +08:00
NeoNetwork/scripts/update-zone-serial.py

55 lines
1.6 KiB
Python
Raw Normal View History

2021-04-14 23:11:55 +08:00
#!/usr/bin/env python3
import subprocess
from os import chdir
2021-06-08 08:26:26 +08:00
from pathlib import Path
from re import match
from time import time
2021-04-14 23:11:55 +08:00
zone_files = [
2021-06-08 08:26:26 +08:00
"neonetwork",
"db.10.127",
"db.fd10.127",
2021-04-14 23:11:55 +08:00
]
serial_base = 1586876035
2021-04-16 14:43:50 +08:00
new_serial = int(time()) - serial_base
2021-06-08 08:26:26 +08:00
2021-04-16 14:43:50 +08:00
def update_serial_to(zone: Path, serial: int = 0) -> int:
lines = zone.read_text().split("\n")
processed = list()
2021-06-08 08:26:26 +08:00
assert 0 <= serial <= 2 ** 32
2021-04-16 14:43:50 +08:00
found = False
old_serial = None
for line in lines:
if not found and (m := match(r"^(\s+)([0-9]+)(\s*;\s*Serial\s*)$", line)):
prefix, old_serial, suffix = m.groups()
old_serial = int(old_serial)
print(f"{old_serial=} {serial=}")
plen = max(len(prefix) - len(str(serial)) + len(str(old_serial)), 0)
processed.append(f"{' '*plen}{serial}{suffix}")
found = True
else:
processed.append(line)
if serial:
zone.write_text("\n".join(processed))
return old_serial
2021-04-14 23:11:55 +08:00
2021-06-08 08:26:26 +08:00
2021-04-14 23:11:55 +08:00
for zone in zone_files:
2021-04-16 14:43:50 +08:00
gen_zone = Path("generated") / "dns" / zone
repo_zone = Path("dns") / zone
assert gen_zone.exists()
assert repo_zone.exists()
old_serial = update_serial_to(gen_zone)
update_serial_to(repo_zone, old_serial)
gen_zone.write_text(repo_zone.read_text())
2021-06-08 08:26:26 +08:00
p = subprocess.run(
["git", "diff", "--exit-code", gen_zone.name], cwd=gen_zone.parent
)
2021-04-14 23:11:55 +08:00
if p.returncode == 0:
2021-04-16 14:43:50 +08:00
print(f"skip {repo_zone.name}")
2021-04-14 23:11:55 +08:00
else:
2021-04-16 14:43:50 +08:00
print(f"update serial {repo_zone.name}")
update_serial_to(repo_zone, new_serial)