【发布时间】:2019-09-25 05:11:06
【问题描述】:
我需要混淆我的 python 代码,为了实现它,我使用 cythonize 扩展,我能够实现它并从 a.py 文件中获取二进制编译的 a.so 文件,但是在执行 bdist_wheel 之后,.whl 包仅包a.so 文件并忽略 resource_folder。
我的项目文件结构是
|main_project
|__,setup.py
|__,main_folder
|____,a.py
|____,__init__.py
|____resource_folder
|__,a.model
|__,a.json
我使用以下链接制作了混淆的python轮包,
https://bucharjan.cz/blog/using-cython-to-protect-a-python-codebase.html
https://medium.com/@xpl/protecting-python-sources-using-cython-dcd940bb188e
Package only binary compiled .so files of a python library compiled with Cython
以下是我的 setup.py 中的 sn-p
packages = find_packages(exclude=('tests',))
def get_package_files_in_directory(directory):
paths = []
for (path, directories, filenames) in os.walk(directory):
for filename in filenames:
paths.append(os.path.join('..', path, filename))
return paths
setup(
packages=[],
ext_modules=cythonize(
[
Extension("main_folder.*", ["main_folder/*.py"])
],
build_dir="build",
compiler_directives=dict(
always_allow_keywords=True
)),
package_data={p: package_files + get_package_files_in_directory(os.path.join(here, p, 'resources')) for p in packages},
,....
,...
)
要打包我使用以下命令
python setup.py build_ext
python setup.py bdist_wheel
预期结果是包含 a.so 文件和资源文件夹的 .whl 文件
实际结果是.whl 文件只包含一个.so 文件。
为了还打包资源文件夹,我使用了此链接“(How do you add additional files to a wheel?)”中建议的 get_package_files_in_directory() 函数,但这对我也不起作用
【问题讨论】:
-
您正在为包
main_folder定义包数据,但不包括在 dist 中的main_folder包。 -
是的,我这样做是为了排除 a.py 按照这个答案stackoverflow.com/questions/39499453/… 中的建议被打包到轮子中
-
我明白了。但是,您链接的建议(和接受)答案是错误的,因为在您实际打包它们时排除包会破坏很多东西,包括包数据(没有包,没有包数据)。正确的方法应该是包含包,但不包括下面的所有模块。一旦找到正确的方法,我将发布答案。
-
我添加了一个答案here。如果您遇到任何问题,请更新您的问题,我会为其添加具体答案。
标签: python cython obfuscation setuptools packaging