Skip to content

Commit 9fb9160

Browse files
authored
Merge pull request #104 from Thadah/Gpujana_issue59
Add support for PyPI packaging. Fixes #59
2 parents 021dda7 + 262fd66 commit 9fb9160

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ build/
1010
*.pyc
1111
.settings/
1212
docs/
13-
demoData/
13+
demoData/
14+
dist/
15+
openfhe/openfhe.so
16+
openfhe/*.pyi
17+
openfhe.egg-info/
18+
stubs/
19+
.venv/

build_package.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
# Exit on any error
3+
set -e
4+
5+
# Find the venv directory
6+
if [ -d ".venv" ]; then
7+
VENV_DIR=".venv"
8+
elif [ -d "../.venv" ]; then
9+
VENV_DIR="../.venv"
10+
else
11+
echo "The virtual environment does not exist. Please run 'python -m venv .venv' to create it." >&2
12+
exit 1
13+
fi
14+
15+
# Activate the virtual environment
16+
source $VENV_DIR/bin/activate
17+
18+
# Install pybind11-stubgen
19+
if ! pip show pybind11-stubgen > /dev/null; then
20+
pip install pybind11-stubgen
21+
fi
22+
23+
# Check if the virtual environment has the openfhe package installed
24+
if ! pip show openfhe > /dev/null; then
25+
echo "The openfhe package is not installed in the virtual environment. Please run 'pip install -e .' to install it." >&2
26+
exit 1
27+
fi
28+
29+
# Generate stub files using pybind11-stubgen
30+
echo "Generating stub files..."
31+
pybind11-stubgen openfhe
32+
33+
# Check if stub generation was successful
34+
if [ $? -eq 0 ]; then
35+
echo "Stub files generated successfully."
36+
else
37+
echo "Stub generation failed." >&2
38+
exit 1
39+
fi
40+
41+
# Move the generated stub files to the openfhe package directory
42+
echo "Moving the generated stub files to the openfhe package directory..."
43+
mv stubs/openfhe/* openfhe/
44+
rm -r -d stubs
45+
46+
# Build the source distribution and wheel distribution
47+
echo "Building the sdist and bdist_wheel..."
48+
python setup.py sdist bdist_wheel
49+
50+
# Indicate where the distributions were saved
51+
echo "The distributions have been built and are located in the 'dist' directory. You can install the package using 'pip install dist/<distribution_file>'."

docs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Pygments==2.11.2
1717
pyparsing==3.0.7
1818
pytz==2021.3
1919
requests==2.27.1
20+
setuptools==69.0.3
2021
snowballstemmer==2.2.0
2122
Sphinx==4.4.0
2223
sphinx-rtd-theme==1.0.0

openfhe/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .openfhe import *

setup.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os
2+
import subprocess
3+
import sys
4+
from setuptools import setup, Extension
5+
from setuptools.command.sdist import sdist as _sdist
6+
from setuptools.command.build_ext import build_ext as _build_ext
7+
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
8+
import glob
9+
import shutil
10+
11+
__version__ = '0.8.4'
12+
13+
class CMakeExtension(Extension):
14+
def __init__(self, name, sourcedir=''):
15+
super().__init__(name, sources=[])
16+
self.sourcedir = os.path.abspath(sourcedir)
17+
18+
class CMakeBuild(_build_ext):
19+
20+
def run(self):
21+
for ext in self.extensions:
22+
self.build_cmake(ext)
23+
24+
def build_cmake(self, ext):
25+
if os.path.exists('openfhe/openfhe.so'):
26+
return
27+
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
28+
print(extdir)
29+
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
30+
'-DPYTHON_EXECUTABLE=' + sys.executable]
31+
32+
cfg = 'Debug' if self.debug else 'Release'
33+
build_args = ['--config', cfg]
34+
35+
build_temp = os.path.abspath(self.build_temp)
36+
os.makedirs(build_temp, exist_ok=True)
37+
38+
num_cores = os.cpu_count() or 1
39+
build_args += ['--parallel', str(num_cores)]
40+
41+
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=build_temp)
42+
subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=build_temp)
43+
44+
so_files = glob.glob(os.path.join(extdir, '*.so'))
45+
if not so_files:
46+
raise RuntimeError("Cannot find any built .so file in " + extdir)
47+
48+
src_file = so_files[0]
49+
dst_file = os.path.join('openfhe', 'openfhe.so')
50+
shutil.move(src_file, dst_file)
51+
52+
# Run build_ext before sdist
53+
class SDist(_sdist):
54+
def run(self):
55+
if os.path.exists('openfhe/openfhe.so'):
56+
os.remove('openfhe/openfhe.so')
57+
self.run_command('build_ext')
58+
super().run()
59+
60+
setup(
61+
name='openfhe',
62+
version=__version__,
63+
description='Python wrapper for OpenFHE C++ library.',
64+
author='OpenFHE Team',
65+
author_email='[email protected]',
66+
url='https://github.com/openfheorg/openfhe-python',
67+
license='BSD-2-Clause',
68+
packages=['openfhe'],
69+
package_data={'openfhe': ['*.so', '*.pyi']},
70+
ext_modules=[CMakeExtension('openfhe', sourcedir='')],
71+
cmdclass={
72+
'build_ext': CMakeBuild,
73+
'sdist': SDist
74+
},
75+
include_package_data=True,
76+
python_requires=">=3.6",
77+
install_requires=['pybind11', 'pybind11-global', 'pybind11-stubgen']
78+
)

0 commit comments

Comments
 (0)