Skip to content

Commit

Permalink
Fix python source distributions builds (#360)
Browse files Browse the repository at this point in the history
* Append dev version only if running in git

* Fix missing file in manifest made sdist builds fail

* Fix dev versioning in sdist
  • Loading branch information
tlifschitz authored Nov 14, 2024
1 parent 7781a07 commit df4ec85
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ include simplepyble/src/wrap_descriptor.cpp
include simplepyble/src/wrap_peripheral.cpp
include simplepyble/src/wrap_service.cpp
include simplepyble/src/wrap_types.cpp
include external/include/external/kvn_bytearray.h
prune .github
prune docs
prune examples
Expand Down
50 changes: 43 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import pybind11
import skbuild

from pathlib import Path
from setuptools.command.sdist import sdist

def exclude_unnecessary_files(cmake_manifest):
def is_necessary(name):
Expand All @@ -22,6 +24,12 @@ def is_necessary(name):

return list(filter(is_necessary, cmake_manifest))

def is_git_repo():
try:
subprocess.check_output(['git', 'rev-parse', '--git-dir'], stderr=subprocess.DEVNULL)
return True
except (subprocess.SubprocessError, FileNotFoundError):
return False

def get_commit_since_hash(hash_cmd):
result = subprocess.run(hash_cmd.split(' '),
Expand Down Expand Up @@ -75,13 +83,21 @@ def is_current_commit_tagged():
root = pathlib.Path(__file__).parent.resolve()

# Generate the version string
version_str = (root / "VERSION").read_text(encoding="utf-8").strip()

is_tagged, tag = is_current_commit_tagged()
if not is_tagged:
N = get_commits_since_version_bump()
if N>0:
version_str += f".dev{N-1}"
def get_version():
root = Path(__file__).parent

version_str = (root / "VERSION").read_text(encoding="utf-8").strip()
if is_git_repo():
is_tagged, tag = is_current_commit_tagged()
if not is_tagged:
N = get_commits_since_version_bump()
if N > 0:
version_str += f".dev{N-1}"
# If we are not in a git repo and running from a source distribution, the VERSION
# file has already been updated with the corresponding dev version if necessary.
return version_str

version_str = get_version()

# Get the long description from the README file
long_description = (root / "simplepyble" / "README.rst").read_text(encoding="utf-8")
Expand All @@ -101,6 +117,23 @@ def is_current_commit_tagged():
if 'PIWHEELS_BUILD' in os.environ:
cmake_options.append("-DLIBFMT_VENDORIZE=OFF")

class CustomSdist(sdist):
def make_release_tree(self, base_dir, files):

# First, let the parent class create the release tree
super().make_release_tree(base_dir, files)

version = get_version()

if "dev" in version:
# Dev versions are generated dynamically.
# Update the VERSION file in the release tree
# so it matches the one published on PyPi
version_file = Path(base_dir) / "VERSION"
if version_file.exists():
version_file.write_text(version + "\n", encoding="utf-8")
print(f"Updated VERSION file in sdist to: {version}")

# The information here can also be placed in setup.cfg - better separation of
# logic and declaration, and simpler if you include description/version in a file.
skbuild.setup(
Expand Down Expand Up @@ -140,4 +173,7 @@ def is_current_commit_tagged():
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3 :: Only",
],
cmdclass={
'sdist': CustomSdist,
},
)

0 comments on commit df4ec85

Please sign in to comment.