Finding application data after install - a solution?

W

Wolodja Wentland

Hi all,

reliably finding distribution data from your program seems to be an
unsolved issue for programs packaged with distutils.

I have seen a lot of code that manipulates mod.__file__ to solve this
problem, but this *will* break for some installation schemes and has the
following problems:

* it breaks if the user specified '--install-data' to have a different
value than '--install-{pure,plat}lib'
* it makes the life of linux distribution package maintainers
unneccessarily hard, because they have to patch your code so it works
with their file system hierarchy.
* it does not work inside eggs
* it is ugly ;-)

Good news everyone! I spend some time to solve this problems and would
like to share my snippet and ask for comments.

The idea is to fill a python module 'build_info.py' with installation
prefix information *at build time* and access the data within that
module.

--- snip ---
from distutils.command.build_py import build_py as _build_py
from types import StringType, ListType, TupleType

import distutils.core as core
import sys
import os.path
import string

class build_py(_build_py):
"""build_py command

This specific build_py command will modify module '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] == 'mwdb'):
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)

core.setup(name='foo',
cmdclass={'build_py': build_py},
...
)
--- snip ---

This works for installers based on distutils and those based on
setuptools and is IMHO a much cleaner and nicer solution than having to
use a complicated API that relies on externally managed information or
__file__ hacks.

Before you copy this i should note that i plan to use string templates
within 'build_info' and just substitute the wanted information and not
generate the whole file from scratch.

The module detection logic and exception handling might need some work
as well. ;-)

I have the following questions:

1. Is the distutils 'API' i use here likely to break?
2. Can you think of a better way to do this?

with kind regards

Wolodja Wentland

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iQIcBAEBCAAGBQJKtPLYAAoJEIt/fTDK8U78DREP/1K+YVffB5wqXj5Tv4TdqLW+
mGNGFPR8Wxp1RovLQC3TEo9pKLpzLhTkQpBjOvCdVHfxhUyhIEC/MxSQp7oCpLkj
viNVy48da1nb/S6BjOxM7wJTThBZa0EprdMXSO4cBX++Cs9k4390vu6vFcgiGs1i
oWIvoGbSBuq0URtWmcr3SAm26NFczJwxtDilj12iliqHQ6br7ExWCpfJ+4LziOuP
Gp2E3ogzS/ZdZaC2mra5U4VgDjOsiCTGfvoFyXp6jaR9GZeQuumtjty73uXpcmBk
578Jxwa68BG3m18wu5X3qRgCGkKQUf5l85yl7RVf5cQtjVVHsWcwutGqZS1BadCo
VOjG1WSfX4Bh16e0DV3VmOe2cA/tY3ujuPFdvX/mKZg0I3sapSyyM067x8dXXlB0
CaHWMJDRgNkmAvqu7KeM2GkJ+DrWg3Y1SOxw/xPYtoUKeiNHhgN7QRo/7NPk6egi
Dqt/Dei6ln6SusDLyun0s5hfuZa+2zbN8LGG93gXuXq78qsjFAwPqwK4vikjMi8d
Vc/Yay6dC+w8nuQip7e+YwQgdLOo4eYzQTwsGByDMcg20tt2LOBTcbl3rOdLNujI
eHASYvO7/adMBeSfQYNGhg3x1m56QVOrq3Jn3J/U65oBU4HkySScH1ZvA6bOcymt
qNErnKRv0eg5F9pNoFGa
=7t3f
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top