[mapserver-commits] r8939 - trunk/mapserver/mapscript/python

svn at osgeo.org svn at osgeo.org
Wed Apr 22 16:13:57 EDT 2009


Author: hobu
Date: 2009-04-22 16:13:57 -0400 (Wed, 22 Apr 2009)
New Revision: 8939

Added:
   trunk/mapserver/mapscript/python/setup.cfg
Modified:
   trunk/mapserver/mapscript/python/setup.py
Log:
substantially rework setup.py to be more like GDAL's... fixes #2637

Added: trunk/mapserver/mapscript/python/setup.cfg
===================================================================
--- trunk/mapserver/mapscript/python/setup.cfg	                        (rev 0)
+++ trunk/mapserver/mapscript/python/setup.cfg	2009-04-22 20:13:57 UTC (rev 8939)
@@ -0,0 +1,8 @@
+# You can override default build options here
+
+[build_ext]
+#include_dirs = ../../ 
+#library_dirs = ../../
+libraries = mapserver
+mapserver_config=../../mapserver-config
+

Modified: trunk/mapserver/mapscript/python/setup.py
===================================================================
--- trunk/mapserver/mapscript/python/setup.py	2009-04-22 04:31:55 UTC (rev 8938)
+++ trunk/mapserver/mapscript/python/setup.py	2009-04-22 20:13:57 UTC (rev 8939)
@@ -11,136 +11,179 @@
 # DEVELOP (build and run in place)
 #   python setup.py develop
 
-import sys
+import sys, os
 
-try:
-    first_arg = sys.argv[1].upper()
-except:
-    first_arg = None
-
-if first_arg:
-    if first_arg =='DEVELOP':
-        from setuptools import setup, Extension
-    else:
-        from distutils.core import setup, Extension
-else:
-    from distutils.core import setup, Extension
-
+from setuptools import setup, Extension
 from distutils import sysconfig
+from distutils.command.build_ext import build_ext
+from distutils.ccompiler import get_default_compiler
+from distutils.sysconfig import get_python_inc
+import popen2
 
-
-import sys
-import os.path
-import string
-
-# Function needed to make unique lists.
+# 
+# # Function needed to make unique lists.
 def unique(list):
     dict = {}
     for item in list:
         dict[item] = ''
     return dict.keys()
 
-# Should be created by the mapserver build process.
-mapscriptvars = "../../mapscriptvars"
+# ---------------------------------------------------------------------------
+# Default build options
+# (may be overriden with setup.cfg or command line switches).
+# ---------------------------------------------------------------------------
 
-# Open and read lines from mapscriptvars.
-try:
-    fp = open(mapscriptvars, "r")
-except IOError, e:
-    raise IOError, '%s. %s' % (e, "Has MapServer been made?")
+include_dirs = ['../..']
+library_dirs = ['../../']
+libraries = ['mapserver']
 
-ms_install_dir = fp.readline()
-ms_macros = fp.readline()
-ms_includes = fp.readline()
-ms_libraries_pre = fp.readline()
-ms_extra_libraries = fp.readline()
+extra_link_args = []
+extra_compile_args = []
+# might need to tweak for Python 2.4 on OSX to be these
+#extra_compile_args = ['-g', '-arch', 'i386', '-isysroot','/']
 
-# Get mapserver version from mapscriptvars, which contains a line like
-# 
-# MS_VERSION "4.x.y"
-ms_version = '5.0' # the default
-ms_version_line = fp.readline()
-if ms_version_line:
-    ms_version = ms_version_line.split()[2]
-    ms_version = ms_version.replace('"', '')
-	
-# Distutils wants a list of library directories and
-# a seperate list of libraries.  Create both lists from
-# lib_opts list.
-lib_opts = string.split(ms_libraries_pre)
-lib_dirs = [x[2:] for x in lib_opts if x[:2] == "-L"]
-lib_dirs = unique(lib_dirs)
-lib_dirs = lib_dirs + string.split(ms_install_dir)
 
-libs = []
-extras = []
-ex_next = False
+def get_config(option, config='../../mapserver-config'):
+    command = config + " --%s" % option
+    p = popen2.popen3(command)
+    r = p[0].readline().strip()
+    if not r:
+        raise Warning(p[2].readline())
+    return r
+    
 
-for x in lib_opts:
-    if ex_next:
-        extras.append(x)
+class ms_ext(build_ext):
+
+    MAPSERVER_CONFIG = '../../mapserver-config'
+    user_options = build_ext.user_options[:]
+    user_options.extend([
+        ('mapserver-config=', None,
+        "The name of the mapserver-config binary and/or a full path to it"),
+    ])
+
+    def initialize_options(self):
+        build_ext.initialize_options(self)
+        self.gdaldir = None
+        self.mapserver_config = self.MAPSERVER_CONFIG
+
+    def get_compiler(self):
+        return self.compiler or get_default_compiler()
+    
+    def get_mapserver_config(self, option):
+        return get_config(option, config =self.mapserver_config)
+    
+    def finalize_options(self):
+        if self.include_dirs is None:
+            self.include_dirs = include_dirs
+        
+        includes =  self.get_mapserver_config('includes')
+        includes = includes.split()
+        for item in includes:
+            if item[:2] == '-I' or item[:2] == '/I':
+                if item[2:] not in include_dirs:
+                    self.include_dirs.append( item[2:] )
+
+        if self.library_dirs is None:
+            self.library_dirs = library_dirs
+
+        libs =  self.get_mapserver_config('libs')
+        self.library_dirs = self.library_dirs + [x[2:] for x in libs.split() if x[:2] == "-L"]
+
         ex_next = False
