How to chmod files within a python setup.py?

2020-07-23 05:29发布

问题:

I created a python package installation with a setup.py, and i want it to copy a data file in the folder (created for the occasion) ~/.did. The problem is that i have to call setup.py with sudo rights, as it writes in /usr/local/... So when my data file is copied in ~/.did, only the root user has write access to the file.

I decided then to add a call to os.chmod() after the setup() function, but i'd like to know if anyone had a more clean way to do so.

Here is my setup.py file :

#!/usr/bin/env python

from distutils.core import setup
import os

home=os.path.expanduser('~')

setup(name='did',
      version='1.0',
      description='Daily Image Downloader',
      author='Luc Mazon',
      author_email='my@mail.com',
      url='',
      license='GNU GPL v3',
      scripts=['did'],
      packages=['didlib'],
      data_files=[
                  ('/usr/share/man/man1', ['doc/did.1.gz']),
                  (home+'/.did', ['did.xml'])
                 ]
     )

os.chmod(home+'/.did/did.xml', 0666)

As did.xml is not a python file, i also created a MANIFEST.in file with the following line in it :

include did.xml

The global structure of my package is the following :

did-1.0
 | didlib
 |  | __init__.py
 |  | variouspyfiles.py
 | doc
 |  |-did.1.gz
 | MANIFEST.in
 | did.xml
 | did
 | setup.py

回答1:

I think it would be better, and more customary, to not write to the installer's home directory any config files at all. What about other users? Better would be to have your code check, on initialization, if that file exists for the user running it and only add a new one if it does not.



回答2:

For me, you're looking either for chown (changing owner) or chgrp (changing group).

Moreover i don't see why you're making sudo, you only need right access to those files. So just add chmod -R gou+rx setup_dir in order to be able to browse the installs.