python3使用 cython 加密python项目源码 - Go语言中文社区

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。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zhajio/article/details/100043058
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-06 22:34:28
  • 阅读 ( 1542 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