2011-05-09 98 views
6

我使用setup.py創建了一個python軟件安裝。在我使用setup.py安裝這些xml文件時,我在這個軟件中使用數據文件(XML文件),然後這些文件與/usr/lib/python2.7/site_packages/XYZ中的其他文件一起保存。但文件權限設置爲這些文件(XML文件)rwx------意味着只有超級用戶(root)才能讀取這些文件我想更改XML文件的文件權限,因爲rwxr-----意味着當前用戶也可以讀取該文件。如何更改數據文件權限。在setup.py文件中設置文件權限

+0

什麼是你的umask設置爲? – Keith 2011-05-09 06:03:03

回答

-5

以root身份登錄,並在外殼類型:

搭配chmod 744 yourfilename

+2

如果是用於部署的目的,請求用戶以root身份登錄到補丁部署是最糟糕的想法 – Bruce 2011-05-09 05:41:09

+0

我不希望用戶在用戶使用setup.py文件安裝此軟件時執行此操作,那麼軟件將自動執行 – 2011-05-09 05:41:45

+0

難道你不能在發佈之前設置權限? – lamwaiman1988 2011-05-09 05:43:13

6

正確的方式做這將是覆蓋install命令,這裏是如何做到這一點。

首先在你的setup.py添加以下進口的開頭:

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

然後,你需要創建一個可調用的命令類,這裏是我創建安裝腳本,並確保一個命令類的例子它是唯一可執行root(在其他的方式來在蟒蛇例如你可以隨時退出腳本,如果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) 

現在創建類。我通知時在命令行中看到install這個類應調用安裝程序:

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

我希望這個答案可以幫助。

1

我使用setup.py來構建各種RPM。這個解決方案對我來說有點不同。我也認爲這是出於兩個原因更強大的:

  1. 我可以覆蓋一個文件的權限明確
  2. 我不需要知道用戶的UID和GID。相反,我可以使用純文本。

這裏是一個工作示例

from distutils.core import setup 
import distutils.command.bdist_rpm 
import distutils.command.install 

version='13' 

data_files = [ 
    ('/usr/share/blah', ['README', 'test.sh']), 
] 

permissions = [ 
    ('/usr/share/blah', 'test.sh', '(755, sri, sri)'), 
] 

class bdist_rpm(distutils.command.bdist_rpm.bdist_rpm): 

    def _make_spec_file(self): 
     spec = distutils.command.bdist_rpm.bdist_rpm._make_spec_file(self) 
     for path, files , perm in permissions: 

      ## 
      # Add a line to the SPEC file to change the permissions of a 
      # specific file upon install. 
      # 
      # example: 
      # %attr(666, root, root) path/file 
      # 
      spec.extend(['%attr{} {}/{}'.format(perm, path, files)]) 

     return spec 


setup(name='sri-testme', 
     version=version, 
     description='This is garganbe and is only used to test the permision flag behavior', 
     author='Chris Gembarowski', 
     author_email='[email protected]', 
     url='https://www.python.org/sigs/distutils-sig/', 
     data_files=data_files, 
     cmdclass={'bdist_rpm':bdist_rpm} 
    ) 

讓我解釋一下這是怎麼回事的更多細節。 RPM是從SPEC文件構建的。 bdist_rpm建立一個SPEC文件。在SPEC文件中,可以通過提供%attr選項來選擇文件的權限和所有權。

在使test.sh可執行文件和用戶'sri'擁有的示例中,我會將%attr(755, sri, sri)添加到SPEC文件的末尾。

因此,當我重寫bdist_rpm._make_spec_file的行爲時,我所做的只是爲每個要覆蓋權限的文件添加一行。

從這個例子完整的SPEC文件將是:

%define name sri-testme 
%define version 13 
%define unmangled_version 13 
%define release 1 

Summary: This is garganbe and is only used to test the permision flag behavior 
Name: %{name} 
Version: %{version} 
Release: %{release} 
Source0: %{name}-%{unmangled_version}.tar.gz 
License: UNKNOWN 
Group: Development/Libraries 
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot 
Prefix: %{_prefix} 
BuildArch: noarch 
Vendor: Chris Gembarowski <[email protected]> 
Url: https://www.python.org/sigs/distutils-sig/ 

%description 
UNKNOWN 

%prep 
%setup -n %{name}-%{unmangled_version} 

%build 
python setup.py build 

%install 
python setup.py install -O1 --root=$RPM_BUILD_ROOT --record=INSTALLED_FILES 

%clean 
rm -rf $RPM_BUILD_ROOT 

%post 
## 
# sri will be turned on in the run-once script instead of here 
# 


%preun 
#!/bin/bash 



%files -f INSTALLED_FILES 
%defattr(-,root,root) 
%attr(755, sri, sri) /usr/share/blah/test.sh