Skip to content

Commit 3f31c7a

Browse files
authored
Build with setuptools scm (#292)
* switch over versioning handling to use setuptools-scm * fix errors * update build action * fetch all tags * set fetch-depth to 0 * use ubuntu-22.04 * update nm-action tag
1 parent 7eb461e commit 3f31c7a

File tree

6 files changed

+72
-96
lines changed

6 files changed

+72
-96
lines changed

.github/workflows/build-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ on:
2121
build_label:
2222
description: "requested runner label for build (specifies instance)"
2323
type: string
24-
default: ubuntu-24.04
24+
default: ubuntu-22.04
2525

2626
# test related parameters
2727
test_configs:

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ jobs:
7070
id: checkout
7171
uses: actions/checkout@v4
7272
with:
73+
fetch-depth: 0
74+
fetch-tags: true
7375
ref: ${{ inputs.gitref }}
7476

7577
- name: build
7678
id: build
77-
uses: neuralmagic/nm-actions/actions/build-ml-whl@v1.12.0
79+
uses: neuralmagic/nm-actions/actions/build-ml-whl@v1.17.0
7880
with:
7981
dev: false
8082
release: ${{ inputs.wf_category == 'RELEASE' }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ share/python-wheels/
2525
.installed.cfg
2626
*.egg
2727
MANIFEST
28+
src/compressed_tensors/version.py
2829

2930
# PyInstaller
3031
# Usually these files are written by a python script from a template

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
[build-system]
2+
requires = ["setuptools", "wheel", "setuptools_scm>8"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[tool.setuptools_scm]
6+
version_file = "src/compressed_tensors/version.py"
7+
18
[tool.black]
29
line-length = 88
310
target-version = ['py36']

setup.py

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,66 @@
1717
from typing import List, Dict, Tuple
1818

1919

20-
def get_release_and_version(package_path: str) -> Tuple[bool, bool, str, str, str, str]:
21-
"""
22-
Load version and release info from compressed-tensors package
23-
"""
24-
# compressed-tensors/src/compressed_tensors/version.py always exists, default source of truth
25-
version_path = os.path.join(package_path, "version.py")
26-
27-
# exec() cannot set local variables so need to manually
28-
locals_dict = {}
29-
exec(open(version_path).read(), globals(), locals_dict)
30-
is_release = locals_dict.get("is_release", False)
31-
version = locals_dict.get("version", "unknown")
32-
version_major = locals_dict.get("version_major", "unknown")
33-
version_minor = locals_dict.get("version_minor", "unknown")
34-
version_bug = locals_dict.get("version_bug", "unknown")
35-
36-
print(f"Loaded version {version} from {version_path}")
37-
38-
return (
39-
is_release,
40-
version,
41-
version_major,
42-
version_minor,
43-
version_bug,
20+
# Set the build type using an environment variable to give us
21+
# different package names based on the reason for the build.
22+
VALID_BUILD_TYPES = {"release", "nightly", "dev"}
23+
BUILD_TYPE = os.environ.get("BUILD_TYPE", "dev")
24+
if BUILD_TYPE not in VALID_BUILD_TYPES:
25+
raise ValueError(
26+
f"Unsupported build type {BUILD_TYPE!r}, must be one of {VALID_BUILD_TYPES}"
4427
)
4528

29+
from setuptools_scm import ScmVersion
30+
31+
def version_func(version: ScmVersion) -> str:
32+
from setuptools_scm.version import guess_next_version
33+
34+
if BUILD_TYPE == "nightly":
35+
# Nightly builds use alpha versions to ensure they are marked
36+
# as pre-releases on pypi.org.
37+
return version.format_next_version(
38+
guess_next=guess_next_version,
39+
fmt="{guessed}.a{node_date:%Y%m%d}",
40+
)
41+
42+
if (
43+
BUILD_TYPE == "release"
44+
and not version.dirty
45+
and (version.exact or version.node is None)
46+
):
47+
# When we have a tagged version, use that without modification.
48+
return version.format_with("{tag}")
49+
50+
# In development mode or when the local repository is dirty, treat
51+
# it is as local development version.
52+
return version.format_next_version(
53+
guess_next=guess_next_version,
54+
fmt="{guessed}.dev{distance}",
55+
)
4656

47-
package_path = os.path.join(
48-
os.path.dirname(os.path.realpath(__file__)), "src", "compressed_tensors"
49-
)
50-
(
51-
is_release,
52-
version,
53-
version_major,
54-
version_minor,
55-
version_bug,
56-
) = get_release_and_version(package_path)
5757

58-
version_nm_deps = f"{version_major}.{version_minor}.0"
58+
def localversion_func(version: ScmVersion) -> str:
59+
from setuptools_scm.version import get_local_node_and_date
60+
61+
# When we are building nightly versions, we guess the next release
62+
# and add the date as an alpha version. We cannot publish packages
63+
# with local versions, so we do not add one.
64+
if BUILD_TYPE == "nightly":
65+
return ""
66+
67+
# When we have an exact tag, with no local changes, do not append
68+
# anything to the local version field.
69+
if (
70+
BUILD_TYPE == "release"
71+
and not version.dirty
72+
and (version.exact or version.node is None)
73+
):
74+
return ""
5975

60-
if is_release:
61-
_PACKAGE_NAME = "compressed-tensors"
62-
else:
63-
_PACKAGE_NAME = "compressed-tensors-nightly"
76+
# In development mode or when the local repository is dirty,
77+
# return a string that includes the git SHA (node) and a date,
78+
# formatted as a local version tag.
79+
return get_local_node_and_date(version)
6480

6581

6682
def _setup_long_description() -> Tuple[str, str]:
@@ -81,8 +97,11 @@ def _setup_extras() -> Dict:
8197
}
8298

8399
setup(
84-
name=_PACKAGE_NAME,
85-
version=version,
100+
name="compressed-tensors",
101+
use_scm_version={
102+
"version_scheme": version_func,
103+
"local_scheme": localversion_func,
104+
},
86105
author="Neuralmagic, Inc.",
87106
author_email="support@neuralmagic.com",
88107
license="Apache 2.0",

src/compressed_tensors/version.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)