Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referring to existing distutils options inside setup.cfg and setup.py

I am using Python 2.7 with Distutils to distribute and install my self-created package. My setup.cfg looks like this:

[install]
prefix=/usr/local/MODULENAME
record=installation.txt

I have two questions:

  1. Is it possible to refer to variables set in setup.cfg (but also using command line options) when defining other setup.cfg options? For example, for:

    install-scripts=PREFIX/my-scripts
    

    I want PREFIX to be the prefix defined either inside setup.cfg or using --prefix command line argument, similar to the interpolation of variables when using ConfigParser.

  2. Is it possible to refer to the variables set in the setup.cfg from within my setup.py, without manually parsing the config file using ConfigParser?

like image 565
Naturjoghurt Avatar asked May 12 '15 12:05

Naturjoghurt


People also ask

Do you need setup py If you have setup CFG?

you must have a valid setup.py file apart from setup. cfg and pyproject. toml .

What is setup CFG used for in Python?

setup. cfg is a file which might be used to specify such options in addition to reading the command line when calling python setup.py <somecommand> . The documentation for setup.

Where is Distutils CFG?

distutils. cfg | python mingw win32 build | C:\Python27\Lib\distutils · GitHub.

Is setup CFG deprecated?

Nope, setup. cfg it is not deprecated and the documentation you mention is misleading. There were serious reasons like security related to the fact that setup.py needed execution and that's the main reason of moving away from it.


1 Answers

Referring to Variables Inside setup.cfg

You can refer to other variables/options inside your setup.cfg file. The syntax is $variable, for instance:

[install]
prefix = /my/prefix
install-scripts = $base/scripts

Please note that I used $base since this variable is affected by your prefix settings provided both in the setup.cfg and using the setup.py install --prefix command line option. In this case, your scripts will be installed to /my/prefix/scripts unless the user specifies another prefix using command line.

It is possible to use $prefix inside setup.cfg as well, but this does not seem to be affected by the custom configuration.

Accessing Values of Variables Within setup.py (after setup())

It is also possible to read the values of all variables/options inside your setup.py. The function setup returns an instance of the class Distribution. That class holds all the values of variables grouped by the command (e.g. install) and you can obtain them using the get_option_dict method. For instance:

#!/usr/bin/env python

from distutils.core import setup

d = setup(
          # Here come your setup arguments
         )
print(d.get_option_dict('install'))

will print:

{'prefix': ('setup.cfg', '/my/prefix'), 
 'install_scripts': ('setup.cfg', '$base/scripts')}

Accessing Values of Variables Within setup.py (before setup())

It is also possible to obtain an instance of the class Distribution before setup() is even run. To do so, we can replicate what setup() is doing internally and build an instance of that class ourselves. Then, a decision about the value of setup arguments can be based on the value of some installation options. Let's look at an example:

from distutils.core import setup
from distutils.dist import Distribution

# Get our own instance of Distribution
dist = Distribution()
dist.parse_config_files()
dist.parse_command_line()

# Get prefix from either config file or command line
prefix = dist.get_option_dict('install')['prefix'][1]
print("Prefix is: " + prefix)

# Use the prefix to decide on the data path for data.txt
setup(data_files=[('/first/path/to/data' if prefix == '/some/prefix'
                                         else '/second/path/to/data',
                   ['data.txt'])])
like image 122
Andrzej Pronobis Avatar answered Oct 23 '22 15:10

Andrzej Pronobis