Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip source from distutils binary distributions?

I want to create a bytecode-only distribution from distutils (no really, I do; I know what I'm doing). Using setuptools and the bdist_egg command, you can simply provide the --exclude-source parameter. Unfortunately the standard commands don't have such an option.

  • Is there an easy way to strip the source files just before the tar.gz, zip, rpm or deb is created.
  • Is there a relatively clean per-command way to do this (eg just for tar.gz or zip).
like image 834
Draemon Avatar asked Aug 09 '10 12:08

Draemon


People also ask

Is Distutils deprecated?

distutils has been deprecated in NumPy 1.23. 0 . It will be removed for Python 3.12; for Python <= 3.11 it will not be removed until 2 years after the Python 3.12 release (Oct 2025).

What can I use instead of distutils?

Most Python users will not want to use this module directly, but instead use the cross-version tools maintained by the Python Packaging Authority. In particular, setuptools is an enhanced alternative to distutils that provides: support for declaring project dependencies.

What is Python Bdist?

Python Built Distribution A built distribution, also sometimes referred to as a bdist, is slightly more complex in that it first pre-interprets or “builds” the package and critically cuts out the post-install setup.py build step that is necessary when using sdist.

What is a built distribution?

Built Distribution. A Distribution format containing files and metadata that only need to be moved to the correct location on the target system, to be installed. Wheel is such a format, whereas distutil's Source Distribution is not, in that it requires a build step before it can be installed.


2 Answers

The distutils "build_py" command is the one that matters, as it's (indirectly) reused by all the commands that create distributions. If you override the byte_compile(files) method, something like:

try:
    from setuptools.command.build_py import build_py
except ImportError:
    from distutils.command.build_py import build_py

class build_py(build_py)
   def byte_compile(self, files):
       super(build_py, self).byte_compile(files)
       for file in files:
           if file.endswith('.py'):
               os.unlink(file)

setup(
    ...
    cmdclass = dict(build_py=build_py),
    ...
)

You should be able to make it so that the source files are deleted from the build tree before they're copied to the "install" directory (which is a temporary directory when bdist commands invoke them).

Note: I have not tested this code; YMMV.

like image 93
PJ Eby Avatar answered Oct 11 '22 14:10

PJ Eby


Try this:

from distutils.command.install_lib import install_lib

class install_lib(install_lib, object):

    """ Class to overload install_lib so we remove .py files from the resulting
    RPM """

    def run(self):

        """ Overload the run method and remove all .py files after compilation
        """

        super(install_lib, self).run()
        for filename in self.install():
            if filename.endswith('.py'):
                os.unlink(filename)

    def get_outputs(self):

        """ Overload the get_outputs method and remove any .py entries in the
        file list """

        filenames = super(install_lib, self).get_outputs()
        return [filename for filename in filenames
                if not filename.endswith('.py')]
like image 20
Alex Avatar answered Oct 11 '22 14:10

Alex