@@ -0,0 +1,2216 @@
+#!/usr/bin/python%0A# -*- coding: utf-8 -*-%0A%0A%0A%22%22%22%0ACompile python file to linux so file.%0A%22%22%22%0A%0A%0Aimport copy%0Aimport multiprocessing%0Aimport os%0Aimport shutil%0Aimport subprocess%0Aimport sys%0A%0A%0ARAW_SETUP = %22%22%22#!/usr/bin/python%0A# -*- coding: utf-8 -*-%0A%0A%0Afrom distutils.core import setup%0Afrom Cython.Build import cythonize%0A%0Asetup(%0A    ext_modules = cythonize(%22%25s%22)%0A)%0A%22%22%22%0A%0A%0Adef write_setup(setup_string):%0A    with open(%22setup_o.py%22, %22w%22) as fout:%0A        fout.write(setup_string)%0A%0A%0Adef real_cmd():%0A    args = %5B%22python%22, %22setup_o.py%22, %22build_ext%22, %22--inplace%22%5D%0A    process = subprocess.Popen(args)%0A    process.wait()%0A%0A%0Adef run_cmd(file_path):%0A    process = multiprocessing.Process(name='real_cmd', target=real_cmd)%0A    process.start()%0A    process.join()%0A%0A    while 1:%0A        if not process.is_alive():%0A            break%0A%0A%0Adef copy_so(file_path):%0A%0A    filename = os.path.basename(file_path)%0A    dirpath = os.path.dirname(file_path)%0A    so = os.path.splitext(filename)%5B0%5D + %22.so%22%0A    so_path = os.path.join(dirpath, so)%0A    shutil.move(so, so_path)%0A%0A%0Adef rm_c(file_path):%0A    filename = os.path.basename(file_path)%0A    dirpath = os.path.dirname(file_path)%0A    c = os.path.splitext(filename)%5B0%5D + %22.c%22%0A    c_path = os.path.join(dirpath, c)%0A%0A    os.unlink(c_path)%0A%0A%0A%0Adef compile_file(file_path):%0A    %22%22%22%0A    The steps of compile a py file to a .so file:%0A    1. Write a setup.py file. In this case, we use %60setup_o.py%60 instead%0A    2. Compile the python file.%0A    3. Copy .so file to the folder where the original py file holds.%0A    4. Remove c file & setup_o.py, remove build folder.%0A    %22%22%22%0A    if not file_path.endswith(%22.py%22):%0A        return%0A    setup_string = copy.deepcopy(RAW_SETUP)%0A    setup_string = setup_string %25 file_path%0A    write_setup(setup_string)%0A    run_cmd(file_path)%0A    copy_so(file_path)%0A    rm_c(file_path)%0A%0A%0Adef walk(base_dir):%0A    for root, dirs, files in os.walk(base_dir):%0A        for _file in files:%0A            compile_file(os.path.join(root, _file))%0A        for _dir in dirs:%0A            walk(os.path.join(root, _dir))%0A    base_dir = os.path.dirname(os.path.abspath(__file__))%0A    shutil.rmtree(os.path.join(base_dir, %22build%22))%0A    os.unlink(%22setup_o.py%22)%0A%0A%0Aif __name__ == '__main__':%0A    walk(sys.argv%5B1%5D)%0A

add compile python file to so file script