Skip to content

Commit

Permalink
Python package change && PyPI Support (#4)
Browse files Browse the repository at this point in the history
Resolve #3 . Resolve #8 . Close #11 .

---------

Co-authored-by: Bet4 <0xbet4@gmail.com>
  • Loading branch information
wtdcode and bet4it authored Jan 8, 2025
1 parent 9c5fd71 commit 373a8ff
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 75 deletions.
86 changes: 86 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: PyPI 📦 Distribution

on:
push:
pull_request:

jobs:
build:
strategy:
fail-fast: false
matrix:
include:
- name: windows_x86_64
os: windows-latest
- name: macos_x86_64
os: macos-13
- name: macos_arm64
os: macos-15
- name: manylinux2014_i686
os: ubuntu-latest
target: i686
container: messense/manylinux2014-cross:i686
- name: manylinux2014_x86_64
os: ubuntu-latest
target: x86_64
container: messense/manylinux2014-cross:x86_64
- name: manylinux2014_aarch64
os: ubuntu-latest
target: aarch64
container: messense/manylinux2014-cross:aarch64

name: ${{ matrix.name }}
runs-on: ${{ matrix.os }}
container: ${{ matrix.container }}

steps:
- uses: actions/checkout@v4

- name: '🦀️ Set up Rust'
if: matrix.os == 'ubuntu-latest'
uses: dtolnay/rust-toolchain@stable
with:
target: ${{ matrix.target }}-unknown-linux-gnu

- name: '🐍 Set up Python'
uses: actions/setup-python@v5
with:
python-version: 3.12

- name: '🔧 Install pkg-config'
if: matrix.os == 'ubuntu-latest'
run: |
apt update
apt install -y pkg-config
- name: '🛠️ Install Python build'
run: |
python -m pip install build
- name: '🏗️ Build distribution'
run: |
cd bindings/python
python -m build --wheel --config-setting=--build-option=--plat-name=${{ matrix.name }}
- name: '📤 Upload artifact'
uses: actions/upload-artifact@v4
with:
name: python-udbserver-${{ matrix.name }}
path: ${{ github.workspace }}/bindings/python/dist/*

publish:
needs: build
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags')
steps:
- uses: actions/download-artifact@v4
with:
pattern: python-udbserver-*
merge-multiple: true
path: dist

- name: '🚀 Publish distribution to PyPI'
uses: pypa/gh-action-pypi-publish@master
with:
user: __token__
password: ${{ secrets.pypi_pass }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/target
/build
build
dist
udbserver.egg-info
Cargo.lock
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ categories = ["emulators"]
keywords = ["gdb", "debugging", "emulator"]

[lib]
name = "udbserver"
crate-type = ["lib"]

[features]
Expand Down
10 changes: 0 additions & 10 deletions bindings/python/README.md

This file was deleted.

23 changes: 23 additions & 0 deletions bindings/python/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Python bindings for udbserver
=========================

This package provides Python bindings for udbserver, allowing you to debug your Unicorn-based projects with GDB.
For more details about udbserver, please check the `project homepage <https://github.com/bet4it/udbserver>`_.

Installation
-----------

From PyPI
~~~~~~~~~

It's highly recommended to install the Python package via pip::

pip install udbserver

From source
~~~~~~~~~~

To build and install this package manually::

python3 -m build --wheel
pip install dist/*.whl
File renamed without changes.
30 changes: 30 additions & 0 deletions bindings/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[build-system]
requires = ["setuptools", "wheel", "setuptools-rust", "setuptools_scm[toml]"]
build-backend = "setuptools.build_meta"

[project]
name = "udbserver"
dynamic = ["version"]
license = { text = "MIT License" }
description = "Python bindings of udbserver"
readme = "README.rst"
authors = [{ name = "Bet4", email = "0xbet4@gmail.com" }]
urls = { homepage = "https://github.com/bet4it/udbserver" }
classifiers = [
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Operating System :: POSIX",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
"Topic :: Software Development :: Debuggers",
]
dependencies = [
"unicorn >= 2",
]

[tool.setuptools]
zip-safe = false

[tool.setuptools_scm]
root = "../.."
45 changes: 21 additions & 24 deletions bindings/python/setup.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
#!/usr/bin/env python3
# encoding: utf-8
import os
import sysconfig
from setuptools import setup
from setuptools_rust import Binding, RustExtension
from setuptools.command.build_ext import build_ext
from wheel.bdist_wheel import bdist_wheel

from setuptools import setup, Extension
class CustomBuildExt(build_ext):
def get_ext_filename(self, ext_name):
filename = super().get_ext_filename(ext_name)
suffix = sysconfig.get_config_var('EXT_SUFFIX')
ext = os.path.splitext(filename)[1]
return filename.replace(suffix, '') + ext

class CustomBdistWheel(bdist_wheel):
def get_tag(self):
_, _, plat = super().get_tag()
return ('py3', 'none', plat)

rust_module = Extension('udbserver',
sources=['udbserver.c'],
libraries=['udbserver'],
)

setup (name = 'udbserver',
version = '0.1',
author = 'Bet4',
author_email = '0xbet4@gmail.com',
description = 'Python bindings of udbserver',
url = 'https://github.com/bet4it/udbserver',
license='MIT License',
classifiers=[
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Topic :: Software Development :: Debuggers',
],
ext_modules = [rust_module],
py_modules = [],
)
setup(
name='udbserver',
rust_extensions=[RustExtension('udbserver.libudbserver', binding=Binding.NoBinding, path='../../Cargo.toml', features=['capi'])],
cmdclass={'build_ext': CustomBuildExt, 'bdist_wheel': CustomBdistWheel},
)
40 changes: 0 additions & 40 deletions bindings/python/udbserver.c

This file was deleted.

22 changes: 22 additions & 0 deletions bindings/python/udbserver/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import ctypes
import sys
from unicorn import Uc
from ctypes import c_void_p, c_uint16, c_uint64

_current_dir = os.path.dirname(os.path.abspath(__file__))

if sys.platform.startswith('win'):
_library_file = "libudbserver.pyd"
else:
_library_file = "libudbserver.so"

_udbserver_lib = ctypes.cdll.LoadLibrary(os.path.join(_current_dir, _library_file))

_udbserver_lib.udbserver.argtypes = [c_void_p, c_uint16, c_uint64]
_udbserver_lib.udbserver.restype = None

def udbserver(uc: Uc, port: int = 1234, start_addr: int = 0):
"""Start udbserver.
"""
_udbserver_lib.udbserver(int(uc._uch.value), port, start_addr)

0 comments on commit 373a8ff

Please sign in to comment.