python2so

批量处理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 <要转换的文件夹或文件(绝对路径)>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值