简体   繁体   中英

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).

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.

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')]

Maybe a full working code here :)

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

import os
import py_compile

class custom_build_pyc(build_py):
    def byte_compile(self, files):
        for file in files:
            if file.endswith('.py'):
                py_compile.compile(file)
                os.unlink(file)
....
setup(
    name= 'sample project',
    cmdclass = dict(build_py=custom_build_pyc),
....

"the standard commands don't have such an option"?

Do you have the latest version of setuptools installed? And did you write a setup.py file?

If so, this should work: python setup.py bdist_egg --exclude-source-files .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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