Skip to content

Commit 6fd8ba9

Browse files
authored
Merge pull request #26 from qsharp-community/bugfix/pythonn-packaging
improve Python packaging
2 parents 4d2d0a3 + 08be21a commit 6fd8ba9

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ jobs:
3333
run: |
3434
python -m pip install --upgrade pip setuptools wheel
3535
python setup.py sdist bdist_wheel
36+
env:
37+
PACKAGE_VERSION: 0.99.0
3638

3739
- name: Upload Python Package Artifact
3840
uses: actions/upload-artifact@v4

.github/workflows/release.yml

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,75 @@ jobs:
1818
steps:
1919
- name: Checkout code
2020
uses: actions/checkout@v3
21+
22+
- name: Set version from tag
23+
id: get_version
24+
run: |
25+
if [ "${{ github.event_name }}" = "release" ]; then
26+
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
27+
else
28+
echo "VERSION=${{ github.event.inputs.tag_name }}" >> $GITHUB_ENV
29+
fi
30+
shell: bash
31+
32+
- name: Show version
33+
run: echo "Building version ${{ env.VERSION }}"
34+
shell: bash
35+
2136
- name: Install uniffi-bindgen-cs
2237
run: |
2338
cargo install uniffi-bindgen-cs --git https://github.com/NordSecurity/uniffi-bindgen-cs --tag v0.2.0
39+
2440
- name: Build Rust Library
2541
run: |
2642
cargo build --release --manifest-path ${{ github.workspace }}/Cargo.toml
43+
2744
- name: Build Python Package
2845
working-directory: platforms/python/qsharp-bridge
2946
run: |
3047
python -m pip install --upgrade pip setuptools wheel
3148
python setup.py sdist bdist_wheel
49+
env:
50+
PACKAGE_VERSION: ${{ env.VERSION }}
51+
3252
- name: Upload wheels to release
3353
uses: softprops/action-gh-release@v1
3454
with:
3555
files: platforms/python/qsharp-bridge/dist/*.whl
36-
56+
3757
build-swift-package:
3858
runs-on: macos-15
3959
steps:
4060
- name: Checkout code
4161
uses: actions/checkout@v3
62+
63+
- name: Set version from tag
64+
id: get_version
65+
run: |
66+
if [ "${{ github.event_name }}" = "release" ]; then
67+
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
68+
else
69+
echo "VERSION=${{ github.event.inputs.tag_name }}" >> $GITHUB_ENV
70+
fi
71+
4272
- name: Install Rust targets
4373
run: |
4474
rustup target add aarch64-apple-ios
4575
rustup target add aarch64-apple-ios-sim
4676
rustup target add aarch64-apple-darwin
77+
4778
- name: Build Swift Package
4879
run: ./build_swift.sh
80+
4981
- name: Clean temporary build files from Swift Package
5082
run: rm -rf platforms/swift/Qsharp.Bridge/.build
83+
5184
- name: Create zip archive of Swift Package
5285
run: |
5386
cd platforms/swift
5487
zip -r Qsharp.Bridge-SwiftPackage.zip Qsharp.Bridge
88+
5589
- name: Upload Swift Package to release
5690
uses: softprops/action-gh-release@v1
5791
with:
58-
files: platforms/swift/Qsharp.Bridge-SwiftPackage.zip
92+
files: platforms/swift/Qsharp.Bridge-SwiftPackage.zip

platforms/python/qsharp-bridge/setup.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from setuptools import setup, find_packages
66
from setuptools.command.build_py import build_py as _build_py
77

8-
# Import the bdist_wheel command from wheel
98
try:
109
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
1110
except ImportError:
@@ -15,6 +14,7 @@
1514
CARGO_MANIFEST_PATH = os.path.abspath(os.path.join(HERE, "../../../Cargo.toml"))
1615
CARGO_TARGET_DIR = os.path.abspath(os.path.join(HERE, "../../../target/release"))
1716
BINDINGS_SRC = os.path.abspath(os.path.join(HERE, "../../../bindings/qsharp_bridge.py"))
17+
VERSION = os.environ.get("PACKAGE_VERSION", "0.1.0")
1818

1919
def get_lib_filename():
2020
"""
@@ -47,39 +47,56 @@ def run(self):
4747
"cargo", "build", "--release",
4848
"--manifest-path", CARGO_MANIFEST_PATH
4949
])
50-
5150
# 2. copy the native library from Cargo's output folder into the Python package.
5251
src_lib = os.path.join(CARGO_TARGET_DIR, lib_filename)
5352
dst_dir = os.path.join(HERE, "qsharp_bridge")
5453
dst_lib = os.path.join(dst_dir, lib_filename)
5554
self.mkpath(dst_dir)
5655
print(f"Copying native library: {src_lib} -> {dst_lib}")
5756
self.copy_file(src_lib, dst_lib)
58-
5957
# 3. copy uniFFI bindings file from the bindings/ folder.
6058
dst_binding = os.path.join(dst_dir, "qsharp_bridge.py")
6159
if os.path.exists(BINDINGS_SRC):
6260
print(f"Copying binding file: {BINDINGS_SRC} -> {dst_binding}")
6361
self.copy_file(BINDINGS_SRC, dst_binding)
6462
else:
6563
print("Warning: Binding file not found at", BINDINGS_SRC)
66-
6764
# 4. continue with the standard build process.
6865
super().run()
6966

7067
if _bdist_wheel:
7168
class bdist_wheel(_bdist_wheel):
7269
def finalize_options(self):
70+
self.plat_name_supplied = True
71+
72+
# Set the platform tag based on the system
73+
if sys.platform.startswith('linux'):
74+
self.plat_name = "py3-none-linux_x86_64"
75+
elif sys.platform.startswith('darwin'):
76+
self.plat_name = "py3-none-macosx_11_0_universal2"
77+
elif sys.platform.startswith('win'):
78+
self.plat_name = "py3-none-win_amd64"
79+
else:
80+
# Fall back to default behavior for unknown platforms
81+
self.plat_name_supplied = False
82+
7383
super().finalize_options()
74-
# mark the wheel as not pure so that a platform tag is used
75-
# I am not sure I know what I am doing, but this sounds right
84+
# We still need to mark it as not pure Python
7685
self.root_is_pure = False
86+
87+
def get_tag(self):
88+
# Override the tag generation to use our custom platform tag
89+
if self.plat_name_supplied:
90+
# Use py3 instead of cpXY to support any Python 3.x version
91+
return ('py3', 'none', self.plat_name.split('-')[-1])
92+
# Fall back to default behavior
93+
return super().get_tag()
7794
else:
7895
bdist_wheel = None
7996

8097
setup(
8198
name="qsharp-bridge",
82-
version="0.1.0",
99+
version=VERSION,
83100
description="Cross platform library for accessing Q# features in a simple way",
84101
author="Filip w",
85102
author_email="contact@strathweb.com",
@@ -91,4 +108,4 @@ def finalize_options(self):
91108
"Programming Language :: Python :: 3",
92109
],
93110
python_requires=">=3.6",
94-
)
111+
)

0 commit comments

Comments
 (0)