|
1 | 1 | #!/usr/bin/env python3 |
2 | | -""" |
3 | | -pack.mcmeta düzenleyici. |
4 | | -Kullanım: edit_pack_mcmeta.py <pack_format> <supported_min> <supported_max> |
5 | | -overlay_updates: GITHUB_EVENT_PATH üzerinden okunur (workflow_dispatch input). |
6 | | -""" |
7 | 2 |
|
8 | 3 | import json |
9 | 4 | import os |
10 | 5 | import sys |
| 6 | +from typing import Dict, Any |
11 | 7 |
|
12 | 8 |
|
13 | | -def main(): |
| 9 | +def main() -> None: |
14 | 10 | if len(sys.argv) < 4: |
15 | 11 | print("Kullanım: edit_pack_mcmeta.py <pack_format> <supported_min> <supported_max>") |
16 | 12 | sys.exit(1) |
17 | 13 |
|
18 | | - pf = int(sys.argv[1]) |
19 | | - smin = int(sys.argv[2]) |
20 | | - smax = int(sys.argv[3]) |
| 14 | + try: |
| 15 | + pf = int(sys.argv[1]) |
| 16 | + smin = int(sys.argv[2]) |
| 17 | + smax = int(sys.argv[3]) |
| 18 | + except ValueError: |
| 19 | + print("Hata: pack_format, supported_min ve supported_max tam sayı olmalıdır.") |
| 20 | + sys.exit(1) |
21 | 21 |
|
22 | | - # overlay_updates'i GITHUB_EVENT_PATH'ten oku (jq/heredoc sorunlarını önler) |
| 22 | + # GitHub Actions'tan overlay_updates girdisini oku |
23 | 23 | overlay_raw = "" |
24 | 24 | event_path = os.environ.get("GITHUB_EVENT_PATH", "") |
25 | 25 | if event_path and os.path.exists(event_path): |
26 | | - with open(event_path, "r", encoding="utf-8") as f: |
27 | | - event = json.load(f) |
28 | | - overlay_raw = event.get("inputs", {}).get("overlay_updates", "") or "" |
| 26 | + try: |
| 27 | + with open(event_path, "r", encoding="utf-8") as f: |
| 28 | + event = json.load(f) |
| 29 | + overlay_raw = event.get("inputs", {}).get("overlay_updates", "") or "" |
| 30 | + except (json.JSONDecodeError, IOError) as e: |
| 31 | + print(f"Uyarı: GitHub event dosyası okunamadı: {e}") |
| 32 | + |
| 33 | + # pack.mcmeta dosyasını oku |
| 34 | + try: |
| 35 | + with open("pack.mcmeta", "r", encoding="utf-8") as f: |
| 36 | + meta: Dict[str, Any] = json.load(f) |
| 37 | + except FileNotFoundError: |
| 38 | + print("Hata: pack.mcmeta dosyası bulunamadı!") |
| 39 | + sys.exit(1) |
| 40 | + except json.JSONDecodeError as e: |
| 41 | + print(f"Hata: pack.mcmeta geçerli bir JSON değil: {e}") |
| 42 | + sys.exit(1) |
29 | 43 |
|
30 | | - with open("pack.mcmeta", "r", encoding="utf-8") as f: |
31 | | - meta = json.load(f) |
| 44 | + pack = meta.setdefault("pack", {}) |
32 | 45 |
|
33 | | - old_pf = meta["pack"]["pack_format"] |
34 | | - old_smin = meta["pack"].get("supported_formats", {}).get("min_inclusive", "?") |
35 | | - old_smax = meta["pack"].get("supported_formats", {}).get("max_inclusive", "?") |
| 46 | + # Eski değerleri kaydet (log için) |
| 47 | + old_pf = pack.get("pack_format") |
| 48 | + supported = pack.get("supported_formats", {}) |
| 49 | + old_smin = supported.get("min_inclusive", "?") |
| 50 | + old_smax = supported.get("max_inclusive", "?") |
36 | 51 |
|
37 | | - meta["pack"]["pack_format"] = pf |
38 | | - meta["pack"]["supported_formats"] = { |
| 52 | + # Ana pack_format ve supported_formats güncelle |
| 53 | + pack["pack_format"] = pf |
| 54 | + pack["supported_formats"] = { |
39 | 55 | "min_inclusive": smin, |
40 | 56 | "max_inclusive": smax, |
41 | 57 | } |
42 | 58 |
|
43 | | - if "min_format" in meta["pack"]: |
44 | | - meta["pack"]["min_format"] = smin |
45 | | - if "max_format" in meta["pack"]: |
46 | | - meta["pack"]["max_format"] = smax |
| 59 | + # Eski stil min_format / max_format varsa onları da güncelle (geriye uyumluluk) |
| 60 | + if "min_format" in pack: |
| 61 | + pack["min_format"] = smin |
| 62 | + if "max_format" in pack: |
| 63 | + pack["max_format"] = smax |
47 | 64 |
|
48 | | - print(f"pack_format : {old_pf} → {pf}") |
| 65 | + print(f"pack_format : {old_pf} → {pf}") |
49 | 66 | print(f"supported_formats: [{old_smin}, {old_smax}] → [{smin}, {smax}]") |
50 | 67 |
|
| 68 | + # Overlay'leri güncelle (varsa) |
51 | 69 | if overlay_raw.strip(): |
52 | | - entries = {e["directory"]: e for e in meta.get("overlays", {}).get("entries", [])} |
| 70 | + entries: Dict[str, Dict[str, Any]] = { |
| 71 | + e["directory"]: e |
| 72 | + for e in meta.get("overlays", {}).get("entries", []) |
| 73 | + } |
| 74 | + |
53 | 75 | for line in overlay_raw.splitlines(): |
54 | 76 | line = line.strip() |
55 | 77 | if not line or "=" not in line or ":" not in line: |
56 | 78 | continue |
57 | | - directory, fmt = line.split("=", 1) |
58 | | - directory = directory.strip() |
59 | | - o_min, o_max = fmt.strip().split(":", 1) |
60 | | - o_min = int(o_min.strip()) |
61 | | - o_max = int(o_max.strip()) |
62 | | - |
63 | | - if directory in entries: |
64 | | - old_fmt = entries[directory].get("formats", {}) |
65 | | - old_min = old_fmt.get("min_inclusive", "?") |
66 | | - old_max = old_fmt.get("max_inclusive", "?") |
67 | | - entries[directory]["formats"] = { |
68 | | - "min_inclusive": o_min, |
69 | | - "max_inclusive": o_max, |
70 | | - } |
71 | | - if "min_format" in entries[directory]: |
72 | | - entries[directory]["min_format"] = o_min |
73 | | - if "max_format" in entries[directory]: |
74 | | - entries[directory]["max_format"] = o_max |
75 | | - print(f"overlay '{directory}': [{old_min}, {old_max}] → [{o_min}, {o_max}]") |
76 | | - else: |
77 | | - print(f"WARN: overlay '{directory}' pack.mcmeta'da bulunamadı, atlandı.") |
78 | | - |
79 | | - with open("pack.mcmeta", "w", encoding="utf-8") as f: |
80 | | - json.dump(meta, f, indent=2, ensure_ascii=False) |
81 | | - f.write("\n") |
82 | | - |
83 | | - print("pack.mcmeta güncellendi.") |
| 79 | + |
| 80 | + try: |
| 81 | + directory, fmt = [x.strip() for x in line.split("=", 1)] |
| 82 | + o_min_str, o_max_str = [x.strip() for x in fmt.split(":", 1)] |
| 83 | + |
| 84 | + o_min = int(o_min_str) |
| 85 | + o_max = int(o_max_str) |
| 86 | + |
| 87 | + if directory in entries: |
| 88 | + entry = entries[directory] |
| 89 | + old_fmt = entry.get("formats", {}) |
| 90 | + old_min = old_fmt.get("min_inclusive", "?") |
| 91 | + old_max = old_fmt.get("max_inclusive", "?") |
| 92 | + |
| 93 | + entry["formats"] = { |
| 94 | + "min_inclusive": o_min, |
| 95 | + "max_inclusive": o_max, |
| 96 | + } |
| 97 | + |
| 98 | + if "min_format" in entry: |
| 99 | + entry["min_format"] = o_min |
| 100 | + if "max_format" in entry: |
| 101 | + entry["max_format"] = o_max |
| 102 | + |
| 103 | + print(f"overlay '{directory}': [{old_min}, {old_max}] → [{o_min}, {o_max}]") |
| 104 | + else: |
| 105 | + print(f"WARN: overlay '{directory}' pack.mcmeta'da bulunamadı, atlandı.") |
| 106 | + |
| 107 | + except (ValueError, IndexError) as e: |
| 108 | + print(f"WARN: Geçersiz overlay satırı atlandı: '{line}' ({e})") |
| 109 | + |
| 110 | + # Güncellenmiş pack.mcmeta'yı yaz |
| 111 | + try: |
| 112 | + with open("pack.mcmeta", "w", encoding="utf-8") as f: |
| 113 | + json.dump(meta, f, indent=2, ensure_ascii=False) |
| 114 | + f.write("\n") |
| 115 | + print("✅ pack.mcmeta başarıyla güncellendi.") |
| 116 | + except IOError as e: |
| 117 | + print(f"Hata: pack.mcmeta yazılamadı: {e}") |
| 118 | + sys.exit(1) |
84 | 119 |
|
85 | 120 |
|
86 | 121 | if __name__ == "__main__": |
|
0 commit comments