Skip to content

Commit 8aa2b73

Browse files
committed
fix: pack one binary per platform into python wheels
1 parent c61da24 commit 8aa2b73

File tree

2 files changed

+81
-16
lines changed

2 files changed

+81
-16
lines changed

packaging/pack.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
ROOT = File.join(__dir__, "..")
1010
DIST = File.join(ROOT, "dist")
1111

12+
OPERATING_SYSTEMS = ["linux", "darwin", "freebsd", "openbsd", "windows"]
13+
ARCHITECTURES = ["x86_64", "arm64"]
14+
PLATFORMS = OPERATING_SYSTEMS.product(ARCHITECTURES).map { |os, arch| { os: os, arch: arch } }
15+
1216
module Pack
1317
extend FileUtils
1418

@@ -164,8 +168,24 @@ def publish_gem
164168

165169
def publish_pypi
166170
puts "Publishing to PyPI..."
167-
cd(File.join(__dir__, "pypi"))
168-
system("python setup.py sdist bdist_wheel", exception: true)
171+
pypi_dir = File.join(__dir__, "pypi")
172+
173+
puts "Building source distribution..."
174+
cd(pypi_dir)
175+
system("python setup.py sdist", exception: true)
176+
177+
PLATFORMS.each do |platform|
178+
puts "Building wheel for #{platform[:os]}-#{platform[:arch]}..."
179+
env = {
180+
"LEFTHOOK_TARGET_PLATFORM" => platform[:os],
181+
"LEFTHOOK_TARGET_ARCH" => platform[:arch]
182+
}
183+
184+
cd(pypi_dir)
185+
system(env, "python setup.py bdist_wheel", exception: true)
186+
end
187+
188+
cd(pypi_dir)
169189
system("python -m twine upload --verbose --repository lefthook dist/*", exception: true)
170190
end
171191

packaging/pypi/setup.py

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,63 @@
1+
import os
2+
import sys
3+
import platform
14
from setuptools import setup, find_packages
5+
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
26

37
with open("README.md", "r") as fh:
48
long_description = fh.read()
59

10+
# Map Python's platform names to our binary folder names
11+
PLATFORM_MAPPING = {
12+
'linux': 'linux',
13+
'darwin': 'darwin',
14+
'win32': 'windows',
15+
'freebsd': 'freebsd',
16+
'openbsd': 'openbsd',
17+
}
18+
19+
ARCH_MAPPING = {
20+
'x86_64': 'x86_64',
21+
'AMD64': 'x86_64',
22+
'amd64': 'x86_64',
23+
'aarch64': 'arm64',
24+
'arm64': 'arm64',
25+
}
26+
27+
def get_platform_info():
28+
"""Determine the platform and architecture from environment or system."""
29+
target_platform, target_architecture = os.environ.get('LEFTHOOK_TARGET_PLATFORM'), os.environ.get('LEFTHOOK_TARGET_ARCH')
30+
31+
if target_platform and target_architecture:
32+
return target_platform, target_architecture
33+
34+
system, machine = platform.system().lower(), platform.machine().lower()
35+
return PLATFORM_MAPPING.get(sys.platform, system), ARCH_MAPPING.get(machine, machine)
36+
37+
class CustomBdistWheel(_bdist_wheel):
38+
"""Custom bdist_wheel that generates platform-specific wheels."""
39+
40+
def finalize_options(self):
41+
"""Initialize platform-specific options."""
42+
super().finalize_options()
43+
platform, architecture = get_platform_info()
44+
self.plat_name = f"{platform}_{architecture}"
45+
46+
def get_tag(self):
47+
"""Override to return platform-specific tag."""
48+
platform, architecture = get_platform_info()
49+
return ('py3', 'none', f'{platform}_{architecture}')
50+
51+
def get_package_data():
52+
"""Get only the binary for the current/target platform."""
53+
platform, architecture = get_platform_info()
54+
binary_name = 'lefthook'
55+
if platform == 'windows':
56+
binary_name = 'lefthook.exe'
57+
return {
58+
'lefthook': [f'bin/lefthook-{platform}-{architecture}/{binary_name}']
59+
}
60+
661
setup(
762
name='lefthook',
863
version='2.0.2',
@@ -18,22 +73,12 @@
1873
'lefthook=lefthook.main:main'
1974
],
2075
},
21-
package_data={
22-
'lefthook':[
23-
'bin/lefthook-linux-x86_64/lefthook',
24-
'bin/lefthook-linux-arm64/lefthook',
25-
'bin/lefthook-freebsd-x86_64/lefthook',
26-
'bin/lefthook-freebsd-arm64/lefthook',
27-
'bin/lefthook-openbsd-x86_64/lefthook',
28-
'bin/lefthook-openbsd-arm64/lefthook',
29-
'bin/lefthook-windows-x86_64/lefthook.exe',
30-
'bin/lefthook-windows-arm64/lefthook.exe',
31-
'bin/lefthook-darwin-x86_64/lefthook',
32-
'bin/lefthook-darwin-arm64/lefthook',
33-
]
76+
package_data=get_package_data(),
77+
license_files=['LICENSE'],
78+
cmdclass={
79+
'bdist_wheel': CustomBdistWheel,
3480
},
3581
classifiers=[
36-
'License :: OSI Approved :: MIT License',
3782
'Operating System :: OS Independent',
3883
'Topic :: Software Development :: Version Control :: Git'
3984
],

0 commit comments

Comments
 (0)