5

Let's assume I have a package which calls an executable file somewhere in the code (for example a third-party c/java-program). Let's further assume, the application is small/trivial enough to bundle with the package. For example a single executable file (cfoo).

I could go ahead, and put the files into the following structure:

.
|-- foo
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- core.py
|   |-- corebin
|   |   `-- cfoo
|   `-- foomain.py
`-- setup.py

And prepare a setup.py as follows:

from setuptools import setup

setup(
    name='foo',
    version='1.0',
    packages=['foo'],
    scripts=['foo/foomain.py'],
    package_data={'foo': ['corebin/*']},
    zip_safe=False
)

This will allow me to properly install the package. Later, in the package-code I could do this:

from subprocess import call

import pkg_resources as res

def main():
    fn = res.resource_filename('foo', 'corebin/cfoo')
    print "Resource located at:", fn
    call([fn])

Unfortunately, the executable file will be installed without executable flag set. Even if the original file had it set. Adding a chmod call at the end of the setup.py script is not as easy, as one would need to figure out the proper installation path first. I tried with resource_filename but that returned the local file (as in "pre-installation").

How can this problem be solved? Also with virtualenv in mind...

exhuma
  • 20,071
  • 12
  • 90
  • 123
  • First: are you generating and installing a `.tar.gz` distribution, or one of those old deprecated `.egg` distributions? Because this might at some point involve the question of which archive formats preserve which file attributes on while operating systems. :) – Brandon Rhodes Nov 07 '11 at 13:40
  • If you install it using the `scripts` keyword, it will get the correct mode (*and* get installed in an appropriate `bin/` directory). – larsks Nov 07 '11 at 15:04
  • @larsks: In my case, the executable is a file provided by a third-party. The script is used as a wrapper around this file. Your comment indeed solves my problem. But this question remains still interesting nonetheless: How would you execute something on files contained inside a package *after* install? – exhuma Nov 08 '11 at 14:45

1 Answers1

2

I'm promoting my comment to an answer:

If you install it using the scripts keyword, it will get the correct mode (and get installed in an appropriate bin/ directory).

How would you execute something on files contained inside a package after install?

This question would appear to address the same situation, and it looks like it has a reasonable answer.

Community
  • 1
  • 1
larsks
  • 277,717
  • 41
  • 399
  • 399