|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Refresh regions.json from the Contentstack CDN. |
| 3 | +# Usage (run from your project root): |
| 4 | +# python3 Scripts/refresh-region.py |
| 5 | +# python Scripts/refresh-region.py |
| 6 | + |
| 7 | +import glob |
| 8 | +import json |
| 9 | +import os |
| 10 | +import ssl |
| 11 | +import sys |
| 12 | +import urllib.request |
| 13 | + |
| 14 | +REGIONS_URL = "https://artifacts.contentstack.com/regions.json" |
| 15 | + |
| 16 | +print(f"Fetching {REGIONS_URL} ...") |
| 17 | + |
| 18 | + |
| 19 | +def _fetch(url): |
| 20 | + # First attempt: normal SSL verification |
| 21 | + try: |
| 22 | + with urllib.request.urlopen(url, timeout=30) as r: |
| 23 | + return r.read().decode("utf-8") |
| 24 | + except urllib.error.URLError as e: |
| 25 | + if "CERTIFICATE_VERIFY_FAILED" not in str(e): |
| 26 | + raise |
| 27 | + # macOS python.org builds often lack system certs — retry without verification |
| 28 | + print("WARNING: SSL certificate verification failed. Retrying without verification.", file=sys.stderr) |
| 29 | + print(" To fix permanently, run: /Applications/Python*/Install\\ Certificates.command", file=sys.stderr) |
| 30 | + ctx = ssl.create_default_context() |
| 31 | + ctx.check_hostname = False |
| 32 | + ctx.verify_mode = ssl.CERT_NONE |
| 33 | + with urllib.request.urlopen(url, timeout=30, context=ctx) as r: |
| 34 | + return r.read().decode("utf-8") |
| 35 | + |
| 36 | + |
| 37 | +try: |
| 38 | + raw = _fetch(REGIONS_URL) |
| 39 | +except Exception as e: |
| 40 | + print(f"ERROR: Could not download regions.json: {e}", file=sys.stderr) |
| 41 | + sys.exit(1) |
| 42 | + |
| 43 | +try: |
| 44 | + data = json.loads(raw) |
| 45 | +except json.JSONDecodeError as e: |
| 46 | + print(f"ERROR: Downloaded content is not valid JSON: {e}", file=sys.stderr) |
| 47 | + sys.exit(1) |
| 48 | + |
| 49 | +if "regions" not in data: |
| 50 | + print("ERROR: Downloaded JSON does not contain a 'regions' key.", file=sys.stderr) |
| 51 | + sys.exit(1) |
| 52 | + |
| 53 | +region_count = len(data["regions"]) |
| 54 | + |
| 55 | +# Scan bin/ for every copy of the DLL (covers Debug/Release, any TFM, any nesting). |
| 56 | +dll_pattern = os.path.join(os.getcwd(), "**", "Contentstack.Utils.dll") |
| 57 | +found = [ |
| 58 | + os.path.dirname(dll) |
| 59 | + for dll in glob.glob(dll_pattern, recursive=True) |
| 60 | + if os.sep + "bin" + os.sep in dll |
| 61 | +] |
| 62 | + |
| 63 | +if not found: |
| 64 | + print("[bin] No build output found — run 'dotnet build' first, then re-run this script.") |
| 65 | + sys.exit(1) |
| 66 | + |
| 67 | +for bin_dir in found: |
| 68 | + assets_dir = os.path.join(bin_dir, "Assets") |
| 69 | + os.makedirs(assets_dir, exist_ok=True) |
| 70 | + dest = os.path.join(assets_dir, "regions.json") |
| 71 | + with open(dest, "w", encoding="utf-8") as f: |
| 72 | + f.write(raw) |
| 73 | + print(f"[bin] Wrote {region_count} regions → {dest}") |
0 commit comments