|
| 1 | +import importlib.metadata |
| 2 | +import os |
| 3 | +import re |
| 4 | +import requests |
| 5 | + |
| 6 | +def get_latest_bokeh_version(): |
| 7 | + url = "https://pypi.org/pypi/bokeh/json" |
| 8 | + response = requests.get(url) |
| 9 | + response.raise_for_status() # Raises an HTTPError if the status is not 200 |
| 10 | + data = response.json() |
| 11 | + return data["info"]["version"] |
| 12 | + |
| 13 | +def download_files(new_version, destination): |
| 14 | + files_to_download = [ |
| 15 | + f"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-{new_version}.min.js", |
| 16 | + f"https://cdn.bokeh.org/bokeh/release/bokeh-gl-{new_version}.min.js", |
| 17 | + f"https://cdn.bokeh.org/bokeh/release/bokeh-api-{new_version}.min.js", |
| 18 | + f"https://cdn.bokeh.org/bokeh/release/bokeh-tables-{new_version}.min.js", |
| 19 | + f"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-{new_version}.min.js", |
| 20 | + f"https://cdn.bokeh.org/bokeh/release/bokeh-{new_version}.min.js" |
| 21 | + ] |
| 22 | + |
| 23 | + for url in files_to_download: |
| 24 | + filename = os.path.basename(url) |
| 25 | + print(f"Downloading {filename}") |
| 26 | + r = requests.get(url, stream=True) |
| 27 | + r.raise_for_status() |
| 28 | + with open(os.path.join(destination, filename), "wb") as f: |
| 29 | + for chunk in r.iter_content(chunk_size=8192): |
| 30 | + f.write(chunk) |
| 31 | + |
| 32 | +def update_setup_py(old_version, new_version): |
| 33 | + setup_py_path = "setup.py" |
| 34 | + with open(setup_py_path, "r") as f: |
| 35 | + setup_content = f.read() |
| 36 | + |
| 37 | + # Replace package version in `version='...'` |
| 38 | + # This pattern is naive; adapt as needed for your file structure. |
| 39 | + setup_content = re.sub( |
| 40 | + r"(version\s*=\s*['\"])([\d\.]+)(['\"])", |
| 41 | + rf"\g<1>{new_version}\g<3>", |
| 42 | + setup_content |
| 43 | + ) |
| 44 | + |
| 45 | + # Replace bokeh==old_version with bokeh==new_version |
| 46 | + if old_version: |
| 47 | + setup_content = re.sub( |
| 48 | + rf"(bokeh\s*==\s*){old_version}", |
| 49 | + rf"\g<1>{new_version}", |
| 50 | + setup_content |
| 51 | + ) |
| 52 | + |
| 53 | + with open(setup_py_path, "w") as f: |
| 54 | + f.write(setup_content) |
| 55 | + |
| 56 | +def update_index_html(public_dir, old_version, new_version): |
| 57 | + index_html_path = os.path.join(public_dir, "index.html") |
| 58 | + if os.path.exists(index_html_path): |
| 59 | + with open(index_html_path, "r", encoding="utf-8") as f: |
| 60 | + html_content = f.read() |
| 61 | + |
| 62 | + # If old_version is known, do a direct replacement |
| 63 | + if old_version: |
| 64 | + # Replace each script reference with the new version |
| 65 | + cdn_suffixes = [ |
| 66 | + "mathjax", |
| 67 | + "gl", |
| 68 | + "api", |
| 69 | + "tables", |
| 70 | + "widgets", |
| 71 | + "" |
| 72 | + ] |
| 73 | + for suffix in cdn_suffixes: |
| 74 | + old_str = f"bokeh-{suffix}-{old_version}.min.js" if suffix else f"bokeh-{old_version}.min.js" |
| 75 | + new_str = f"bokeh-{suffix}-{new_version}.min.js" if suffix else f"bokeh-{new_version}.min.js" |
| 76 | + html_content = html_content.replace(old_str, new_str) |
| 77 | + |
| 78 | + with open(index_html_path, "w", encoding="utf-8") as f: |
| 79 | + f.write(html_content) |
| 80 | + else: |
| 81 | + print("No index.html found in frontend/public. Skipping HTML update.") |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + new_version = get_latest_bokeh_version() |
| 86 | + old_version = importlib.metadata.version("streamlit_bokeh_chart") |
| 87 | + |
| 88 | + print(f"Current local bokeh version: {old_version}") |
| 89 | + print(f"Latest PyPI bokeh version: {new_version}") |
| 90 | + |
| 91 | + if new_version == old_version: |
| 92 | + print("No new version available") |
| 93 | + print(f"::set-output name=needs_update::false") |
| 94 | + exit(0) |
| 95 | + |
| 96 | + print("New version available!") |
| 97 | + public_dir = "streamlit_bokeh_chart/frontend/public" |
| 98 | + |
| 99 | + # Remove original files |
| 100 | + for filename in os.listdir(public_dir): |
| 101 | + if "bokeh" in filename and filename.endswith(".js"): |
| 102 | + os.remove(os.path.join(public_dir, filename)) |
| 103 | + |
| 104 | + download_files(new_version, public_dir) |
| 105 | + update_setup_py(old_version, new_version) |
| 106 | + update_index_html(public_dir, old_version, new_version) |
| 107 | + |
| 108 | + print(f"::set-output name=needs_update::true") |
| 109 | + print(f"::set-output name=old_version::{old_version}") |
| 110 | + print(f"::set-output name=new_version::{new_version}") |
0 commit comments