summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorStefan Behnel <scoder@users.berlios.de>2011-04-10 22:49:41 +0200
committerStefan Behnel <scoder@users.berlios.de>2011-04-10 22:49:41 +0200
commit3c612380021c3c6cb02f8c13114a379dac666d46 (patch)
treeefe2a1998bebe3c10fbd5ec982e44881f1f917f0 /bin
parent962d37ca8089fcebc18f311ba77b245d51c39250 (diff)
downloadcython-3c612380021c3c6cb02f8c13114a379dac666d46.tar.gz
new script to compile+run Python files directly in Cython
Diffstat (limited to 'bin')
-rwxr-xr-xbin/cythonrun66
1 files changed, 66 insertions, 0 deletions
diff --git a/bin/cythonrun b/bin/cythonrun
new file mode 100755
index 000000000..0cd1c2898
--- /dev/null
+++ b/bin/cythonrun
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+
+"""
+Compile a Python script into an executable that embeds CPython and run it.
+Requires CPython to be built as a shared library ('libpythonX.Y').
+
+Basic usage:
+
+ python cythonrun somefile.py [ARGS]
+"""
+
+DEBUG = True
+
+import sys
+import os
+import subprocess
+from distutils import sysconfig
+
+INCDIR = sysconfig.get_python_inc()
+LIBDIR1 = sysconfig.get_config_var('LIBDIR')
+LIBDIR2 = sysconfig.get_config_var('LIBPL')
+PYLIB = sysconfig.get_config_var('LIBRARY')[3:-2]
+
+CC = sysconfig.get_config_var('CC')
+CFLAGS = sysconfig.get_config_var('CFLAGS') + ' ' + os.environ.get('CFLAGS', '')
+LINKCC = sysconfig.get_config_var('LINKCC')
+LINKFORSHARED = sysconfig.get_config_var('LINKFORSHARED')
+LIBS = sysconfig.get_config_var('LIBS')
+SYSLIBS = sysconfig.get_config_var('SYSLIBS')
+
+def runcmd(cmd, shell=True):
+ cmd = ' '.join(cmd)
+ if DEBUG:
+ print(cmd)
+ returncode = subprocess.call(cmd, shell=True)
+ if returncode:
+ sys.exit(returncode)
+
+def clink(basename):
+ runcmd([LINKCC, '-o', basename, basename+'.o', '-L'+LIBDIR1, '-L'+LIBDIR2, '-l'+PYLIB]
+ + LIBS.split() + SYSLIBS.split() + LINKFORSHARED.split())
+
+def ccompile(basename):
+ runcmd([CC, '-c', '-o', basename+'.o', basename+'.c', '-I' + INCDIR] + CFLAGS.split())
+
+def cycompile(input_file):
+ from Cython.Compiler import Version, CmdLine, Main
+ options, sources = CmdLine.parse_command_line(['--embed', input_file])
+ if DEBUG:
+ print('Using Cython %s to compile %s' % (Version.version, input_file))
+ result = Main.compile(sources, options)
+ if result.num_errors > 0:
+ sys.exit(1)
+
+def exec_file(basename, *args):
+ runcmd([os.path.abspath(basename)] + list(args), shell=False)
+
+def main(input_file, *args):
+ basename = os.path.splitext(input_file)[0]
+ cycompile(input_file)
+ ccompile(basename)
+ clink(basename)
+ exec_file(basename)
+
+if __name__ == '__main__':
+ main(*sys.argv[1:])