Files

83 lines
2.9 KiB
Python
Raw Permalink Normal View History

2026-06-21 14:52:23 +08:00
"""
扫描所有模板,找出所有引用的 PostgreSQL 表,列出哪些已导出 GPKG、哪些还没。
"""
import zipfile, re, os, sys
TEMPLATE_BASE = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"app", "data", "template"
)
# 当前已导出的 GPKG 表(从 qgis_mappings.py / export_static_layers.py
EXPORTED = {
"rivers", "sx_capital", "river", "active_fault", "sx",
"sx_street", "sx_xa_county", "sx_xa_county_boundary",
"sx_zb_county_boundary", "sx_zb_city", "sx_zb_county",
"traffic_expressway", "traffic_provincial", "traffic_railway",
"traffic_township", "traffic_trunk_line",
}
# 避难场所 OGR→PG 映射的表(已由 TemplateModifier 转换)
OGR_TABLES = {
"shelter_park", "shelter_school", "shelter_cultural",
"shelter_defence", "shelter_gymnasium", "shelter_square", "shelter_stay",
}
all_tables = {} # {table_name: set of templates}
for event_type in ["rainfall", "earthquake"]:
tdir = os.path.join(TEMPLATE_BASE, event_type)
if not os.path.isdir(tdir):
continue
for tf in sorted(os.listdir(tdir)):
if not tf.endswith(".qgz") or tf.startswith("tmp"):
continue
tpath = os.path.join(tdir, tf)
with zipfile.ZipFile(tpath, "r") as z:
for item in z.infolist():
if item.filename.endswith(".qgs"):
content = z.read(item.filename).decode("utf-8")
# 匹配 datasource 中的 table="schema"."name"
for m in re.finditer(r'table="(\w+)"\."(\w+)"', content):
tbl = m.group(2)
all_tables.setdefault(tbl, set()).add(tf)
# 也检查 provider=postgres 但没有 table= 的(如震中)
# 匹配 dbname='...' 格式
for m in re.finditer(r"dbname='([^']+)'", content):
db = m.group(1)
if db == 'xxgx_client_ya':
all_tables.setdefault(f"[外部DB: {db}]", set()).add(tf)
print("=" * 60)
print("已导出 GPKG (16):")
for t in sorted(EXPORTED):
if t in all_tables:
print(f"{t}")
print()
print("避难场所 OGR→PG (7):")
for t in sorted(OGR_TABLES):
print(f"{t}")
print()
print("未导出,需要新增:")
need_export = []
for tbl, tmps in sorted(all_tables.items()):
if tbl.startswith("[外部DB:"):
continue
if tbl in EXPORTED or tbl in OGR_TABLES:
continue
need_export.append(tbl)
print(f"{tbl}{', '.join(sorted(tmps))}")
print()
print("外部数据库(无法静态化):")
for tbl, tmps in sorted(all_tables.items()):
if tbl.startswith("[外部DB:"):
print(f" 🔗 {tbl}{', '.join(sorted(tmps))}")
print()
print(f"总结: 需要新增导出 {len(need_export)} 张表")
if need_export:
print(f" {', '.join(sorted(need_export))}")