-
-
Save ophers/093c31bce28f1356b7bf149a99e58326 to your computer and use it in GitHub Desktop.
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} | |
) |
@ophers
great work, it's really helpful! But what if I want to exclude all the python source file except the __init__.py
files at the root of packages? Your solution seems remove all the python source file include __init__.py
. Thank you!
@ophers
I know how to modify the code to meet my needs though I'm not really understand the detail logic, change line 25 to:
if file.endswith('.py') and not file.endswith('__init__.py'):
Thank you very much!
great work, it's really helpful!
Happy to have helped.
BTW, how did you arrive at this page?
I know how to modify the code to meet my needs though I'm not really understand the detail logic, change line 25 to:
if file.endswith('.py') and not file.endswith('__init__.py'):
Why would you need to do this?
Thanks for the code. This is just what I need. However, when I run this, all the pyc files are placed in the pycache directory and not discovered when importing (as per this flow chart).
From this, it looks like the version tag needs to be removed and the files need to end up in the source directory, rather than the pycache directory.
Is there a way to overload that functionality?
As a work around, I have started using pyc_wheel. It makes the build a two-step process, but results in the desired outcome.
@sethgilchrist I developed and tested only with Python2.7.
What version are you using?
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.
@ophers I'm running 3.7.
I found this useful, thank you 👍