1
0
Fork 0
mirror of https://github.com/NeoCloud/NeoNetwork synced 2024-11-05 16:32:22 +08:00
NeoNetwork/scripts/update-zone-serial.py

51 lines
1.5 KiB
Python
Raw Normal View History

2021-04-14 23:11:55 +08:00
#!/usr/bin/env python3
from pathlib import Path
import subprocess
from time import time
from re import match
from os import chdir
zone_files = [
'neonetwork',
'db.10.127',
'db.fd10.127',
]
serial_base = 1586876035
2021-04-16 14:43:50 +08:00
new_serial = int(time()) - serial_base
def update_serial_to(zone: Path, serial: int = 0) -> int:
lines = zone.read_text().split("\n")
processed = list()
assert 0 <= serial <= 2**32
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
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())
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)