在setup.py文件中设置文件权限

Arj*_*ain 7 python setup.py

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

Oz1*_*123 10

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

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

from setuptools.command.install import install
from distutils import log # needed for outputting information messages 
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

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

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

我希望这个答案有所帮助.


lam*_*988 -7

以 root 身份登录,然后在 shell 中输入:

chmod 744 你的文件名

  • 如果是出于部署目的,请求用户以 root 身份登录来进行补丁部署是最糟糕的主意 (2认同)