[Distutils] Get install prefix for module at runtime

Wolodja Wentland wentland at cl.uni-heidelberg.de
Thu Sep 17 21:05:16 CEST 2009


On Thu, Sep 17, 2009 at 18:51 +0200, Tarek Ziadé wrote:
> you might be able to alter it on-the-fly by overriding  the build_py command
> instead of the install command

That worked perfectly! Thanks again for the help and pointers you gave
me. I really appreciate that.

The solution i came up with is:

--- snip ---
class build_py(_build_py):
    """build_py command
    
    This specific build_py command will modify module 'foo.build_config' so that it
    contains information on installation prefixes afterwards.
    """
    def build_module (self, module, module_file, package):
        if type(package) is StringType:
            _package = string.split(package, '.')
        elif type(package) not in (ListType, TupleType):
            raise TypeError, \
                  "'package' must be a string (dot-separated), list, or tuple"

        if ( module == 'build_info' and len(_package) == 1 and
            package[0] == 'foo'):
            iobj = self.distribution.command_obj['install']

            with open(module_file, 'w') as module_fp:
                module_fp.write('# -*- coding: UTF-8 -*-\n\n')
                module_fp.write("DATA_DIR = '%s'\n"%(
                    os.path.join(iobj.install_data, 'share')))
                module_fp.write("LIB_DIR = '%s'\n"%(iobj.install_lib))
                module_fp.write("SCRIPT_DIR = '%s'\n"%(iobj.install_scripts))

        _build_py.build_module(self, module, module_file, package)
--- snip ---

I might change the 'detect my module' logic a little because i rely on
python short circuit evaluation a bit too much.

I have some final questions:

1. Is the distutils API i am using here likely to change?

2. Is there a better way to access install_{lib,scripts,data} than going
   through the install command object available from distribution?

3. Could you include something like this in distutils? My idea on how to
   handle this would be to define an additional argument 'foo_bar' for
   core.setup() which will take a user defined dotted module name in
   which information like this will be saved.

   So if a user defines:

    core.setup( ...
                foo_bar : 'foo.build_info',
                ...
                )

    A file lib_prefix/foo/build_info.py will be injected into the
    library or an already one would be altered according to user defined
    string templates. 

    Something like this would end module.__file__ hacks once and for all
    and is IMHO a much cleaner way to advertise this information to
    libraries/application than trying to take care of this externally.

so long

    Wolodja Wentland
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/distutils-sig/attachments/20090917/a28c5c5e/attachment.pgp>


More information about the Distutils-SIG mailing list