[英]Exclude single source file from python bdist_egg or bdist_wheel
背景:我有一个负责安全性的源文件。 有魔术键和特定算法。
是否可以从python egg或wheel包中删除这个单个源文件?
我已经完成了使用egg命令只运送binarys。
python setup.py bdist_egg --exclude-source-files
编辑项目结构:
├── setup.py
├── src
| ├── __init__.py
| ├── file1.py
| ├── file2.py
| ├── file_to_exclude.py
谢谢你的帮助!
不幸的是, distutils
和setuptools
都没有提供排除单个模块的可能性,所以你必须解决它。
我在这里描述了一个更好的解决方案,它模仿了find_packages()
的包排除setuptools
。 您必须覆盖setup脚本中的build_py
命令,该命令可以接受排除模式列表,与find_packages
exclude
列表相同。 在你的情况下,它将是:
import fnmatch
from setuptools import find_packages, setup
from setuptools.command.build_py import build_py as build_py_orig
exclude = ['src.file_to_exclude']
class build_py(build_py_orig):
def find_package_modules(self, package, package_dir):
modules = super().find_package_modules(package, package_dir)
return [(pkg, mod, file, ) for (pkg, mod, file, ) in modules
if not any(fnmatch.fnmatchcase(pkg + '.' + mod, pat=pattern)
for pattern in exclude)]
setup(
...,
packages=find_packages(),
cmdclass={'build_py': build_py},
)
我觉得这是一个更强大和distutils
比下面的那些-conform解决方案。 例如,它还允许通过通配符匹配排除多个模块
exclude = ['src.file*']
将排除在src
包中以file
开头的所有模块,或
exclude = ['*.file1']
将在所有包中排除file1.py
。
您可以使用setuptools
可以排除包(包含__init__.py
文件的目录)的事实,但它需要进行一些重构。 创建一个package_to_exclude
,将file_to_exclude.py
放在那里并修复所有最终的导入错误:
project
├── setup.py
└── src
├── __init__.py
├── file1.py
├── file2.py
└── package_to_exclude
├── __init__.py
└── file_to_exclude.py
现在,您可以在安装脚本中排除package_to_exclude
:
from setuptools import find_packages, setup
setup(
...,
packages=find_packages(exclude=['src.package_to_exclude'])
)
py_modules
包含的模块 如果您不能或不想在单独的包中移动模块,则可以排除src
包并在src
添加除py_modules
file_to_exclude
之外的所有模块。 例:
import os
from setuptools import find_packages, setup
excluded_files = ['file_to_exclude.py']
included_modules = ['src.' + os.path.splitext(f)[0]
for f in os.listdir('src')
if f not in excluded_files]
setup(
...,
packages=find_packages(exclude=['src']),
py_modules=included_modules,
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.