David Wolever
David Wolever

Reputation: 154504

python + distutils: add .py file to package at build/install time?

For various not-very-good-but-unfortunately-necessary reasons I'm using a setup.py file to manage some binary assets.

During py setup.py build or install I would like to create a .py file in the "normal" Python package being installed by setup.py which contains some details about these binary assets (their absolute path, version information, etc).

What's the best way to create that file?

For example, I'd like it to work something like this:

$ cd my-python-package
$ py setup.py install
...
Installing version 1.23 of my_binary_assets to /some/path...
...
$ python -c "from  my_python_package import binary_asset_version_info as info; print info"
{"path": "/some/path", "version": "1.23"}

(note: I'm using the cmdclass argument to setup(…) to manage the building + installation of the binary assets… I'd just like to know how to create the binary_asset_version_info.py file used in the example)

Upvotes: 0

Views: 289

Answers (1)

merwok
merwok

Reputation: 6907

At first sight, there is a catch-22 in your requirements: The most obvious place to create this .py file would be in the build or build_py command (to get usual distutils operations like byte-compilation), but you want that file to contain the paths to the installed assets, so you’d have to create it during the install step. I see two ways to solve that:

a) Create your info.py file during build_py, and use distutils machinery to get the installation paths of the assets files

b) Create info.py during install and call distutils.util.byte_compile to byte-compile it

I find both ideas distasteful, but well :) Now, do you know how to fill in the file (i.e. get the install paths from distutils)?

Upvotes: 1

Related Questions