Skip to content

Instantly share code, notes, and snippets.

@ophers
Created January 22, 2020 12:51
Show Gist options
  • Save ophers/093c31bce28f1356b7bf149a99e58326 to your computer and use it in GitHub Desktop.
Save ophers/093c31bce28f1356b7bf149a99e58326 to your computer and use it in GitHub Desktop.
Produce a Python Wheel without sources with CLI option --exclude-source-files
import os
from distutils import log
from setuptools import setup, find_packages
from setuptools.command.build_py import build_py as _build_py
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
from wheel.pep425tags import get_impl_ver
class build_py(_build_py):
def initialize_options(self):
_build_py.initialize_options(self)
wc = self.get_finalized_command("bdist_wheel", 0)
self.exclude_source_files = wc and wc.exclude_source_files
def finalize_options(self):
if self.exclude_source_files:
self.force = 1
self.compile = 1
_build_py.finalize_options(self)
def byte_compile(self, files):
_build_py.byte_compile(self, files)
if self.exclude_source_files:
for file in files:
if file.endswith('.py'):
os.unlink(file)
log.info('removing source file %s', file)
class bdist_wheel(_bdist_wheel):
_bdist_wheel.user_options.append(('exclude-source-files', None, "remove all .py files from the generated wheel"))
def initialize_options(self):
_bdist_wheel.initialize_options(self)
self.python_tag = None
self.exclude_source_files = False
def finalize_options(self):
if self.python_tag is None:
if self.exclude_source_files:
self.python_tag = 'py' + get_impl_ver()
else:
self.python_tag = 'py' + get_impl_ver()[0]
_bdist_wheel.finalize_options(self)
setup(
name='package name',
# ...,
setup_requires=['wheel>=0.30;python_version>="2.7"', 'wheel==0.29;python_version<"2.7"'],
cmdclass={"build_py": build_py, "bdist_wheel": bdist_wheel}
)
@spokeydokeys
Copy link

Dug a bit deeper and it appears that moving the compiled files into the distribution directory would require overloading cache_from_source in importlib\_bootstrap_external.py and is likely to be more of a hassle than just executing pyc_wheel.

@spokeydokeys
Copy link

@ophers I'm running 3.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment