批量处理python转so
1.新建一个文件夹用于存放脚本文件,
mkdir batch_encryption
cd batch_encryption
2.在batch_encryption目录下新建python转so的py文件 setup.py
# coding=utf-8
'''
@Author: Qifeng Wen
@File: setup.py
@Time: 2019/07/13 14:13:45
@Software: PyCharm
'''
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import sys
import os
cwd = os.getcwd()
with open(os.path.join(cwd, 'params.txt')) as f:
name, path = f.readline().strip('\n').split(' ')
path = path.replace('//','/')
setup(
ext_modules=[
Extension(name, [path]),
],
cmdclass={'build_ext': build_ext},
)
3.再在batch_encryption目录下新建shell脚本traverse_folder.sh
shell脚本中的python_env需要替换为当前机器的python环境路径
文件过滤条件可自行更改
#!/bin/bash
# python环境
python_env=/home/wqf/anaconda3/envs/algo-work/include/python3.5m
work_path=`pwd`
read_dir(){
echo ${1}
for file in `ls ${1}`
do
filepath=${1}"/"${file}
if [ -d ${filepath} ]&&[ "${file}" != "opencv-3.4.2" ]&&[ "${file}" != "batch_encryption" ];then
read_dir ${filepath}
elif [ "${file}" != "opencv-3.4.2" ]&&[ "${file}" != "Algorithm" ]&&[ "${file##*.}"x = "py"x ]&&[ "${file}" != "__init__.py" ];then
if [ "${file}" != "worker.py" ]&&[ "${file}" != "manage.py" ]&&[ "${file}" != "apps.py" ]&&[ "${file}" != "config_variable.py" ]&&[ "${file}" != "settings.py" ]&&[ "${file}" != "basic_sample.py" ]&&[ "${file}" != "wsgi.py" ]&&[ "${file}" != "pipeline_base.py" ]&&[ "${file}" != "models.py" ]&&[ "${file}" != "gen_config_files.py" ]&&[ "${file}" != "remove_migration_files.py" ]&&[ "${file}" != "db_init.py" ];then
echo ${file%%.*} ${filepath} > ${work_path}'/params.txt'
echo ${1}"/"${file%%.*}".c-------------->"${1}"/"${file%%.*}".o"
#生成.c文件
python3 setup.py build_ext
gcc -fPIC -I"${python_env}" -c ${1}"/"${file%%.*}".c" -o ${1}"/"${file%%.*}".o"
gcc -shared ${1}"/"${file%%.*}".o" -o ${1}"/"${file%%.*}".so"
#删除生成加密so文件的中间文件
rm ${1}"/"${file%%.*}".o"
rm ${1}"/"${file%%.*}".c"
# 创建了so才删除py对应的py文件
if [ -e ${1}"/"${file%%.*}".so" ]; then
rm ${1}"/"${file%%.*}".py"
fi
fi
fi
done
}
read_dir ${1}
rm -rf ./build params.txt
4.批量生成so文件
bash traverse_folder.sh <要转换的文件夹或文件(绝对路径)>