python 3 uses cython to encrypt python project source code

Keywords: Python Lambda Ubuntu

  • Under the project folder, create a new setup.py file, as follows:
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
  • Execute the python 3 setup.py build_ext -- inplace command

    build folders are generated, along with some. so files.

Then delete the non__init_ py *. py source file under the folder

The file of ralbotgen.py in Figure 2 is different from the file of the same name in Figure 1. The file of ralbotgen.py in Figure 2 is the application file of the whole project. The class or function in the top-level file of the project will be invoked to realize the corresponding function. Of course, the corresponding library needs to be import ed.

```

from ralbotgen import ralbotGenerator
from ralbotgen import RDLMessagePrinter

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

```

If you need to migrate from ubuntu to redhat, it's best to re-execute the build with source code.

Posted by FastLaneHosting on Sun, 06 Oct 2019 22:28:41 -0700