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

add a mustachize function to preprocess the Extensions #26

Open
wants to merge 1 commit into
base: master
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
3 changes: 3 additions & 0 deletions fst/_fst.pyx.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# distutils: language = c++
# distutils: extra_compile_args = -O2 -std=c++11

cimport libfst
cimport sym
import subprocess
Expand Down
4 changes: 4 additions & 0 deletions fst/libfst.pxd.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# distutils: language = c++
# distutils: libraries = fst
# distutils: extra_compile_args = -O2 -std=c++11

from libcpp.vector cimport vector
from libcpp.string cimport string
from libcpp.pair cimport pair
Expand Down
50 changes: 44 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import glob
import logging
import subprocess
import sys
import os
import distutils
from distutils.cmd import Command
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

INC, LIB = [], []

Expand All @@ -10,15 +16,47 @@
INC.append('/opt/local/include')
LIB.append('/opt/local/lib')

# Homebrew
if sys.platform == 'darwin' and os.path.isdir('/usr/local/lib'):
INC.append('/usr/local/include')
LIB.append('/usr/local/lib')


ext_modules = [
Extension(name='fst._fst',
sources=['fst/_fst.cpp'],
libraries=['fst'],
extra_compile_args=['-O2'],
include_dirs=INC,
library_dirs=LIB)
sources=['fst/_fst.pyx.tpl', 'fst/libfst.pxd.tpl', 'fst/types.yml'],
include_dirs=INC,
library_dirs=LIB)
]


def mustachize(modules, mustache_command='mustache'):
"""Run command."""
for module in modules:
sources = module.sources

yamls = [source for source in sources if os.path.splitext(source)[-1] in {'.yml'}]
templates = [source for source in sources if os.path.splitext(source)[-1] in {'.tpl'}]

new_sources = []
for template in templates:
new_source, new_ext = os.path.splitext(template)
cmd = 'cat {yamls} | {mustache_cmd} - {template} > {new_source}'.format(yamls=' '.join(yamls),
template=template,
new_source=new_source,
mustache_cmd=mustache_command)
logging.info('Running command: %s' % str(cmd))
subprocess.check_call(cmd, shell=True)

_, new_ext = os.path.splitext(new_source)
if new_ext in {'.pyx'}:
new_sources.append(new_source)

module.sources[:] = new_sources

return modules


long_description = """
pyfst
=====
Expand Down Expand Up @@ -59,5 +97,5 @@
'Intended Audience :: Education',
'Intended Audience :: Science/Research'],
packages=['fst'],
ext_modules=ext_modules
ext_modules=cythonize(mustachize(ext_modules)),
)