import contextlib import json import os import pathlib import shutil import subprocess from datetime import datetime, timezone PYTHON_BUILD_DIR = pathlib.Path(__file__).parent WHEEL_DIR = PYTHON_BUILD_DIR / "wheels" SITE_OUTPUT_DIR = PYTHON_BUILD_DIR / "site" shutil.rmtree(SITE_OUTPUT_DIR, ignore_errors=True) SITE_OUTPUT_DIR.mkdir(parents=True) SITE_ARTIFACT_WHEEL_DIR = SITE_OUTPUT_DIR / "artifacts" / "wheels" SITE_ARTIFACT_WHEEL_DIR.mkdir(parents=True) for wheel_file in WHEEL_DIR.glob("*"): shutil.copy(wheel_file, SITE_ARTIFACT_WHEEL_DIR) def package_name(wheel_file: pathlib.Path) -> str: return wheel_file.name.split("-")[0].replace("_", "-") # Get some extra context about the build ts = datetime.now(timezone.utc).isoformat() context_info: dict = { "timestamp": ts, } # Get branch info. with contextlib.suppress(Exception): if branch_info := os.getenv("GITHUB_HEAD_REF"): pass else: branch_info = subprocess.check_output( ["git", "branch", "--show-current"], text=True ) context_info["branch"] = branch_info.strip() # Get commit info. with contextlib.suppress(Exception): commit_info = subprocess.check_output( ["git", "log", "-1", "--pretty=%H%n%B"], text=True ) commit_hash, commit_msg = commit_info.strip().split("\n", 1) context_info["commit"] = { "hash": commit_hash, "message": commit_msg.strip(), } # Get PR info. with contextlib.suppress(Exception): pr_info = "unknown" if github_ref := os.getenv("GITHUB_REF"): # e.g. GITHUB_REF=refs/pull/12157/merge parts = github_ref.split("/") if parts[1] == "pull": pull_number = parts[2] pr_info = json.loads( subprocess.check_output( ["gh", "pr", "view", pull_number, "--json", "title,number,url"], text=True, ) ) else: # The `gh` CLI might be able to figure it out. pr_info = json.loads( subprocess.check_output( ["gh", "pr", "view", "--json", "title,number,url"], text=True ) ) context_info["pr"] = pr_info newline = "\n" (SITE_OUTPUT_DIR / "index.html").write_text( f""" DataHub Python Builds

DataHub Python Builds

These prebuilt wheel files can be used to install our Python packages as of a specific commit.

Build context

Built at {ts}.

{json.dumps(context_info, indent=2)}

Usage

Current base URL: unknown

{ newline.join( f''' ''' for wheel_file in sorted(WHEEL_DIR.glob("*.whl")) ) }
Package Size Install command
{package_name(wheel_file)} {wheel_file.stat().st_size / 1024 / 1024:.3f} MB uv pip install '{package_name(wheel_file)} @ <base-url>/artifacts/wheels/{wheel_file.name}'
""" ) print("DataHub Python wheel site built in", SITE_OUTPUT_DIR)