-    elif x[:2] == '-l':
-        libs.append( x[2:] )
-    elif x[-4:] == '.lib' or x[-4:] == '.LIB':
-	    dir, lib = os.path.split(x)
-	    libs.append( lib[:-4] )
-	    if len(dir) > 0:
-	        lib_dirs.append( dir )
-    elif x[-2:] == '.a':
-        extras.append(x)
-    elif x[:10] == '-framework':
-        extras.append(x)
-        ex_next = True
-    elif x[:2] == '-F':
-        extras.append(x)
-          
-libs = unique(libs)
-#libs = ['mapserver']
-# if we're msvc, just link against the stub lib
-# and be done with it
-if sys.platform == 'win32':
-    libs = ['mapserver_i','gd']
+        libs = libs.split()
+        for x in libs:
+            if ex_next:
+                extra_link_args.append(x)
+                ex_next = False
+            elif x[:2] == '-l':
+                libraries.append( x[2:] )
+            elif x[-4:] == '.lib' or x[-4:] == '.LIB':
+                dir, lib = os.path.split(x)
+                libraries.append( lib[:-4] )
+                if len(dir) > 0:
+                    lib_dirs.append( dir )
+            elif x[-2:] == '.a':
+                extra_link_args.append(x)
+            elif x[:10] == '-framework':
+                extra_link_args.append(x)
+                ex_next = True
+            elif x[:2] == '-F':
+                extra_link_args.append(x)
+                
+        # don't forget to add mapserver lib
+        self.libraries = unique(libraries) + ['mapserver',]
 
-lib_dirs = unique(lib_dirs)
+        if self.libraries is None:
+            if self.get_compiler() == 'msvc':
+                libraries.remove('mapserver')
+                libraries.append('mapserver_i')
+                libraries.append('gd')
+            self.libraries = libraries
 
-# Create list of macros used to create mapserver.
-ms_macros = string.split(ms_macros)
-macros = [(x[2:], None) for x in ms_macros]
+        build_ext.finalize_options(self)
+        
+        if self.get_compiler() == 'msvc':
+            return True
+        try:
+            self.dir = os.path.abspath('..')
+            self.library_dirs.append(self.dir)
+            self.include_dirs.append(self.dir)
+        except:
+            print 'Could not run mapserver-config!!!!'
 
-# Create list of include directories to create mapserver.
 
-include_dirs = [sysconfig.get_python_inc()]
+    
+mapserver_module = Extension('_mapscript',
+                        sources=["mapscript_wrap.c", "pygdioctx/pygdioctx.c"],
+#                        define_macros = define_macros,
+                        extra_compile_args = extra_compile_args,
+                        extra_link_args = extra_link_args)
 
-ms_includes = string.split(ms_includes)
-for item in ms_includes:
-    if item[:2] == '-I' or item[:2] == '/I':
-	if item[2:] not in include_dirs:
-	    include_dirs.append( item[2:] )
 
-# Here is the distutils setup function that does all the magic.
+mapserver_version = get_config('version')
+author = "Steve Lime"
+author_email = "steve.lime at dnr.state.mn.us"
+maintainer = "Howard Butler"
+maintainer_email = "hobu.inc at gmail.com"
+description = "MapServer Python MapScript bindings"
+license = "MIT"
+url="http://www.mapserver.org"
+name = "MapScript"
+ext_modules = [mapserver_module,]
+py_modules = ['mapscript',]
 
-# Uncomment lines below if using static gd
-#extras.append("-static")
-#extras.append("-lgd")
+readme = file('README','rb').read()
 
 if not os.path.exists('mapscript_wrap.c') :
-	os.system('swig -python -shadow -modern %s -o mapscript_wrap.c ../mapscript.i' % " ".join(ms_macros))
+	os.system('swig -python -shadow -modern %s -o mapscript_wrap.c ../mapscript.i' % " ".get_config('defines'))
 
-setup(name = "mapscript",
-      version = ms_version,
-      description = "Python interface to MapServer",
-      author = "MapServer Project",
-      url = "http://mapserver.gis.umn.edu/",
-      ext_modules = [Extension("_mapscript",
-                               ["mapscript_wrap.c", "pygdioctx/pygdioctx.c"],
-                               include_dirs = include_dirs,
-                               library_dirs = lib_dirs,
-                               libraries = libs,
-                               define_macros =  macros,
-                               extra_link_args = extras,
-                              )
-                    ],
-      py_modules = ["mapscript"]
-     )
+classifiers = [
+        'Development Status :: 4 - Beta',
+        'Intended Audience :: Developers',
+        'Intended Audience :: Science/Research',
+        'License :: OSI Approved :: MIT License',
+        'Operating System :: OS Independent',
+        'Programming Language :: Python',
+        'Programming Language :: C',
+        'Programming Language :: C++',
+        'Topic :: Scientific/Engineering :: GIS',
+        'Topic :: Scientific/Engineering :: Information Analysis',
+        
+]
 
+
+setup( name = name,
+       version = mapserver_version,
+       author = author,
+       author_email = author_email,
+       maintainer = maintainer,
+       maintainer_email = maintainer_email,
+       long_description = readme,
+       description = description,
+       license = license,
+       classifiers = classifiers,
+       py_modules = py_modules,
+       url=url,
+       zip_safe = False,
+       cmdclass={'build_ext':ms_ext},
+       ext_modules = ext_modules )



More information about the mapserver-commits mailing list