| 
 | 1 | +"""  | 
 | 2 | +script to update nbviewer  | 
 | 3 | +
  | 
 | 4 | +run via watch-dependencies.yaml workflow,  | 
 | 5 | +but can be run locally.  | 
 | 6 | +
  | 
 | 7 | +"""  | 
 | 8 | + | 
 | 9 | +import os  | 
 | 10 | +import shlex  | 
 | 11 | +from pathlib import Path  | 
 | 12 | +from subprocess import check_output  | 
 | 13 | + | 
 | 14 | +import requests  | 
 | 15 | +import yaml  | 
 | 16 | + | 
 | 17 | +nbviewer_deploy = Path(__file__).absolute().parents[1]  | 
 | 18 | +cd_yaml = nbviewer_deploy / ".github/workflows/cd.yml"  | 
 | 19 | +nbviewer_config_yaml = nbviewer_deploy / "config/nbviewer.yaml"  | 
 | 20 | + | 
 | 21 | + | 
 | 22 | +def _maybe_output(key, value):  | 
 | 23 | +    """Make outputs available to github actions (if running in github actions)"""  | 
 | 24 | +    github_output = os.environ.get("GITHUB_OUTPUT")  | 
 | 25 | +    line = f"{key}={shlex.quote(value)}"  | 
 | 26 | +    if github_output:  | 
 | 27 | +        with Path(github_output).open("a") as f:  | 
 | 28 | +            f.write(f"{line}\n")  | 
 | 29 | +    else:  | 
 | 30 | +        print(line)  | 
 | 31 | + | 
 | 32 | + | 
 | 33 | +def get_current_chart():  | 
 | 34 | +    """Get the current version of the chart in cd.yaml"""  | 
 | 35 | +    with cd_yaml.open() as f:  | 
 | 36 | +        cd = yaml.safe_load(f)  | 
 | 37 | +    chart_rev = cd["env"]["NBVIEWER_VERSION"]  | 
 | 38 | +    return chart_rev  | 
 | 39 | + | 
 | 40 | + | 
 | 41 | +def get_latest_chart():  | 
 | 42 | +    """Get the latest version of the chart repo"""  | 
 | 43 | +    out = check_output(  | 
 | 44 | +        ["git", "ls-remote", "https://github.com/jupyter/nbviewer", "HEAD"], text=True  | 
 | 45 | +    ).strip()  | 
 | 46 | +    return out.split()[0]  | 
 | 47 | + | 
 | 48 | + | 
 | 49 | +def get_current_image():  | 
 | 50 | +    """Get the current version of the nbviewer image in config"""  | 
 | 51 | +    with nbviewer_config_yaml.open() as f:  | 
 | 52 | +        config = yaml.safe_load(f)  | 
 | 53 | +    current_image = config["image"]  | 
 | 54 | +    return current_image  | 
 | 55 | + | 
 | 56 | + | 
 | 57 | +def get_latest_image():  | 
 | 58 | +    """Get the latest version of the nbviewer image from docker hub"""  | 
 | 59 | +    r = requests.get("https://hub.docker.com/v2/repositories/jupyter/nbviewer/tags")  | 
 | 60 | +    r.raise_for_status()  | 
 | 61 | +    tags = r.json()  | 
 | 62 | +    tag = tags["results"][0]["name"]  | 
 | 63 | +    return f"jupyter/nbviewer:{tag}"  | 
 | 64 | + | 
 | 65 | + | 
 | 66 | +def update_chart():  | 
 | 67 | +    """Update the version of the nbviewer chart to be deployed"""  | 
 | 68 | +    current_chart = get_current_chart()  | 
 | 69 | +    latest_chart = get_latest_chart()  | 
 | 70 | +    _maybe_output("chart_before", current_chart)  | 
 | 71 | +    _maybe_output("chart_after", latest_chart)  | 
 | 72 | +    _maybe_output("chart_short", latest_chart[:7])  | 
 | 73 | +    if latest_chart != current_chart:  | 
 | 74 | +        print(f"Updating {current_chart} -> {latest_chart} in {cd_yaml}")  | 
 | 75 | +        with cd_yaml.open() as f:  | 
 | 76 | +            current_yaml = f.read()  | 
 | 77 | +        modified_yaml = current_yaml.replace(current_chart, latest_chart, 1)  | 
 | 78 | +        with cd_yaml.open("w") as f:  | 
 | 79 | +            f.write(modified_yaml)  | 
 | 80 | + | 
 | 81 | + | 
 | 82 | +def update_image():  | 
 | 83 | +    """Update the version of the nbviewer image to be deployed"""  | 
 | 84 | +    current_image = get_current_image()  | 
 | 85 | +    latest_image = get_latest_image()  | 
 | 86 | +    _maybe_output("image_before", current_image)  | 
 | 87 | +    _maybe_output("image_after", latest_image)  | 
 | 88 | +    _maybe_output("image_tag", latest_image.partition(":")[2])  | 
 | 89 | + | 
 | 90 | +    if latest_image != current_image:  | 
 | 91 | +        print(f"Updating {current_image} -> {latest_image} in {nbviewer_config_yaml}")  | 
 | 92 | +        with nbviewer_config_yaml.open() as f:  | 
 | 93 | +            current_yaml = f.read()  | 
 | 94 | +        modified_yaml = current_yaml.replace(current_image, latest_image, 1)  | 
 | 95 | +        with nbviewer_config_yaml.open("w") as f:  | 
 | 96 | +            f.write(modified_yaml)  | 
 | 97 | + | 
 | 98 | + | 
 | 99 | +def main():  | 
 | 100 | +    update_chart()  | 
 | 101 | +    update_image()  | 
 | 102 | + | 
 | 103 | + | 
 | 104 | +if __name__ == "__main__":  | 
 | 105 | +    main()  | 
0 commit comments