Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python bindings: Fully remove distutils and pkg_resources #2369

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .github/workflows/CITest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ jobs:
fail-fast: false
matrix:
config:
- {
name: 'ubuntu-20.04 x64 python3.6 cmake',
os: ubuntu-20.04,
arch: x64,
python-arch: x64,
python-version: '3.6',
build-system: 'cmake',
enable-asan: 'OFF'
}
- {
name: 'ubuntu-22.04 x64 python3.9 make',
os: ubuntu-22.04,
Expand Down Expand Up @@ -74,6 +65,14 @@ jobs:
build-system: 'cmake',
enable-asan: 'ON'
}
- {
name: 'ubuntu-22.04 x64 python3.12 cmake',
os: ubuntu-22.04,
arch: x64,
python-arch: x64,
python-version: '3.12',
build-system: 'cmake',
}

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -177,6 +176,7 @@ jobs:
- name: run python binding test
if: matrix.config.enable-asan == 'OFF'
run: |
pip install --upgrade setuptools
cp libcapstone.* bindings/python/prebuilt
cd bindings/python
make install
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/fuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ jobs:
with:
name: artifacts
path: ./out/artifacts
- name: Install Python Dependencies
run: python -m pip install --upgrade setuptools build wheel
9 changes: 5 additions & 4 deletions bindings/python/capstone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@
import ctypes, ctypes.util
from os.path import split, join, dirname
import sysconfig
import pkg_resources
from importlib import resources
from pathlib import PurePath

import inspect
if not hasattr(sys.modules[__name__], '__file__'):
Expand Down Expand Up @@ -415,17 +416,17 @@ def _load_lib(path):

# Loading attempts, in order
# - user-provided environment variable
# - pkg_resources can get us the path to the local libraries
# - importlib.resources can get us the path to the local libraries
# - we can get the path to the local libraries by parsing our filename
# - global load
# - python's lib directory
# - last-gasp attempt at some hardcoded paths on darwin and linux

_path_list = [os.getenv('LIBCAPSTONE_PATH', None),
pkg_resources.resource_filename(__name__, 'lib'),
resources.files(__name__) / "lib",
join(split(__file__)[0], 'lib'),
'',
sysconfig.get_path('platlib'),
PurePath(sysconfig.get_path('platlib')),
"/usr/local/lib/" if sys.platform == 'darwin' else '/usr/lib64']

for _path in _path_list:
Expand Down
3 changes: 3 additions & 0 deletions bindings/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
23 changes: 13 additions & 10 deletions bindings/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import sys
import platform

from distutils import log
import logging
from setuptools import setup
from distutils.util import get_platform
from distutils.command.build import build
from distutils.command.sdist import sdist
from sysconfig import get_platform
from setuptools.command.build import build
from setuptools.command.sdist import sdist
from setuptools.command.bdist_egg import bdist_egg

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

SYSTEM = sys.platform

# adapted from commit e504b81 of Nguyen Tan Cong
Expand Down Expand Up @@ -102,7 +105,7 @@ def copy_sources():

for filename in src:
outpath = os.path.join(SRC_DIR, os.path.basename(filename))
log.info("%s -> %s" % (filename, outpath))
logger.info("%s -> %s" % (filename, outpath))
shutil.copy(filename, outpath)

def build_libraries():
Expand All @@ -123,7 +126,7 @@ def build_libraries():
# if prebuilt libraries are available, use those and cancel build
if os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', LIBRARY_FILE)) and \
(not STATIC_LIBRARY_FILE or os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE))):
log.info('Using prebuilt libraries')
logger.info('Using prebuilt libraries')
shutil.copy(os.path.join(ROOT_DIR, 'prebuilt', LIBRARY_FILE), LIBS_DIR)
if STATIC_LIBRARY_FILE is not None:
shutil.copy(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE), LIBS_DIR)
Expand Down Expand Up @@ -167,9 +170,9 @@ def run(self):
class custom_build(build):
def run(self):
if 'LIBCAPSTONE_PATH' in os.environ:
log.info('Skipping building C extensions since LIBCAPSTONE_PATH is set')
logger.info('Skipping building C extensions since LIBCAPSTONE_PATH is set')
else:
log.info('Building C extensions')
logger.info('Building C extensions')
build_libraries()
return build.run(self)

Expand All @@ -191,7 +194,7 @@ def dummy_src():
from setuptools.command.develop import develop
class custom_develop(develop):
def run(self):
log.info("Building C extensions")
logger.info("Building C extensions")
build_libraries()
return develop.run(self)

Expand All @@ -218,7 +221,7 @@ def run(self):
url='https://www.capstone-engine.org',
long_description=open('README.txt', encoding="utf8").read(),
long_description_content_type='text/markdown',
python_requires='>=3.6',
python_requires='>=3.7',
classifiers=[
'License :: OSI Approved :: BSD License',
'Programming Language :: Python :: 3',
Expand Down
Loading