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

WIP: Fix python #135

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ jobs:
- name: dependencies
run: |
sudo apt-get -qy update
sudo apt-get -qfy install --no-install-recommends libboost-dev libicu-dev cmake build-essential perl python3-dev python3-setuptools swig
sudo apt-get -qfy install --no-install-recommends cmake build-essential libboost-dev libicu-dev libsqlite3-dev perl pkg-config python3 python3-dev python3-pip swig
sudo python3 -m pip install --upgrade build pip setuptools
- name: cmake
run: cmake -DENABLE_PYTHON_BINDINGS=ON .
- name: build
Expand All @@ -21,3 +22,7 @@ jobs:
run: ./test/runall.pl
- name: install
run: sudo cmake --install .
- name: test python
run: |
python3 -c "import constraint_grammar; print(dir(constraint_grammar))"
python3 -c "import cg3; print(dir(cg3))"
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
/CMakeCache.txt
/CMakeFiles
Makefile
/src/cg-annotate
/src/cg-comp
/src/cg-conv
/src/cg-merge-annotations
/src/cg-mwesplit
/src/cg-proc
/src/cg-relabel
Expand All @@ -31,8 +33,11 @@ Makefile
/test/**/*.bin

__pycache__
/python/cg3.py
/python/constraint_grammar_wrap.cpp
/python/constraint_grammar.i
/python/constraint_grammar.py
/python/dist/
/python/setup.py
/python/build*
*.egg-info/
Expand Down
7 changes: 4 additions & 3 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ configure_file(constraint_grammar.i.in constraint_grammar.i @ONLY)
configure_file(setup.py.in setup.py)

add_custom_command(OUTPUT ${CPP_WRAP_FILE} ${PYTHON_FILE}
COMMAND ${PYTHON_EXECUTABLE} setup.py build
COMMAND swig -python -c++ -I/usr/include -I.. -I../src -I../include -Wall -o constraint_grammar_wrap.cpp constraint_grammar.i
COMMAND ${PYTHON_EXECUTABLE} -m build
COMMENT "Building ${PYTHON_FILE}"
)

Expand All @@ -29,8 +30,8 @@ add_custom_target(wrapper ALL
)

if(NOT PYTHON_INSTALL_PARAMS)
set(PYTHON_INSTALL_PARAMS "--prefix=${CMAKE_INSTALL_PREFIX} --root=\$ENV{DESTDIR}/")
set(PYTHON_INSTALL_PARAMS "--no-index --find-links=dist/") # TODO removing --prefix=${CMAKE_INSTALL_PREFIX} --root=\$ENV{DESTDIR}/ probably has downstream effects
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I removed the arguments for --prefix and --root to simplify the Github Action test of the python module. I'm guessing this affects downstream scripts for publishing debian or nightly builds. I would recommend basing downstream scripts on the wheel file created in cg3/python/dist/, but another option is to expand python's search path in the GH Action to find the install.

endif()

set(INSTALL_WRAPPER "${PYTHON_EXECUTABLE} setup.py install ${PYTHON_INSTALL_PARAMS}")
set(INSTALL_WRAPPER "${PYTHON_EXECUTABLE} -m pip install ${PYTHON_INSTALL_PARAMS} constraint_grammar")
install(CODE "execute_process(COMMAND ${INSTALL_WRAPPER} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")
12 changes: 6 additions & 6 deletions python/constraint_grammar.i.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ private:
CG3::Grammar grammar;

public:
CGProc(char *dictionary_path);
CGProc(char *grammar_path);
void cg_proc(int argc, char **argv, char *input_path, char *output_path);
};

CGProc::CGProc(char *dictionary_path)
CGProc::CGProc(char *grammar_path)
{
std::unique_ptr<CG3::IGrammarParser> parser;
FILE* dictionary = fopen(dictionary_path, "rb");
fread(&CG3::cbuffers[0][0], 1, 4, dictionary);
fclose(dictionary);
FILE* grammar_file = fopen(grammar_path, "rb");
fread(&CG3::cbuffers[0][0], 1, 4, grammar_file);
fclose(grammar_file);
if (CG3::cbuffers[0][0] == 'C' && CG3::cbuffers[0][1] == 'G' && CG3::cbuffers[0][2] == '3' && CG3::cbuffers[0][3] == 'B') {
parser.reset(new CG3::BinaryGrammar(grammar, std::cerr));
}
else {
parser.reset(new CG3::TextualParser(grammar, std::cerr));
}
parser->parse_grammar(dictionary_path);
parser->parse_grammar(grammar_path);
}

void CGProc::cg_proc(int argc, char **argv, char *input_path, char *output_path)
Expand Down
16 changes: 4 additions & 12 deletions python/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,19 @@
"""
Setup for SWIG Python bindings for constraint_grammar
"""
from os import path
from distutils.core import Extension, setup
from distutils.command.build import build

from os import path

class CustomBuild(build):
sub_commands = [
('build_ext', build.has_ext_modules),
('build_py', build.has_pure_modules),
('build_clib', build.has_c_libraries),
('build_scripts', build.has_scripts),
]
from setuptools import Extension
from setuptools import setup


def get_sources():
sources = ['constraint_grammar.i', '${CMAKE_SOURCE_DIR}/include/posix/popen_plus.cpp']
cc_sources = [
'ApertiumApplicator.cpp', 'BinaryGrammar.cpp', 'BinaryGrammar_read.cpp', 'BinaryGrammar_read_10043.cpp',
'BinaryGrammar_write.cpp', 'Cohort.cpp', 'CohortIterator.cpp', 'ContextualTest.cpp', 'Grammar.cpp',
'GrammarApplicator.cpp', 'GrammarApplicator_matchSet.cpp', 'GrammarApplicator_reflow.cpp',
'GrammarApplicator.cpp', 'GrammarApplicator_context.cpp', 'GrammarApplicator_matchSet.cpp', 'GrammarApplicator_reflow.cpp',
'GrammarApplicator_runContextualTest.cpp', 'GrammarApplicator_runGrammar.cpp',
'GrammarApplicator_runRules.cpp', 'GrammarWriter.cpp', 'MatxinApplicator.cpp',
'Reading.cpp', 'Rule.cpp', 'Set.cpp', 'SingleWindow.cpp', 'Strings.cpp', 'Tag.cpp',
Expand All @@ -48,7 +41,6 @@ setup(
long_description='SWIG interface to constraint_grammar for use in apertium-python',
# TODO: author, author_email, maintainer, url
license='GPL-3.0+',
cmdclass={'build': CustomBuild},
ext_modules=[constraint_grammar_module],
py_modules=['constraint_grammar', 'cg3'],
)