在 setup.py 文件中设置文件权限 [英] set file permissions in setup.py file

查看:38
本文介绍了在 setup.py 文件中设置文件权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 setup.py 创建了一个 python 软件安装.在这个软件中,当我使用 setup.py 安装这些 xml 文件时,我使用数据文件(XML 文件),然后这些文件与 /usr/lib/python2.7/site_packages/XYZ 中的其他文件一起保存.但是文件权限设置为这些文件(XML文件)rwx------意味着只有超级用户(root)可以读取这些文件我想将XML文件的文件权限更改为rwxr----- 表示当前用户也可以读取该文件.如何更改数据文件权限.

I created a python software install with setup.py . In this software I use data files (XML file) when I install these xml file using setup.py then these files save with other files in /usr/lib/python2.7/site_packages/XYZ . But file permission set to these files (XML Files) rwx------ means only super user(root) can read these file I want to change the file permission of XML files as rwxr----- means current user can also read that file. How do I change the data files permission.

推荐答案

正确的做法是覆盖 install 命令,这里是如何做到的.

The correct way to do it would be to override the install command, here is how to do it.

首先在 setup.py 的开头添加以下导入:

First in the beginning of your setup.py add the following imports:

from setuptools.command.install import install
from distutils import log # needed for outputting information messages 

然后你需要创建一个可调用的命令类,这里是一个例子,我创建一个命令类来安装一个脚本并确保它只对 root 可执行(还有其他方法在 python 中.例如,如果您的 UID 不为 0,您可以随时退出脚本.)我也在此处使用另一个导入:

Then you need to create a callable command class, here is an example where I create a command class that installs a script and makes sure that it's only executable for root (The are other ways to that in python. For example you can always exit the script, if you UID is not 0.) I am also using another import here:

from setuptools.command.install_scripts import install_scripts

class OverrideInstall(install):

    def run(self):
        uid, gid = 0, 0
        mode = 0700
        install.run(self) # calling install.run(self) insures that everything that happened previously still happens, so the installation does not break! 
        # here we start with doing our overriding and private magic ..
        for filepath in self.get_outputs():
            if self.install_scripts in filepath:
                log.info("Overriding setuptools mode of scripts ...")
                log.info("Changing ownership of %s to uid:%s gid %s" %
                         (filepath, uid, gid))
                os.chown(filepath, uid, gid)
                log.info("Changing permissions of %s to %s" %
                         (filepath, oct(mode)))
                os.chmod(filepath, mode)

现在该类已创建.我通知安装程序在命令行中看到 install 后应该调用这个类:

Now the class is created. I notify the installer that upon seeing install in the command line this class should be invoked:

setup(
      # keep
      # all the previous keywords you had ...
      # add
      cmdclass={'install': OverrideInstall}
      ) 

我希望这个答案有帮助.

I hope this answer helps.

这篇关于在 setup.py 文件中设置文件权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