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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章