Skip to content

Commit

Permalink
fix: release sdist to PyPI (BYVoid#797)
Browse files Browse the repository at this point in the history
* fix: release sdist to PyPI

* fix: add newline at file end

* fix: ignore more files

Signed-off-by: Frost Ming <me@frostming.com>

* fix: change the install root of cmake

Signed-off-by: Frost Ming <me@frostming.com>

* fix: make it work for editable build as well

Signed-off-by: Frost Ming <me@frostming.com>

* fix release script

Signed-off-by: Frost Ming <me@frostming.com>

* fix: include files in sdist

Signed-off-by: Frost Ming <me@frostming.com>

---------

Signed-off-by: Frost Ming <me@frostming.com>
Co-authored-by: Carbo Kuo <BYVoid@users.noreply.github.com>
  • Loading branch information
2 people authored and jiahansu committed Aug 21, 2024
1 parent 5b994e2 commit 1bdab18
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 51 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
python-version: [3.8, 3.9, '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand All @@ -32,9 +32,9 @@ jobs:
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --exclude deps --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Build and install
run: python setup.py build_ext install
run: python -m pip install .
- name: Test with pytest
run: cd python && pytest
run: pytest python/

test-pypi:
strategy:
Expand All @@ -43,7 +43,7 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4

- name: Build package and upload from docker (Linux)
if: runner.os == 'Linux'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
/xcode
/node_modules
/*.egg-info
/.venv/
9 changes: 9 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
graft src
graft deps
graft test
graft data
graft doc
include CMakeLists.txt OpenCCConfig.cmake.in opencc.pc.in README* LICENSE*
global-exclude *~ *.py[cod] *.so
include python/**/__init__.py
graft python/tests
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ xcode-build:
xcodebuild build)

python-build:
python setup.py build_ext
echo "No need to build"

python-install: python-build
python setup.py install
python -m pip install .

python-dist: python-build
python setup.py bdist_wheel
python -m build

python-test: python-build
cd python; pytest .
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools>=61", "wheel", "cmake"]
build-backend = "setuptools.build_meta"
1 change: 1 addition & 0 deletions python/opencc/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
version.py
clib/
!clib/__init__.py
1 change: 0 additions & 1 deletion python/opencc/clib/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

8 changes: 4 additions & 4 deletions release-pypi-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ for VERSION in 3.8 3.9 3.10 3.11 3.12; do
conda activate py$VERSION

# Build and package
pip install --no-cache-dir setuptools wheel cmake
python setup.py build_ext bdist_wheel \
--plat-name "manylinux2014_$(uname --machine)"
pip install --no-cache-dir build
python -m build \
-C--plat-name="manylinux2014_$(uname --machine)"

# Cleanup
conda deactivate
rm -rf build python/opencc/clib OpenCC.egg-info
rm -rf build OpenCC.egg-info
done

if [ "$1" != "testonly" ]; then
Expand Down
6 changes: 3 additions & 3 deletions release-pypi-macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ for VERSION in 3.8 3.9 3.10 3.11 3.12; do
conda activate py$VERSION

# Build and package
pip install --no-cache-dir setuptools wheel
python setup.py build_ext bdist_wheel
pip install --no-cache-dir build
python -m build --wheel

# Cleanup
conda deactivate
rm -rf build python/opencc/clib OpenCC.egg-info
rm -rf build OpenCC.egg-info
done

if [ "$1" != "testonly" ]; then
Expand Down
6 changes: 3 additions & 3 deletions release-pypi-windows.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ for %%v in (%VERSIONS%) do (
if !ERRORLEVEL! NEQ 0 (EXIT !ERRORLEVEL!)
CALL C:\Miniconda/condabin/conda.bat activate py%%v
if !ERRORLEVEL! NEQ 0 (EXIT !ERRORLEVEL!)
pip install --no-cache-dir setuptools wheel pytest
pip install --no-cache-dir build
if !ERRORLEVEL! NEQ 0 (EXIT !ERRORLEVEL!)

REM Build and package
python setup.py build_ext bdist_wheel
python -m build --wheel
if !ERRORLEVEL! NEQ 0 (EXIT !ERRORLEVEL!)

REM Cleanup
CALL C:\Miniconda/condabin/conda.bat deactivate
rmdir /S /Q build python\opencc\clib OpenCC.egg-info
rmdir /S /Q build OpenCC.egg-info
)

if NOT "%~1"=="testonly" (
Expand Down
43 changes: 11 additions & 32 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,12 @@
import wheel.bdist_wheel

_this_dir = os.path.dirname(os.path.abspath(__file__))
_clib_dir = os.path.join(_this_dir, 'python', 'opencc', 'clib')
_build_dir = os.path.join(_this_dir, 'build', 'python')

_cmake_file = os.path.join(_this_dir, 'CMakeLists.txt')
_author_file = os.path.join(_this_dir, 'AUTHORS')
_readme_file = os.path.join(_this_dir, 'README.md')

try:
sys.path.insert(0, os.path.join(_this_dir, 'python'))

import opencc # noqa
_libopencc_built = True
except ImportError:
_libopencc_built = False


def get_version_info():
version_info = ['1', '0', '0']
Expand Down Expand Up @@ -70,20 +61,13 @@ def get_long_description():
return f.read().decode('utf-8')


def build_libopencc():
if _libopencc_built:
return # Skip building binary file
def build_libopencc(output_path):
print('building libopencc into %s' % _build_dir)

is_windows = sys.platform == 'win32'

# Make build directories
if is_windows:
subprocess.call('md {}'.format(_build_dir), shell=True)
subprocess.call('md {}'.format(_clib_dir), shell=True)
else:
subprocess.call('mkdir -p {}'.format(_build_dir), shell=True)
subprocess.call('mkdir -p {}'.format(_clib_dir), shell=True)
os.makedirs(_build_dir, exist_ok=True)

# Configure
cmake_args = [
Expand All @@ -93,14 +77,14 @@ def build_libopencc():
'-DENABLE_BENCHMARK:BOOL=OFF',
'-DBUILD_PYTHON:BOOL=ON',
'-DCMAKE_BUILD_TYPE=Release',
'-DCMAKE_INSTALL_PREFIX={}'.format(_clib_dir),
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}'.format(_clib_dir),
'-DCMAKE_INSTALL_PREFIX={}'.format(output_path),
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}'.format(output_path),
'-DPYTHON_EXECUTABLE={}'.format(sys.executable),
]

