[Distutils] setuptools: hardcoding the path in a script and site.py

Ian Bicking ianb at colorstudy.com
Fri May 12 22:37:43 CEST 2006

So, there's really two monkeypatches to setuptools that workenv does to 
setuptools, and I'd like to get rid of them; they cause a lot of 
trouble, in part because setuptools is making it difficult to 
monkeypatch with the recent change to easy-install.pth (at least, I 
think it's a recent change) -- but if I don't need to monkeypatch 
setuptools then this won't be a problem, and I don't really need to be 
compatible with old versions of setuptools because workingenv can (maybe 
always should) install its own version of setuptools.

The first is hardcoding paths in scripts.  This is the monkeypatch:

def get_script_header(script_text, executable=easy_install.sys_executable):
     from distutils.command.build_scripts import first_line_re
     first, rest = (script_text+'\\n').split('\\n',1)
     match = first_line_re.match(first)
     options = ''
     if match:
         script_text = rest
         options = match.group(1) or ''
         if options:
             options = ' '+options
     if options.find('-S') == -1:
         options += ' -S'
     shbang = \"#!%(executable)s%(options)s\\n\" % locals()
     shbang += ("import sys, os\\n"
                "join, dirname = os.path.join, os.path.dirname\\n"
                "lib_dir = join(dirname(dirname(__file__)), 'lib', 
'python%s.%s' % tuple(sys.version_info[:2]))\\n"
                "sys.path.insert(0, lib_dir)\\n"
                "import site\\n")
     return shbang

easy_install.get_script_header = get_script_header

Basically we're creating:

#!/usr/bin/python -S
import sys, os
join, dirname = os.p;ath.join, os.path.dirname
lib_dir = join(dirname(dirname(__file__)), 'lib', 'python%s.%s' % 
sys.path.insert(0, lib_dir)
import site
# the rest...

I'm thinking this could be done with an installation option (that I can 
put in distutils.cfg), like:

   script_path_override = lib/python2.4

I'd prefer this path be interpreted relative to the location the script 
is installed (os.path.join(os.path.dirname(__file__), override)), but 
that doesn't fit with the typical way distutils interprets things.  Two 
options?  script_path_override, script_path_relative_override?

This is also a feature Jim has requested for Zope.

The other monkeypatch I'm doing is to keep setuptools from demanding to 
own site.py.  The monkeypatch is:

   def install_site_py(self):
       # to heck with this, we gots our own site.py and we'd like
       # to keep it, thank you
   easy_install.easy_install.install_site_py = install_site_py

But what would be better is if setuptools installed code somewhere 
besides site.py.  For instance, site-setuptools.py.  Then if site.py 
didn't exist, it would be created like:

   import os
   # add site-setuptools
   execfile(os.path.join(os.path.dirname(__file__), 'site-setuptools.py')

If the file did exist, setuptools would check for the text "add 
site-setuptools", and give a warning or error if it was not, or maybe 
would just prefix the site.py with this text, and leave the rest of it 
as is.  Then I'd include that code in workingenv's site.py.

Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org

More information about the Distutils-SIG mailing list