How to chmod files within a python setup.py?

非 Y 不嫁゛ 提交于 2019-12-21 20:25:11

问题


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.



来源:https://stackoverflow.com/questions/4552689/how-to-chmod-files-within-a-python-setup-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!