PYREX

Sandy Norton sandskyfly at hotmail.com
Sat Jul 13 16:29:19 EDT 2002


edlsoft at mindspring.com (Burt Leavenworth) wrote in message news:<3d1749d3.19107439 at news1.news.adelphia.net>...
> Has anyone installed Pyrex on Windows? If so, can you share the
> details?

Here's a dirty little script that allows me to type:

  C:\> python pyx.py extension.pyx

and have pyrex produce an extension.c and an extension.pyd file
(assuming Pyrex is in sys.path of course). It's only been tested in
present form with single *.pyx files but I'm sure you can modify to do
what you want. Incidentally, I use Microsoft Visual C++ and winXP.

PS: Pyrex rocks.

ciao,

Sandy

<code>
# pyx.py
import sys, os, glob, time, shutil
import Pyrex.Compiler.Main


#GLOBAL 
#   variables
cwd = os.getcwd()
os.chdir(cwd)
timestamp = time.asctime(time.localtime())
filename = os.path.splitext(sys.argv[1])[0]
filenameExt = os.path.join(cwd, sys.argv[1])
#   functions
join = os.path.join

# run pyrex compiler
Pyrex.Compiler.Main.main(c_only = 1, use_listing_file = 0)

def isMember(file):
    x = len(filename)
    if os.path.basename(file)[:x] == filename: return 1
    else: return 0

# create setup.py for c files
setupfile = join(cwd, "setup.py")
files = glob.glob(join(cwd,"*.c"))
setup = open(setupfile, 'w')
setup.write("""# automatically generated  - %s
from distutils.core import setup, Extension
setup(name = "%s", version = "1.0",
    ext_modules = [Extension("%s", sources=%s)]
)
""" % (timestamp, filename, filename, [os.path.basename(file) for file
in files if isMember(file)]))
setup.close()

# compile corresponding python extension
os.system("python %s build" % setupfile)

# copy extension to python extension directory
path = join("build\\lib.win32-2.2", "*.pyd")
files = glob.glob(path)
for file in files:
    shutil.copyfile(file, join(cwd, os.path.basename(file)))

# cleanup
shutil.rmtree(join(cwd, "build"))
os.remove('setup.py')
</code>



More information about the Python-list mailing list