2

I'm trying to find a nice way to ship default configuration files with my python setuptools project.

For now I'm doing it like this:

from setuptools import setup

setup(
    ...
    data_files = [('/usr/local/etc', ['files/myproject.conf', ...])]
    ...
)

The problem is that the configuration files are erased if I uninstall my package. Usually, when uninstalling a package on Linux or FreeBSD, the configuration files are not deleted. I think this is good, because sometimes you just want to uninstall a package to reinstall another version, or to install it with other options etc... You don't expect your configurations files to be deleted.

How to achieve the same with setuptools? How to install configurations files only if they don't already exist?

John Smith Optional
  • 22,259
  • 12
  • 43
  • 61

1 Answers1

0

Why can't you? setup.py is also a python script, maybe you can manually add your configuration files to avoid adding them into metadata? Like the following code:

from setuptools import setup
from shutil import copyfile
import os
...
setup(
...
# no data_files option
...
)
if not os.path.exists(configuration_file):
    copyfile(configuration, configuration_file)
Sraw
  • 18,892
  • 11
  • 54
  • 87
  • I don't know exactly how the uninstall process decides what files must be erased. Does the `pip uninstall` command just use the same setup.py script as the `pip install` command? If so, I guess your solution should work. – John Smith Optional Jun 03 '17 at 16:43
  • @JohnSmithOptional First, `pip uninstall` works without `setup.py` file. Second, I think I have misunderstood your question. Now I think you could add your configuration file by raw python code instead of using `data_files` option. So it won't be added into packages' metadata. And you can have the full access to control your configuration files. I will edit my answer. – Sraw Jun 03 '17 at 17:20
  • This code executes when I create the package using the command `python setup.py sdist --formats=gztar`. This executes on my laptop where I'm a simple user and it tries to create a /usr/local/etc/myproject.conf file on my laptop, (which, as you know, is not what I'm trying to do). It also prevents the creation of the archive, since the script fails with a "PermissionError: [Errno 13] Permission denied". – John Smith Optional Jun 03 '17 at 18:03
  • @JohnSmithOptional Actually, if you check how `jupyter` store it's configuration file, you will find it is stored at `~/` but not some path that need a superuser permission. So could you just save it at home path? And, I think the critical point is to maintain configuration files by yourself but not setuptools. In this case, you could include your configuration file in your source code dictionary, and copy it to a normal path such as `~/` if it doesn't exist when you want to load configuration. – Sraw Jun 03 '17 at 18:27
  • Yes, I'm actually going to distribute "sample" conf files and copy them to the conf file locations if the conf files are missing. Just wanted to know if setuptools had a simple feature to achieve this at installation time. Apparently not. – John Smith Optional Jun 03 '17 at 18:40