Skip to content

Commit

Permalink
provide sdist
Browse files Browse the repository at this point in the history
  • Loading branch information
JanSellner committed Feb 12, 2025
1 parent cc1621c commit dc127ad
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 26 deletions.
47 changes: 44 additions & 3 deletions .github/workflows/build_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,63 @@ jobs:
- uses: actions/checkout@v4

# cibuildwheel is configured via pyproject.toml
- name: cibuildwheel
- name: Build wheels with cibuildwheel
uses: pypa/[email protected]
env:
# Otherwise PyTorch builds may fail on MacOS: https://cibuildwheel.readthedocs.io/en/stable/cpp_standards/#macos-and-deployment-target-versions
CIBW_ENVIRONMENT: CI_COMMIT_TAG="${{ github.ref_name }}" MACOSX_DEPLOYMENT_TARGET="10.14"
# Force UTF-8 mode (especially for Windows)
PYTHONUTF8: 1

- name: upload_artifacts
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-${{ matrix.os }}
path: ./wheelhouse/*.whl

build_sdist:
name: Build source distribution (sdist)
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest]
python-version: ["3.13"]

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"
cache-dependency-path: "dependencies/requirements*.txt"

- name: Install build dependencies
run: pip install build

- name: Build sdist
run: python -m build . --sdist

- name: Upload sdist package
uses: actions/upload-artifact@v4
with:
name: wheels-source
path: dist/*.tar.gz

- name: Build building extension from sdist package
env:
HTC_ADD_NETWORK_ALTERNATIVES: false
run: |
pip install -r dependencies/requirements-tests.txt
pip install torch
pip install dist/*.tar.gz
- name: Basic test for the sdist package
run: py.test --doctest-modules --import-mode=importlib --collect-only --pyargs "htc" "htc_projects"

upload_pypi:
needs: build_wheels
needs: [build_wheels, build_sdist]
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
Expand Down
42 changes: 19 additions & 23 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,17 @@
from pathlib import Path

from setuptools import find_namespace_packages, setup
from setuptools.command.sdist import sdist
from torch.utils import cpp_extension


def read_file(path: Path) -> str:
"""
Reads the content of a file into a string.
Args:
path: Path to the file to read.
Returns:
The content of the file.
"""
with path.open() as f:
content = f.read()

return content
class ExtendedSdist(sdist):
def make_distribution(self):
# The requirements files need to be part of the source distribution as otherwise setuptools can't build the package
# The wheels don't need the requirements files as they have already been parsed and added to the package metadata
self.filelist.append(requirements)
self.filelist.append(requirements_extra)
super().make_distribution()


def parse_requirements(requirements_file: Path) -> list[str]:
Expand All @@ -36,7 +30,7 @@ def parse_requirements(requirements_file: Path) -> list[str]:
"""
req = []

for line in read_file(requirements_file).splitlines():
for line in requirements_file.read_text().splitlines():
match = re.search(r"^\w+[^+@]*", line)
if match is not None:
lib = match.group(0)
Expand All @@ -55,7 +49,7 @@ def parse_readme(readme_file: Path) -> str:
Returns: Readme as string with adjusted links.
"""
readme = read_file(readme_file)
readme = readme_file.read_text()

# Make local anchors and links to files absolute since they don't work on PyPi
readme = re.sub(r"\(\./([^)]+)\)", r"(https://github.com/IMSY-DKFZ/htc/tree/main/\1)", readme)
Expand All @@ -65,6 +59,8 @@ def parse_readme(readme_file: Path) -> str:


repo_root = Path(__file__).parent
requirements = "dependencies/requirements.txt"
requirements_extra = "dependencies/requirements-extra.txt"

source_files = sorted(repo_root.rglob("htc/cpp/*.cpp"))
source_files = [str(f.relative_to(repo_root)) for f in source_files]
Expand All @@ -76,7 +72,7 @@ def parse_readme(readme_file: Path) -> str:

setup(
name="imsy-htc",
version="0.0.19",
version="0.0.20",
# We are using find_namespace_packages() instead of find_packages() to resolve this deprecation warning: https://github.com/pypa/setuptools/issues/3340
packages=find_namespace_packages(include=["htc*"]),
author="Division of Intelligent Medical Systems, DKFZ",
Expand All @@ -95,9 +91,9 @@ def parse_readme(readme_file: Path) -> str:
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"License :: OSI Approved :: MIT License",
"Operating System :: POSIX :: Linux",
"Operating System :: Microsoft :: Windows",
Expand All @@ -111,12 +107,12 @@ def parse_readme(readme_file: Path) -> str:
],
long_description=parse_readme(repo_root / "README.md"),
long_description_content_type="text/markdown",
python_requires=">=3.10",
install_requires=parse_requirements(repo_root / "dependencies" / "requirements.txt"),
python_requires=">=3.11",
install_requires=parse_requirements(repo_root / requirements),
extras_require={
"extra": parse_requirements(repo_root / "dependencies" / "requirements-extra.txt"),
"extra": parse_requirements(repo_root / requirements_extra),
},
include_package_data=True,
package_data={"": ["*.json", "*.h", "*.js"]},
entry_points={
"console_scripts": ["htc=htc.cli:main"],
},
Expand All @@ -127,5 +123,5 @@ def parse_readme(readme_file: Path) -> str:
extra_compile_args=compiler_flags,
)
],
cmdclass={"build_ext": cpp_extension.BuildExtension},
cmdclass={"build_ext": cpp_extension.BuildExtension, "sdist": ExtendedSdist},
)

0 comments on commit dc127ad

Please sign in to comment.