Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify execute permissions of scripts using Python Setuptools

I have a Python package that I've created and I'm using setuptools.setup() to install it. The package includes executable scripts, which use the scripts parameter of the setup() function.

I'm installing like this:

sudo python setup.py install

After installation, the executable scripts are located in /usr/local/bin. The only problem is that the permissions are:

-rwxr-x---  1 root root   57 Aug 23 15:13 example_script*

Instead of:

-rwxr-xr-x  1 root root   57 Aug 23 15:13 example_script*

Anybody know either how I can specify the permissions of the output executables or why the default isn't allowing anyone to execute?

FYI: My umask is 0027 and the permission of /usr/local/bin/ is drwxr-xr-x (owner=root group=root). All executable scripts have -rwxr-xr-x permissions in the development area.

like image 617
nic Avatar asked Aug 23 '14 22:08

nic


People also ask

Does a Python script needs execution permissions to run?

Unlike a CGI script, Python scripts do not need to have execute permissions to run when you invoke them with the python command. Python is an interpreted language, which means that if you call the file with python [filename], Python itself handles the execution. The file only needs to be readable by Python.

What is setuptools used for in Python?

Setuptools is a collection of enhancements to the Python distutils that allow developers to more easily build and distribute Python packages, especially ones that have dependencies on other packages. Packages built and distributed using setuptools look to the user like ordinary Python packages based on the distutils .

Does Python include setuptools?

Even in a vanilla version of Python 3.7. 6 (installed via pyenv ), the packages installed by default are both pip and setuptools .

How do I set up setuptools for Python on Windows?

Step 1: Download the latest source package of Setuptools for Python3 from here. Step 2: Extract the downloaded package using the given command. Step 3: Go to the setuptools-60.2. 0 folder and enter the following command to install the package.


1 Answers

This question is a bit old by now, but I hope my answer will be useful to anyone who happens to find it just as I did.

I've had the same problem with my package. Apparently, neither setuptools nor pip (my pip is 1.5.6) handle permissions explicitly. Your option is either change permissions manually, or migrate from scripts parameter to entry_points.

1) Change file permissions manually

This is tricky and kludgy enough, but should work. The idea is, if the installation process puts your executable to /usr/local/bin, it also has permissions to chmod the file to include executable flags. From setup.py, after your setup() clause, run something like:

execpath = '/usr/local/bin/yourexec'

if os.name is 'posix' and os.path.exists(execpath):
    os.chmod(execpath, int('755', 8))

2) Migrate to entry_points

setuptools offers an option for your setup() named entry_points. It accepts a dictionary with keys console_scripts and gui_scripts, which you can use as follows:

setup(
    # Package name, version, author, links, classifiers, etc
    entry_points = {
        'console_scripts': [
            'myexecfile = mypackage.mymainmodule:mymainfunction'
        ]
    }
)

This statement will automatically generate myexecfile executable which calls mymainfunction from mymainmodule of mypackage, which can easily be the package you're distributing.

The catch you should know is that entry point functions (mymainfunction in this example) may not have any arguments. If you passed sys.argv to your main function from your executable as an argument, you should rewrite the function to retrieve and parse sys.argv by itself.

Chris Warrick wrote a good article on this feature called Python Apps The Right Way, you might find it useful.

like image 174
bacondropped Avatar answered Sep 28 '22 06:09

bacondropped