if is_windows:
cmake_args += \
['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE={}'.format(_clib_dir)]
['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE={}'.format(output_path)]
if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']

Expand All @@ -117,11 +101,6 @@ def build_libopencc():
errno = subprocess.call(cmd)
assert errno == 0, 'Build failed'

# Empty __init__.py file has to be created
# to make opencc.clib a module
with open('{}/__init__.py'.format(_clib_dir), 'w'):
pass


class OpenCCExtension(setuptools.Extension, object):
def __init__(self, name, sourcedir=''):
Expand All @@ -131,8 +110,12 @@ def __init__(self, name, sourcedir=''):

class BuildExtCommand(setuptools.command.build_ext.build_ext, object):
def build_extension(self, ext):
if self.inplace:
output_path = os.path.join(_this_dir, 'python', 'opencc', 'clib')
else:
output_path = os.path.abspath(os.path.join(self.build_lib, 'opencc', 'clib'))
if isinstance(ext, OpenCCExtension):
build_libopencc()
build_libopencc(output_path)
else:
super(BuildExtCommand, self).build_extension(ext)

Expand All @@ -157,7 +140,7 @@ def _determine_platform_tag():
return 'macosx-11.0-{}'.format(machine)
else:
raise NotImplementedError

if os.name == 'posix':
_, _, _, _, machine = os.uname()
return 'manylinux2014-{}'.format(machine)
Expand Down Expand Up @@ -190,10 +173,6 @@ def initialize_options(self):

packages=packages,
package_dir={'opencc': 'python/opencc'},
package_data={str('opencc'): [
'clib/opencc_clib*',
'clib/share/opencc/*',
]},
ext_modules=[OpenCCExtension('opencc.clib.opencc_clib', 'python')],
cmdclass={
'build_ext': BuildExtCommand,
Expand Down

0 comments on commit 1bdab18

Please sign in to comment.