python3使用 cython 加密python项目源码

  • 在项目文件夹下,新建setup.py文件,如下: 
import os
import re
from distutils.core import Extension, setup
from Cython.Build import cythonize
from Cython.Compiler import Options
 
 
exclude_so = ['__init__.py', "setup.py"]
sources = ['./']
 
extensions = []
for source in sources:
    for dirpath, foldernames, filenames in os.walk(source):
        for filename in filter(lambda x: re.match(r'.*[.]py$', x), filenames):
            print(filename)
            file_path = os.path.join(dirpath, filename)
            if filename not in exclude_so:
                print("debug point ", file_path[:-3].replace('/', '.')[2:])
                extensions.append(
                        Extension(file_path[:-3].replace('/', '.')[2:], [file_path], extra_compile_args = ["-Os", "-g0"],
                                  extra_link_args = ["-Wl,--strip-all"]))
 
 
print("debug point 1")
Options.docstrings = False
compiler_directives = {'optimize.unpack_method_calls': False}
setup(  
        ext_modules = cythonize(extensions, exclude = None, nthreads = 20, quiet = True, build_dir = './build',
                                language_level = 3 , compiler_directives = compiler_directives))

#python setup.py build_ext --inplace
  • 执行python3 setup.py build_ext --inplace 命令

    会生成build文件夹,以及一些.so文件。

然后将文件夹下的非__init__.py的*.py 源码文件删除

图二中的ralbotgen.py的文件和图一中的同名文件不同,图二中的ralbotgen.py是整个项目的应用文件,里面会调用项目顶层文件中的类或者函数实现对应的功能,当然需要import相应的库。

```

from ralbotgen import ralbotGenerator
from ralbotgen import RDLMessagePrinter

if __name__ == "__main__":
    ralbotGenerator(RDLMessagePrinter()).export()

```

如果需要从ubuntu移植到redhat,最好用源码重新执行build。

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值