Set file permissions in setup.py

I created a python software installation with setup.py. In this software, I use data files (XML file) when I install these xml files using setup.py, then these files are saved along with other files in /usr/lib/python2.7/site_packages/XYZ . But the file permission set for these files (XML files) rwx------ means that only the superuser (root) can read this file. I want to change the resolution of XML file files, because rwxr----- means that the current user can also read this file. How to change the resolution of data files.

+6
source share
3 answers

The correct way to do this is to override the install command, here's how to do it.

First, at 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 

Then you need to create a callable class of commands, here is an example when I create a command class that installs a script and ensures that it is only executable for root (these are other ways used in python. For example, you can always exit the script if UID is not 0.) I also use another import:

 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) 

Now the class is created. I am notifying the installer that after viewing install on the command line, this class should be called:

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

Hope this answer helps.

+6
source

I use setup.py to create all kinds of RPMs. The solution for me is a little different. I also believe that it is more reliable for two reasons:

  • I can explicitly override file permissions
  • I do not need to know the uid and gid of the user. Instead, I can use plain text.

here is a working example

 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=' chrisg@summationresearch.com ', url='https://www.python.org/sigs/distutils-sig/', data_files=data_files, cmdclass={'bdist_rpm':bdist_rpm} ) 

Let me explain what happens in more detail. RPMs are created from the SPEC file. bdist_rpm creates the SPEC file. In the SPEC file, you can select permissions and ownership of the file by providing the% attr parameter.

In the example, to make test.sh executable and owned by user 'sri', I would add %attr(755, sri, sri) to the end of the SPEC file.

Therefore, when I override the behavior of bdist_rpm._make_spec_file, all I do is add a line for each file I want to override permissions on.

The full SPEC file from this example will look like this:

 %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 < chrisg@summationresearch.com > 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 
+1
source

Log in as root and enter a shell type:

chmod 744 yourfilename

-5
source

Source: https://habr.com/ru/post/887592/


All Articles