Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

235 lines
7.8 KiB
Python
Raw Permalink Normal View History

2024-07-04 08:29:25 +00:00
# commands to run it on a generic ubuntu 2404 oci container:
2024-07-03 12:52:32 +00:00
"""
apt update && apt install git wget curl ptpython mono-devel micro
mkdir -p /home/runner/work/IfcOpenShell && cd /home/runner/work/IfcOpenShell
git clone https://github.com/IfcOpenShell/IfcOpenShell
cd /home/runner/work/IfcOpenShell/IfcOpenShell/choco/blenderbim/
2024-07-04 08:29:25 +00:00
micro choco_release.py # paste this script, comment out push command
2024-07-03 12:52:32 +00:00
export CHOCO_TOKEN="secret_choco_release_token"
python3 choco_release.py
"""
import datetime
import hashlib
import os
import pathlib
import re
2026-03-17 13:39:10 +05:00
import subprocess
2025-12-19 18:53:04 +05:00
from typing import NoReturn
2024-07-03 12:52:32 +00:00
from urllib import request
2025-12-19 18:53:04 +05:00
2024-08-06 16:47:24 +05:00
from github import Github
2024-07-03 12:52:32 +00:00
2024-08-06 16:47:24 +05:00
def get_repo_tag_names() -> list[str]:
2026-03-17 13:39:10 +05:00
git_return = subprocess.check_output("git tag -l", text=True)
2024-07-04 08:29:25 +00:00
tag_names = [tag_name for tag_name in git_return.split("\n") if tag_name]
print(f"{len(tag_names)} tag_names found in repo")
return tag_names
2024-07-03 12:52:32 +00:00
def request_repo_info(url: str):
req = request.Request(url)
resp = request.urlopen(req)
if not resp.status == 200:
print(f"[ERROR] could not contact server: {url}")
quit(1)
return resp
def get_choco_package_info() -> str:
resp = request_repo_info(URL_CHOCO_PACKAGE)
html_txt = str(resp.read())
return html_txt
2024-07-04 08:29:25 +00:00
def get_latest_choco_blender_version() -> list:
2024-07-03 12:52:32 +00:00
html_txt = get_choco_package_info()
return re.findall(RE_BLENDER_VERSION_MIN_MAJ_PAT, html_txt)
2024-08-06 16:47:24 +05:00
def get_file_sha256_hash(file_path: str) -> str:
2024-07-03 12:52:32 +00:00
BLOCKSIZE = 65536
hasher = hashlib.sha256()
with open(file_path, "rb") as input_file:
buffer = input_file.read(BLOCKSIZE)
while len(buffer) > 0:
hasher.update(buffer)
buffer = input_file.read(BLOCKSIZE)
return hasher.hexdigest()
2024-08-06 16:47:24 +05:00
def quit_with_error_message(message: str) -> NoReturn:
2024-07-03 12:52:32 +00:00
print(f"ERROR: {message}")
quit(0)
2024-08-06 16:47:24 +05:00
def get_release_zip(tag: str) -> tuple[str, str]:
g = Github()
repo = g.get_repo("IfcOpenShell/IfcOpenShell")
release = repo.get_release(tag)
for asset in release.get_assets():
asset_name = asset.name
if python_version not in asset_name:
continue
if TARGET_OS not in asset_name:
continue
return (asset_name, asset.browser_download_url)
raise Exception(f"Couldn't find the release matching '{python_version}' and '{TARGET_OS}' in tag '{tag}'.")
2026-03-17 13:39:10 +05:00
def run(command: str) -> None:
subprocess.check_output(command)
2024-07-03 12:52:32 +00:00
start = datetime.datetime.now()
URL_CHOCO_PACKAGE = "https://community.chocolatey.org/packages/blender"
URL_BLENDER_CMAKE = "https://raw.githubusercontent.com/blender/blender/{}/build_files/cmake/Modules/FindPythonLibsUnix.cmake"
RE_BLENDER_VERSION_MIN_MAJ = r"Latest Version.+<span>Blender (\d+\.\d+)\..+</span>"
RE_BLENDER_VERSION_MIN_MAJ_PAT = r"Latest Version.+<span>Blender (\d+\.\d+\.\d+)</span>"
RE_BLENDER_PYTHON_VERSION_MAJ_MIN = r"\(_PYTHON_VERSION_SUPPORTED (\d+\.\d+)\)"
2024-07-04 08:29:25 +00:00
BLENDERBIM_DIR = pathlib.Path("/home/runner/work/IfcOpenShell/IfcOpenShell/choco/blenderbim/")
2024-07-03 12:52:32 +00:00
print("_____ check choco release needed?")
2024-07-04 08:29:25 +00:00
os.chdir(BLENDERBIM_DIR)
2024-07-03 12:52:32 +00:00
blenderbim_date_yesterday = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%y%m%d")
should_release = False
target_release_tag = ""
2024-08-06 16:47:24 +05:00
TARGET_OS = "windows-x64"
2024-07-03 12:52:32 +00:00
2026-03-17 13:39:10 +05:00
git_status = subprocess.check_output("git status", text=True)
2024-07-04 08:29:25 +00:00
print(git_status)
2024-07-03 12:52:32 +00:00
for tag_name in get_repo_tag_names():
2024-07-04 08:29:25 +00:00
print(tag_name)
2024-07-03 12:52:32 +00:00
if blenderbim_date_yesterday in tag_name:
should_release = True
target_release_tag = tag_name
print(f"found {tag_name=} - {should_release=}")
break
if not should_release:
print(f"INFO: no blenderbim release tags found for {blenderbim_date_yesterday} -> no choco release today.")
quit(0)
print(f"{should_release=}")
if not os.environ.get("CHOCO_TOKEN"):
quit_with_error_message("could retrieve CHOCO_TOKEN env var")
choco_token = os.environ["CHOCO_TOKEN"]
print("\n_____ get blender info")
# blender_version_min_maj_pat - from chocolatey.org
2024-07-04 08:29:25 +00:00
latest_blender_release_maj_min_pat = get_latest_choco_blender_version()
2024-07-03 12:52:32 +00:00
if not latest_blender_release_maj_min_pat:
quit_with_error_message("could not determine blender_version_min_maj_pat")
latest_blender_release_maj_min_pat = latest_blender_release_maj_min_pat[0]
print(f"{latest_blender_release_maj_min_pat=}")
# blender_version_min_maj - from chocolatey.org
2024-07-04 08:29:25 +00:00
blender_version_min_maj = latest_blender_release_maj_min_pat.rsplit(".", 1)[0]
2024-07-03 12:52:32 +00:00
print(f"{blender_version_min_maj=}")
# blender_python_version_maj_min - from blender repo
python_version = ""
latest_blender_version_tag = f"v{latest_blender_release_maj_min_pat}"
resp = request_repo_info(URL_BLENDER_CMAKE.format(latest_blender_version_tag))
html_txt = str(resp.read())
found = re.findall(RE_BLENDER_PYTHON_VERSION_MAJ_MIN, html_txt)
if not found:
quit_with_error_message("could not determine blender_python_version_maj_min")
blender_python_version_maj_min = found[0]
print(f"{blender_python_version_maj_min=}")
python_version = f"py{found[0].replace('.', '')}"
print(f"{python_version=}")
2024-07-10 07:14:23 +00:00
blenderbim_build_version = target_release_tag.replace("blenderbim-", "")
2024-07-03 12:52:32 +00:00
# url_blenderbim_py3x_win_zip
2024-08-06 16:47:24 +05:00
release_zip_file_name, url_blenderbim_py3x_win_zip = get_release_zip(target_release_tag)
2026-03-17 13:39:10 +05:00
subprocess.check_call(f"wget {url_blenderbim_py3x_win_zip} --no-verbose")
2024-07-03 12:52:32 +00:00
# sha256sum_blenderbim_py310_win_zip
sha256sum_blenderbim_py3x_win_zip = get_file_sha256_hash(release_zip_file_name)
print("\n_____ fill dynamic chocolatey package parameters")
HERE_DIR = pathlib.Path(__file__).parent.absolute()
# print(f"[INFO] HERE_DIR: {HERE_DIR}")
topics = {
"spec": {
"path": HERE_DIR / "blenderbim.nuspec",
"key_values": {
"latest_blender_version_maj_min_pat": latest_blender_release_maj_min_pat,
"blenderbim_build_version" : blenderbim_build_version,
},
},
"install": {
"path": HERE_DIR / "tools" / "chocolateyinstall.ps1",
"key_values": {
"url_blenderbim_py3x_win_zip" : url_blenderbim_py3x_win_zip,
"sha256sum_blenderbim_py3x_win_zip": sha256sum_blenderbim_py3x_win_zip,
"latest_blender_version_maj_min" : blender_version_min_maj,
},
},
"uninstall": {
"path": HERE_DIR / "tools" / "chocolateyuninstall.ps1",
"key_values": {
"latest_blender_version_maj_min": blender_version_min_maj,
},
},
}
for topic, info in topics.items():
print(f" {topic}:")
with open(info["path"], encoding="utf-8") as txt:
content = txt.read()
for key, value in info["key_values"].items():
# print(f"[INFO] replace: {env_var_name}")
if not key in content:
print(f" {key=} not found in {info['path']}")
content = content.replace(key, value)
with open(info["path"], "w", encoding="utf-8") as txt:
txt.write(content)
print(f" written: {info['path']}")
print("[INFO] inserting dynamic chocolatey package parameters successful")
2024-07-04 08:29:25 +00:00
print("\n_____ build choco.exe with mono")
2024-07-03 12:52:32 +00:00
choco_version = "1.1.0"
2026-03-17 13:39:10 +05:00
run(f"wget https://github.com/chocolatey/choco/archive/refs/tags/{choco_version}.tar.gz --quiet")
run(f"tar -xzf {choco_version}.tar.gz")
2024-07-03 12:52:32 +00:00
print("choco tar unpack successful")
os.chdir("choco-1.1.0")
2026-03-17 13:39:10 +05:00
run("./build.sh")
2024-07-03 12:52:32 +00:00
2026-03-17 13:39:10 +05:00
run("cp -r build_output/chocolatey /opt/chocolatey")
2024-07-04 08:29:25 +00:00
os.chdir(BLENDERBIM_DIR)
2024-07-03 12:52:32 +00:00
if pathlib.Path("/opt/chocolatey/choco.exe").exists():
print("choco build successful")
print("\n_____ build choco pack")
2026-03-17 13:39:10 +05:00
run("mono /opt/chocolatey/choco.exe pack --allow-unofficial")
run(
'mono /opt/chocolatey/choco.exe setapikey --key="{choco_token}" --source="https://push.chocolatey.org/" --allow-unofficial'
)
2024-07-03 12:52:32 +00:00
print("\n_____ build choco push")
2026-03-17 13:39:10 +05:00
run(
'mono /opt/chocolatey/choco.exe push --source="https://push.chocolatey.org/" --key="$CHOCO_TOKEN" --allow-unofficial --verbose'
)
2024-07-03 12:52:32 +00:00
print(f"choco push of version: {target_release_tag} successful!")
print(f"it took: {datetime.datetime.now() - start}")