|
| 1 | +""" |
| 2 | +Utility to pull the latest regions.json from the Contentstack CDN and |
| 3 | +overwrite the bundled copy at contentstack_utils/assets/regions.json. |
| 4 | +
|
| 5 | +Exposed as a package-level function so tooling and CI pipelines can call it |
| 6 | +programmatically instead of invoking the script directly: |
| 7 | +
|
| 8 | + from contentstack_utils import refresh_regions |
| 9 | + refresh_regions() |
| 10 | +""" |
| 11 | + |
| 12 | +import json |
| 13 | +import os |
| 14 | +import sys |
| 15 | +import urllib.request |
| 16 | + |
| 17 | +_REGIONS_URL = "https://artifacts.contentstack.com/regions.json" |
| 18 | +_ASSET_PATH = os.path.join(os.path.dirname(__file__), "assets", "regions.json") |
| 19 | + |
| 20 | + |
| 21 | +def refresh_regions( |
| 22 | + url: str = _REGIONS_URL, |
| 23 | + dest: str = _ASSET_PATH, |
| 24 | + *, |
| 25 | + timeout: int = 30, |
| 26 | + silent: bool = False, |
| 27 | +) -> dict: |
| 28 | + """ |
| 29 | + Download the latest regions manifest from the Contentstack CDN and write |
| 30 | + it to the bundled assets file so all consumers get the update. |
| 31 | +
|
| 32 | + @param url - URL to fetch regions.json from (defaults to Contentstack CDN) |
| 33 | + @param dest - Destination file path (defaults to contentstack_utils/assets/regions.json) |
| 34 | + @param timeout - HTTP request timeout in seconds |
| 35 | + @param silent - Suppress progress output when True |
| 36 | + @returns The parsed regions dict on success |
| 37 | + @raises RuntimeError on download failure, invalid JSON, or unexpected schema |
| 38 | + """ |
| 39 | + dest = os.path.normpath(dest) |
| 40 | + |
| 41 | + if not silent: |
| 42 | + print(f"Fetching {url} ...") |
| 43 | + |
| 44 | + try: |
| 45 | + with urllib.request.urlopen(url, timeout=timeout) as resp: |
| 46 | + data = resp.read().decode("utf-8") |
| 47 | + except Exception as exc: |
| 48 | + raise RuntimeError(f"Could not download regions.json: {exc}") from exc |
| 49 | + |
| 50 | + try: |
| 51 | + decoded = json.loads(data) |
| 52 | + except json.JSONDecodeError as exc: |
| 53 | + raise RuntimeError(f"Downloaded content is not valid JSON: {exc}") from exc |
| 54 | + |
| 55 | + if not isinstance(decoded, dict) or "regions" not in decoded: |
| 56 | + raise RuntimeError("Downloaded JSON does not contain a 'regions' key.") |
| 57 | + |
| 58 | + os.makedirs(os.path.dirname(dest), exist_ok=True) |
| 59 | + with open(dest, "w", encoding="utf-8") as fh: |
| 60 | + json.dump(decoded, fh, indent=2, ensure_ascii=False) |
| 61 | + fh.write("\n") |
| 62 | + |
| 63 | + region_count = len(decoded["regions"]) |
| 64 | + if not silent: |
| 65 | + print(f"OK: Wrote {region_count} regions to {dest}") |
| 66 | + |
| 67 | + return decoded |
| 68 | + |
| 69 | + |
| 70 | +def _cli_main() -> int: |
| 71 | + """Entry point kept for backward compatibility with the scripts/ invocation.""" |
| 72 | + try: |
| 73 | + refresh_regions() |
| 74 | + print("Next steps:") |
| 75 | + return 0 |
| 76 | + except RuntimeError as exc: |
| 77 | + print(f"ERROR: {exc}", file=sys.stderr) |
| 78 | + return 1 |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + sys.exit(_cli_main()) |
0 commit comments