From: <sv...@ww...> - 2004-09-17 04:41:25
|
Author: mkrose Date: 2004-09-16 21:41:16 -0700 (Thu, 16 Sep 2004) New Revision: 1232 Added: trunk/CSP/setup.py trunk/CSP/tools/CSP_bootstrap Modified: trunk/CSP/__init__.py Log: Add a mechanism for bootstrapping the CSP python module space. The basic idea is to run ./setup.py in a new workspace, which installs the file tools/CSP_bootstrap as CSP.py into your Python site-packages directory. This special module provides access to the python modules in the current workspace. Once it has been installed, you can import these modules without any changes to sys.path. If you change to a directory in another workspace, the imports will automagically find the modules in that workspace. This will simplify some of the existing modules and allow new modules to be added more easily. Eventually setup.py will also take care of preparing the build system in a platform-specific way (for example, running aclocal and autoconf under GNU/Linux). Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1232 Modified: trunk/CSP/__init__.py =================================================================== --- trunk/CSP/__init__.py 2004-09-16 19:31:03 UTC (rev 1231) +++ trunk/CSP/__init__.py 2004-09-17 04:41:16 UTC (rev 1232) @@ -1 +1,35 @@ +# Combat Simulator Project - CSPSim +# Copyright (C) 2002, 2004 The Combat Simulator Project +# http://csp.sourceforge.net +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + CSP = 1 + +import sys +import os +import os.path + +# make SimData available without qualification +sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'SimData')) + +def initDynamicLoading(): + """Enable lazy loading of shared library modules if available""" + if os.name == 'posix': + import dl + sys.setdlopenflags(dl.RTLD_GLOBAL|dl.RTLD_LAZY) + +initDynamicLoading() + Added: trunk/CSP/setup.py =================================================================== --- trunk/CSP/setup.py 2004-09-16 19:31:03 UTC (rev 1231) +++ trunk/CSP/setup.py 2004-09-17 04:41:16 UTC (rev 1232) @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# +# Combat Simulator Project Workspace Setup Script +# Copyright (C) 2004 The Combat Simulator Project +# http://csp.sourceforge.net +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +""" +CSP Workspace Setup Script + +Installs a stub module CSP.py into the Python site-packages directory, +allowing CSP and its submodules to be imported from the current client +workspace without modifying the default Python module path. + +Also prepares the build environment for the current platform. (Not +fully implemented yet.) + +Usage: %(prog)s [flags] +""" + +import sys +import os.path +from distutils import sysconfig +from distutils import file_util +from distutils import util +from distutils import log +from base import app # (CSP.base) + +log.set_verbosity(log.INFO) + + +def InstallLoader(force): + """Install tools/CSP_bootstrap as CSP.py in Python site-packages.""" + site_packages = sysconfig.get_python_lib() + target = os.path.join(site_packages, 'CSP.py') + if os.path.exists(target) and not force: + print 'Error: unable to import CSP, but CSP.py exists in site-packages.' + print 'Use --force to overwrite.' + sys.exit(1) + source = os.path.join('tools', 'CSP_bootstrap') + log.info('installing bootstrap loader') + try: + file_util.copy_file(source, target) + except file_util.DistutilsFileError, e: + print e + sys.exit(1) + log.info('byte compiling bootstrap loader') + util.byte_compile(target) + + +def main(args): + do_install = app.options.force + try: + import CSP + except ImportError: + do_install = 1 + if do_install: + InstallLoader(app.options.force) + else: + print 'Nothing to do; try %s --help for more options.' % app.programName() + + +app.addOption('-f', '--force', action='store_true', default=False, help='force reinstall') +app.start() Property changes on: trunk/CSP/setup.py ___________________________________________________________________ Name: svn:executable + * Added: trunk/CSP/tools/CSP_bootstrap =================================================================== --- trunk/CSP/tools/CSP_bootstrap 2004-09-16 19:31:03 UTC (rev 1231) +++ trunk/CSP/tools/CSP_bootstrap 2004-09-17 04:41:16 UTC (rev 1232) @@ -0,0 +1,71 @@ +# -*-python-*- +# +# Combat Simulator Project Workspace Setup Script +# Copyright (C) 2004 The Combat Simulator Project +# http://csp.sourceforge.net +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +""" +This is a bootstrap loader for the Combat Simulator Project. +It is intended to be placed into the Python path (typically +in site-packages). When loaded, it replaces itself with the +CSP package from the current workspace (if possible). +""" + +import os +import os.path +import sys + +# remove '.' from sys.path +try: + dotindex = sys.path.index('.') +except ValueError: + dotindex = -1 + +if dotindex >= 0: + del sys.path[dotindex] + +# remove this module from sys.modules +self = sys.modules['CSP'] +del sys.modules['CSP'] + +# look for the root of the current workspace +cwd = os.getcwd() +parts = cwd.split(os.sep) +if not 'CSP' in parts: + print 'Error: trying to import CSP from outside of a workspace.' + sys.exit(1) +while parts[-1] != 'CSP': + parts = parts[:-1] + +ROOT = os.path.sep.join(parts[:-1]) + +if not os.path.exists(os.path.join(ROOT, '.svn')): + print 'Warning: %s does not contain .svn and may not be a valid CSP workspace.' % ROOT + +# load the real CSP package +sys.path.insert(0, ROOT) + +try: + import CSP +except ImportError, e: + print 'Unable to bootstrap a CSP module space from the current working directory:' + print e + +# restore '.' to the path +if dotindex >= 0: + sys.path.insert(dotindex + 1, '.') + |