From 18ea280388d5bb204bbd15eb6852be913a156202 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Wed, 28 Aug 2024 18:04:16 +0200 Subject: [PATCH] Check if pip package already available before building --- setup.py | 12 ++-------- tests/build_pip.ps1 | 5 ++++ tests/build_pip.sh | 2 ++ tests/build_pip_mac.sh | 2 ++ tests/utils.py | 54 +++++++++++++++++++++++++++++++----------- 5 files changed, 51 insertions(+), 24 deletions(-) diff --git a/setup.py b/setup.py index 7e9492c9..4c55d907 100644 --- a/setup.py +++ b/setup.py @@ -42,16 +42,8 @@ def is_dev_build(): return False return True -git_version = check_output(['git', 'describe', '--tags']).decode('utf-8').strip() -version = git_version[1:].split('-') -if len(version)>2: - version = version[:2] -if len(version)>1: - version = '.post'.join(version) - if is_dev_build(): - version += '.dev0' -else: - version = version[0] +git_version = check_output([sys.executable, os.path.join('tests', 'utils.py'), '--get-git-version']).decode('utf-8').strip() +version = check_output([sys.executable, os.path.join('tests', 'utils.py'), '--get-version']).decode('utf-8').strip() py_install_dir = os.path.relpath(sysconfig.get_path('platlib'), sysconfig.get_path('data')).replace('\\','/') diff --git a/tests/build_pip.ps1 b/tests/build_pip.ps1 index c6cbe723..2692af82 100644 --- a/tests/build_pip.ps1 +++ b/tests/build_pip.ps1 @@ -10,6 +10,11 @@ $env:NETGEN_ARCH = 'avx2' $pydir=$args[0] & $pydir\python.exe --version +& $pydir\python.exe -m pip install packaging +& $pydir\python.exe tests\utils.py --check-pip +if ($LASTEXITCODE -ne 0) { + exit 0 +} & $pydir\python.exe -m pip install scikit-build wheel numpy twine pybind11-stubgen & $pydir\python.exe -m pip install --upgrade netgen-occt==7.8.1 netgen-occt-devel==7.8.1 & $pydir\python setup.py bdist_wheel -G"Visual Studio 16 2019" diff --git a/tests/build_pip.sh b/tests/build_pip.sh index fb132ff1..533d1173 100755 --- a/tests/build_pip.sh +++ b/tests/build_pip.sh @@ -23,6 +23,8 @@ for pyversion in 312 311 310 39 38 do export PYDIR="/opt/python/cp${pyversion}-cp${pyversion}/bin" echo $PYDIR + $PYDIR/pip install requests packaging + $PYDIR/python3 ./tests/utils.py --check-pip || continue $PYDIR/pip install -U pytest-check numpy wheel scikit-build pybind11-stubgen netgen-occt==7.8.1 netgen-occt-devel==7.8.1 $PYDIR/pip install -i https://pypi.anaconda.org/mpi4py/simple/ --pre mpi4py diff --git a/tests/build_pip_mac.sh b/tests/build_pip_mac.sh index 31c48a34..d6f6cb7a 100755 --- a/tests/build_pip_mac.sh +++ b/tests/build_pip_mac.sh @@ -7,6 +7,8 @@ export PATH=$PYDIR:/Applications/CMake.app/Contents/bin:$PATH export NETGEN_CCACHE=1 $PYDIR/python3 --version +$PYDIR/python3 -m pip install packaging +$PYDIR/python3 tests/utils.py --check-pip || exit 0 $PYDIR/python3 -m pip install --user numpy twine scikit-build wheel pybind11-stubgen $PYDIR/python3 -m pip install --user -U netgen-occt==7.8.1 netgen-occt-devel==7.8.1 diff --git a/tests/utils.py b/tests/utils.py index 3be478e9..2554d7be 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,15 +1,32 @@ -from subprocess import check_output +import argparse import os -import platform import requests import sys -import argparse +from subprocess import check_output +from packaging import tags +from packaging.utils import parse_wheel_filename + + +_sys_tags = None + + +def _is_wheel_compatible(wheel_filename: str): + global _sys_tags + try: + if _sys_tags is None: + _sys_tags = set(tags.sys_tags()) + + for tag in parse_wheel_filename(wheel_filename)[-1]: + if tag in _sys_tags: + return True + + return False + except Exception as e: + print(f"Error parsing wheel file: {e}") + return False def is_package_available(package_name, version): - architecture = platform.machine() - py_version = "cp" + "".join(platform.python_version_tuple()[:2]) - url = f"https://pypi.org/pypi/{package_name}/{version}/json" try: @@ -21,7 +38,7 @@ def is_package_available(package_name, version): for file_info in data["urls"]: name = file_info.get("filename", "") - if name.endswith(".whl") and py_version in name and architecture in name: + if _is_wheel_compatible(name): return True return False @@ -42,10 +59,12 @@ def is_dev_build(): return True +def get_git_version(cwd): + return check_output(["git", "describe", "--tags"], cwd=cwd).decode("utf-8").strip() + + def get_version(cwd): - git_version = ( - check_output(["git", "describe", "--tags"], cwd=cwd).decode("utf-8").strip() - ) + git_version = get_git_version(cwd) version = git_version[1:].split("-") if len(version) > 2: @@ -53,7 +72,7 @@ def get_version(cwd): if len(version) > 1: version = ".post".join(version) if is_dev_build(): - version += ".dev0" + version += ".dev2" else: version = version[0] @@ -67,6 +86,11 @@ def main(): action="store_true", help="Check if package is on pypi already, fails with exit code 1 if available", ) + parser.add_argument( + "--get-git-version", + action="store_true", + help="Generate the current package git version string", + ) parser.add_argument( "--get-version", action="store_true", @@ -82,10 +106,12 @@ def main(): args = parser.parse_args() - version = get_version(args.dir) - if args.get_version: - print(version) + if args.get_git_version: + print(get_git_version(args.dir)) + elif args.get_version: + print(get_version(args.dir)) elif args.check_pip: + version = get_version(args.dir) if is_package_available(args.package, version): print(f"{args.package}=={version} is already on pypi") sys.exit(1)