Skipping compilation of some .py files on "install"?

The email package has two files to help it maintain compatibility with Python 2.1 and Python 2.2+. There's a _compat21.py and a _compat22.py that contains tricky bits that are different between the two Python versions. Other code that needs something out of these modules, first tries to import _compat22.py, wrapping this import so that if errors occur, it backs down to _compat21.py. The problem comes when I do "python2.1 setup.py install", which tries to bytecompile all the .py files. Naturally Python 2.1 won't be able to compile _compat22.py and indeed you get a SyntaxError. Is there a way -- in my setup.py -- that I can first detected which Python version I'm using (should be easy), and then skip byte compilation of _compat22.py when I find I'm using Python 2.1? Thanks, -Barry

Barry A. Warsaw wrote:
Not builtin, but it should be easy to write your own subclass which implements this. Note that the installers sometimes use their own code to byte-compile files late during the install. You can only either choose to disable the compile completely or live with the SyntaxError (if the installers let you ;-). -- Marc-Andre Lemburg CEO eGenix.com Software GmbH ______________________________________________________________________ Company & Consulting: http://www.egenix.com/ Python Software: http://www.egenix.com/files/python/

"MAL" == M <mal@lemburg.com> writes:
>> The email package has two files to help it maintain >> compatibility with Python 2.1 and Python 2.2+. There's a >> _compat21.py and a _compat22.py that contains tricky bits that >> are different between the two Python versions. >> [...] >> Naturally Python 2.1 won't be able to compile _compat22.py and >> indeed you get a SyntaxError. Is there a way >> -- in my setup.py -- that I can [...] skip byte >> compilation of _compat22.py when I find I'm using Python 2.1? MAL> Not builtin, but it should be easy to write your own subclass MAL> which implements this. And indeed it was! Thanks for the hint MAL. Below is what I came up with. -Barry -------------------- snip snip --------------------setup.py import sys from os.path import basename from distutils.core import setup from distutils.command.install_lib import install_lib class EmailInstall(install_lib): def byte_compile(self, files): # For Python 2.1.x do not byte compile the _compat22.py file since # that will most definitely fail. Any newer Python can compile # everything. major, minor = sys.version_info[0:2] if major == 2 and minor == 1: files = [f for f in files if basename(f) <> '_compat22.py'] return install_lib.byte_compile(self, files) setup(name='email', version='2.0.5', description='Next generation MIME library', author='Barry Warsaw', author_email='barry@zope.com', url='http://sf.net/projects/mimelib', packages=['email'], # Because we need to selectively byte-compile cmdclass={'install_lib': EmailInstall}, )

Barry A. Warsaw wrote:
Not builtin, but it should be easy to write your own subclass which implements this. Note that the installers sometimes use their own code to byte-compile files late during the install. You can only either choose to disable the compile completely or live with the SyntaxError (if the installers let you ;-). -- Marc-Andre Lemburg CEO eGenix.com Software GmbH ______________________________________________________________________ Company & Consulting: http://www.egenix.com/ Python Software: http://www.egenix.com/files/python/

"MAL" == M <mal@lemburg.com> writes:
>> The email package has two files to help it maintain >> compatibility with Python 2.1 and Python 2.2+. There's a >> _compat21.py and a _compat22.py that contains tricky bits that >> are different between the two Python versions. >> [...] >> Naturally Python 2.1 won't be able to compile _compat22.py and >> indeed you get a SyntaxError. Is there a way >> -- in my setup.py -- that I can [...] skip byte >> compilation of _compat22.py when I find I'm using Python 2.1? MAL> Not builtin, but it should be easy to write your own subclass MAL> which implements this. And indeed it was! Thanks for the hint MAL. Below is what I came up with. -Barry -------------------- snip snip --------------------setup.py import sys from os.path import basename from distutils.core import setup from distutils.command.install_lib import install_lib class EmailInstall(install_lib): def byte_compile(self, files): # For Python 2.1.x do not byte compile the _compat22.py file since # that will most definitely fail. Any newer Python can compile # everything. major, minor = sys.version_info[0:2] if major == 2 and minor == 1: files = [f for f in files if basename(f) <> '_compat22.py'] return install_lib.byte_compile(self, files) setup(name='email', version='2.0.5', description='Next generation MIME library', author='Barry Warsaw', author_email='barry@zope.com', url='http://sf.net/projects/mimelib', packages=['email'], # Because we need to selectively byte-compile cmdclass={'install_lib': EmailInstall}, )
participants (2)
-
Barry A. Warsaw
-
M.-A. Lemburg