[Distutils] Extending distutils with 3rd party build commands?

Bob Ippolito bob at redivi.com
Thu Oct 21 00:49:52 CEST 2004


On Oct 20, 2004, at 18:38, Bob Ippolito wrote:

> On Oct 20, 2004, at 18:10, M.-A. Lemburg wrote:
>
>> Bob Ippolito wrote:
>>> I've developed two bdist commands (bdist_pkg and bdist_mpkg) for 
>>> usage on OS X.  These commands apply to any setup.py that supports 
>>> an "install" command, similar to bdist_wininst, so it would be nice 
>>> if every setup.py didn't have to explicitly import something in 
>>> order to get the commands there.
>>> I know I can make it work with a pth file that imports bdist_pkg, 
>>> but that seems a bit costly to do because it will end up importing a 
>>> bunch of stuff (distutils, mainly).
>>> Is there another way to do it?
>>
>> Submit them for inclusion in the standard distribution ;-)
>
> I will, for 2.5... but there's quite some time between now and then.

Well I did find another way, thanks to PEP 302.

How "evil" would it be if I had this module imported by a pth file?  It 
does do what I want it to do, but I'm not sure if it's safe to inflict 
on users or not..

--

import sys
import imp
try:
     set
except NameError:
     from sets import Set as set

class DistutilsHook(object):
     def __init__(self, package='distutils', hooks=('py2app', 
'bdist_mpkg')):
         self.package = package
         self.hooks = set(hooks)
         self.hooks.add(package)

     def find_module(self, fullname, path=None):
         if fullname in self.hooks:
             self.hooks.remove(fullname)
         if fullname != self.package:
             return None
         try:
             sys.meta_path.remove(self)
         except ValueError:
             pass
         try:
             fileobj, filename, stuff = imp.find_module(fullname, None)
         except ImportError:
             return None
         hooks = list(self.hooks)
         self.hooks.clear()
         return ImpLoader(fileobj, filename, stuff, hooks)

class ImpLoader(object):
     def __init__(self, fileobj, filename, stuff, hooks):
         self.fileobj = fileobj
         self.filename = filename
         self.stuff = stuff
         self.hooks = hooks

     def load_module(self, fullname):
         mod = imp.load_module(fullname, self.fileobj, self.filename, 
self.stuff)
         if self.fileobj:
             self.fileobj.close()
         for hook in self.hooks:
             try:
                 __import__(hook)
             except:
                 pass
         return mod

sys.meta_path.insert(0, DistutilsHook())



More information about the Distutils-SIG mailing list