Message ID | 20191102172812.22492-1-nicolas.iooss@m4x.org (mailing list archive) |
---|---|
State | Rejected |
Headers | show |
Series | [1/4] libselinux: compile Python bytecode when installing Python files | expand |
Hello Nicolas, On Sat, 2 Nov 2019 18:28:09 +0100 Nicolas Iooss <nicolas.iooss@m4x.org> wrote: > When selinux module is imported from a Python script, the content of > __init__.py is compiled into bytecode and the result is saved into a > file if it is allowed. For example, when root runs with Python 3.7 a > script that uses "import selinux" on a system where SELinux is in > permissive mode, this file may be created: > > /usr/lib/python3.7/site-packages/selinux/__pycache__/__init__.cpython-37.pyc > > Prevent this file from being dynamically created by creating it when > libselinux is installed, using "python -m compileall". > > Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org> As far as I know, this not typically done by "setup.py install", and this is generally left to distributions. In the context of Buildroot [1], we do the byte-compilation all at once at the very end of the build of all packages. Having individual packages do their own byte-compilation would be annoying. If you would like to have this byte-compilation done by the SELinux Makefile, could you make it optional (even if you decide to enable it by default) ? Thanks! Thomas
On Sat, Nov 2, 2019 at 9:18 PM Thomas Petazzoni <thomas.petazzoni@bootlin.com> wrote: > > Hello Nicolas, > > On Sat, 2 Nov 2019 18:28:09 +0100 > Nicolas Iooss <nicolas.iooss@m4x.org> wrote: > > > When selinux module is imported from a Python script, the content of > > __init__.py is compiled into bytecode and the result is saved into a > > file if it is allowed. For example, when root runs with Python 3.7 a > > script that uses "import selinux" on a system where SELinux is in > > permissive mode, this file may be created: > > > > /usr/lib/python3.7/site-packages/selinux/__pycache__/__init__.cpython-37.pyc > > > > Prevent this file from being dynamically created by creating it when > > libselinux is installed, using "python -m compileall". > > > > Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org> > > As far as I know, this not typically done by "setup.py install", and > this is generally left to distributions. Hello, this is done when a parameter such as "--optimize=1" is given to "python setup.py install". Such a parameter is described in Arch Linux packaging guidelines (https://wiki.archlinux.org/index.php/Python_package_guidelines#distutils). Nevertheless I agree it seems to be left to distributions whether to bytecode-compile Python modules. > In the context of Buildroot [1], we do the byte-compilation all at once > at the very end of the build of all packages. Having individual > packages do their own byte-compilation would be annoying. What is the reference you are using for [1]? Why would this be annoying? For example, do you use a non-standard way of generating the bytecode which is not compatible with the one that would be generated by the package itself? > If you would like to have this byte-compilation done by the SELinux > Makefile, could you make it optional (even if you decide to enable it > by default) ? My main motivation behind this patch is to remove lines such as this one from Arch Linux's PKGBUILD: /usr/bin/python3 -m compileall "${pkgdir}/$(/usr/bin/python3 -c 'import site; print(site.getsitepackages()[0])')" (Example from https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=selinux-python&id=dd87f7ae1d636a02bf404ed527a27022ee5d2ab3) As every Makefile installing Python scripts has everything that is needed to build this command line, it is much simpler for "make install" to perform the bytecode-compilation directly instead. Nevertheless, as there is an interest to keep bytecode-compilation out of the "make && make install" process, I will rework my patches to make it optional (so that Arch Linux and maybe other distributions can invoke it, but not Buildroot). Thanks, Nicolas
On Sun, Nov 3, 2019 at 9:57 PM Nicolas Iooss <nicolas.iooss@m4x.org> wrote: > > On Sat, Nov 2, 2019 at 9:18 PM Thomas Petazzoni > <thomas.petazzoni@bootlin.com> wrote: > > > > Hello Nicolas, > > > > On Sat, 2 Nov 2019 18:28:09 +0100 > > Nicolas Iooss <nicolas.iooss@m4x.org> wrote: > > > > > When selinux module is imported from a Python script, the content of > > > __init__.py is compiled into bytecode and the result is saved into a > > > file if it is allowed. For example, when root runs with Python 3.7 a > > > script that uses "import selinux" on a system where SELinux is in > > > permissive mode, this file may be created: > > > > > > /usr/lib/python3.7/site-packages/selinux/__pycache__/__init__.cpython-37.pyc > > > > > > Prevent this file from being dynamically created by creating it when > > > libselinux is installed, using "python -m compileall". > > > > > > Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org> > > > > As far as I know, this not typically done by "setup.py install", and > > this is generally left to distributions. > > Hello, this is done when a parameter such as "--optimize=1" is given > to "python setup.py install". Such a parameter is described in Arch > Linux packaging guidelines > (https://wiki.archlinux.org/index.php/Python_package_guidelines#distutils). > Nevertheless I agree it seems to be left to distributions whether to > bytecode-compile Python modules. I have spent some time working on these patches, and it appears that the base assumption of "this not typically done by setup.py install" is not true on my system (Arch Linux with Python 3.7): setup.py *DOES* compile bytecode into .pyc files. What the distribution I'm using does when packaging is giving a parameter such as --optimize=1 in order to create .opt-1.pyc files too. Currently, python/sepolicy/Makefile already uses setup.py (cf. https://github.com/SELinuxProject/selinux/blob/selinux-python-3.0-rc1/python/sepolicy/Makefile#L30), so .pyc files are already generated in some Makefiles of the project. Could you please confirm that "setup.py install" does not compile Python scripts into bytecode on your system? This test can be achieved for example by cloning https://github.com/SELinuxProject/selinux, running "make -C python/sepolicy DESTDIR=/tmp/selinux install" and searching for .pyc files in /tmp/selinux. If "setup.py install" produces .pyc files on every system, from my point of view there are several alternatives that can be considered: 1. Keep the current behavior by default and introduce a COMPILE_PY boolean variable in Makefiles that produces both .pyc and .opt-1.pyc files in Makefiles that do not use setup.py. 2. Generate .pyc files everywhere (what my patches do), and introduce a OPTIMIZE_PY boolean variable in Makefiles that is used to trigger the compilation to .opt-1.pyc files. 3. Mix 2 and 3 by introducing both COMPILE_PY and OPTIMIZE_PY. 4. Find a way for "setup.py install" not to produce such files in python/sepolicy/Makefile and do as 1 or 3. (A 5th option would be to compile both .pyc and .opt-1.pyc files without introducing new variables, but this would go against what appears to be a legitimate user request) My personal preference here would be either 1. or 2. Would one of these options suits Buildroot maintainers better? Thanks, Nicolas
diff --git a/libselinux/src/Makefile b/libselinux/src/Makefile index 3b8bad810de0..349f957355c1 100644 --- a/libselinux/src/Makefile +++ b/libselinux/src/Makefile @@ -176,6 +176,7 @@ install-pywrap: pywrap $(PYTHON) setup.py install --prefix=$(PREFIX) `test -n "$(DESTDIR)" && echo --root $(DESTDIR)` install -m 644 $(SWIGPYOUT) $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py ln -sf --relative $(DESTDIR)$(PYTHONLIBDIR)/selinux/_selinux$(PYCEXT) $(DESTDIR)$(PYTHONLIBDIR)/_selinux$(PYCEXT) + $(PYTHON) -m compileall $(DESTDIR)$(PYTHONLIBDIR)/selinux install-rubywrap: rubywrap test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL)
When selinux module is imported from a Python script, the content of __init__.py is compiled into bytecode and the result is saved into a file if it is allowed. For example, when root runs with Python 3.7 a script that uses "import selinux" on a system where SELinux is in permissive mode, this file may be created: /usr/lib/python3.7/site-packages/selinux/__pycache__/__init__.cpython-37.pyc Prevent this file from being dynamically created by creating it when libselinux is installed, using "python -m compileall". Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org> --- libselinux/src/Makefile | 1 + 1 file changed, 1 insertion(+)