简体   繁体   English

设置python模块的权限(python setup install)

[英]setting permissions of python module (python setup install)

I am configuring a distutils-based setup.py for a python module that is to be installed on a heterogeneous set of resources. 我正在为要安装在异构资源集上的python模块配置基于distutils的setup.py。 Due to the heterogeneity, the location where the module is installed is not the same on each host however disutils picks the host-specific location. 由于异构性,模块的安装位置在每个主机上都不相同,但是Disutils选择了主机特定的位置。

I find that the module is installed without o+rx permissions using disutils (in spite of setting umask ahead of running setup.py). 我发现该模块使用disutils安装时没有o + rx权限(尽管在运行setup.py之前设置了umask)。 One solution is to manually correct this problem, however I would like an automated means that works on heterogeneous install targets. 一种解决方案是手动纠正此问题,但是我想要一种适用于异构安装目标的自动方式。

For example, is there a way to extract the ending location of the installation from within setup.py? 例如,是否有一种方法可以从setup.py中提取安装的结束位置?

Any other suggestions? 还有其他建议吗?

I'm not very knowledgeable about disutils, but I am guessing that If you dig through it until You find the place where your files are written then you will see the path variable on that line. 我对disutils不太了解,但我想如果您仔细研究一下,直到找到文件的写入位置,那么您会在那一行看到path变量。

this page might help you 此页面可能对您有帮助

os.path.dirname(__file__) may be what you're looking for. os.path.dirname(__ file__)可能就是您想要的。 __file__ in a module returns the path the module was loaded from. 模块中的__file__返回从中加载模块的路径。

Assuming yourmodule is a folder containing Something.py, in setup.py: 假设您的模块是setup.py中包含Something.py的文件夹:

import os
#setup(...) call here
from yourmodule import Something
print os.path.dirname(Something.__file__)

The only wrinkle with this would be if your file structure has yourmodule in the same dir as setuputils. 唯一的麻烦是,如果您的文件结构的模块与setuputils位于同一目录中。 In that case, the python loader would preferentially load yourmodule.Something from the current dir. 在这种情况下,python loader会优先从当前目录加载模块。

Two somewhat hackish but effective options to subvert that could be to either 可以颠覆的两种有些骇人听闻但有效的选择是

  1. Remove the current directory from the python path, forcing it to load from the files that now exist in site-packages: 从python路径中删除当前目录,强制其从站点包中现在存在的文件中加载:

    import sys sys.path = sys.path[1:] 导入sys sys.path = sys.path [1:]

  2. Temporarily rename the yourmodule folder right before the import statement. 在导入语句之前临时重命名yourmodule文件夹。

With option 1, the whole thing is: 使用选项1,整个过程是:

import os
import sys

#setup(...) call here

#Remove first entry in sys.path which is current folder (probably impl dependent, but for practical purposes is consistent)
sys.path = sys.path[1:]
from yourmodule import Something
print os.path.dirname(Something.__file__)

I just tested this with one of my setup.py and it works great. 我只是使用setup.py之一对其进行了测试,效果很好。 Good luck! 祝好运!

I find that the module is installed without o+rx permissions using disutils 我发现使用disutils安装该模块时没有o + rx权限

I don't remember right now if distutils copies the files with their rights as is or if it just copies the contents. 我现在不记得distutils是按原样复制文件的权限还是仅复制内容。

(in spite of setting umask ahead of running setup.py) (尽管在运行setup.py之前设置了umask)

I'm not sure how umask and file copying from Python should interact; 我不确定umask和从Python复制文件应该如何交互; does umask apply to the system calls or does it need to be explicitly heeded by Python code? umask是否适用于系统调用,还是需要由Python代码明确注意?

For example, is there a way to extract the ending location of the installation from within setup.py? 例如,是否有一种方法可以从setup.py中提取安装的结束位置?

There is one, a bit convoluted. 有一个,有点令人费解。 What would you do with that information? 您将如何处理这些信息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM