py文件转换为so文件
将py文件编译为so文件

利用上面代码生成so文件生成的文件不方便取放,名字也不一样
通过执行发现转换执行了下面三句
/usr/local/python3/bin/cython test.py
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/usr/local/python3/include/python3.7m -c test.c -o test.o
gcc -pthread -shared test.o -o test.so
通过命令行执行也可以生成,那么开始编写一个shell脚本
import datetime
import os '''
/usr/local/python3/bin/cython test.py
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/usr/local/python3/include/python3.7m -c test.c -o test.o
gcc -pthread -shared test.o -o test.so
''' def func(path, save_file='build.sh'):
path = path.replace('\\', '/')
path = path.replace('\\', '/')
with open(save_file, "w", encoding='utf-8') as fw:
fw.write("#!/bin/bash" + "\n")
basic_folder = os.path.split(path)[1]
# print(path)
now_time = datetime.datetime.now()
encryp_path = f'encryp_so{now_time.strftime("%Y%m%d%H%M%S")}'
# print(encryp_path)
fw.write("\n")
fw.write(f"mkdir {encryp_path}" + "\n")
fw.write("\n")
fw.write("\n")
for root, dirs, files in os.walk(path):
for file in files:
# print(file)
ftype = str(file).split('.')[-1]
# print(ftype)
if ftype == 'py':
if file != '__init__.py':
folder = os.path.split(root)[1]
if folder != 'migrations':
if root == path:
befor_path = ''
else:
# print(root)
length = len(path)
join_path = root[length+1:]
join_path = join_path.replace('\\', '/')
join_path = join_path.replace('\\', '/')
# print(join_path)
befor_path = f'{join_path}/'
final_file = befor_path+file
fw.write(f"/usr/local/python3/bin/cython {final_file}" + "\n")
fw.write("\n")
fw.write("\n")
fw.write("\n")
for root, dirs, files in os.walk(path):
for file in files:
# print(file)
ftype = str(file).split('.')[-1]
fname = str(file).split('.')[0]
# print(ftype)
if ftype == 'py':
if file != '__init__.py':
folder = os.path.split(root)[1]
if folder != 'migrations':
if root == path:
befor_path = ''
else:
# print(root)
length = len(path)
join_path = root[length + 1:]
join_path = join_path.replace('\\', '/')
join_path = join_path.replace('\\', '/')
# print(join_path)
befor_path = f'{join_path}/'
final_file = befor_path + fname
o_file = befor_path + fname fw.write(f"gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/usr/local/python3/include/python3.7m -c {final_file}.c -o {o_file}.o" + "\n")
# print(root, dirs, files) fw.write("\n")
fw.write("\n")
fw.write("\n")
for root, dirs, files in os.walk(path):
for file in files:
# print(file)
ftype = str(file).split('.')[-1]
fname = str(file).split('.')[0]
# print(ftype)
if ftype == 'py':
if file != '__init__.py':
folder = os.path.split(root)[1]
if folder != 'migrations':
if root == path:
befor_path = ''
else:
# print(root)
length = len(path)
join_path = root[length + 1:]
join_path = join_path.replace('\\', '/')
join_path = join_path.replace('\\', '/')
# print(join_path)
befor_path = f'{join_path}/'
final_file = befor_path + fname
o_file = befor_path + fname fw.write(f"gcc -pthread -shared {final_file}.o -o {o_file}.so" + "\n") fw.write("\n")
fw.write("\n")
fw.write("\n")
fw.write("find . -name '*.c' -delete" + "\n")
fw.write("find . -name '*.o' -delete" + "\n")
fw.write("\n")
for root, dirs, files in os.walk(path):
# print(dirs)
length = len(path)
join_path = root[length + 1:]
join_path = join_path.replace('\\', '/')
join_path = join_path.replace('\\', '/')
# print(join_path, dirs) for dir in dirs:
if '.git' in join_path or '.idea' in join_path:
# print(join_path)
continue
if dir != '__pycache__' and '.git' not in dir and '.idea' not in dir:
if join_path:
new_path = join_path+f'/{dir}'
else:
new_path = dir
fw.write(f"mkdir {encryp_path}/{new_path}" + "\n")
fw.write("\n")
for root, dirs, files in os.walk(path):
for file in files:
# print(file)
ftype = str(file).split('.')[-1]
fname = str(file).split('.')[0]
# print(ftype)
if ftype == 'py':
folder = os.path.split(root)[1]
if folder != 'migrations':
if root == path:
befor_path = ''
else:
# print(root)
length = len(path)
join_path = root[length + 1:]
join_path = join_path.replace('\\', '/')
join_path = join_path.replace('\\', '/')
# print(join_path)
befor_path = f'{join_path}/'
final_file = befor_path + fname
o_file = befor_path + fname
if file != '__init__.py': fw.write(f"mv {final_file}.so {encryp_path}/{o_file}.so" + "\n")
else:
final_file = befor_path + fname
o_file = befor_path + fname
fw.write(f"cp {final_file}.py {encryp_path}/{o_file}.py" + "\n") def start(basic_path, folder_list,save_list): for i in range(len(folder_list)):
path = os.path.join(basic_path, folder_list[i])
func(path, save_list[i]) if __name__ == '__main__':
# file_path = __file__
# basic_path = os.path.split(file_path)[0]
# folder_list = ['']
# save_list = ['build222.sh']
# start(basic_path, folder_list, save_list)
# print(path)
# print(os.path.join(basic_path, '')) basic_path = r'G:\work\debug\test_so'
folder_list = ['aaa']
save_list = ['build221.sh']
start(basic_path, folder_list, save_list)
通过代码生成一个shell脚本,再去执行这个脚本,达到生成一样目录结构的so文件目录
执行完后生成的sh文件
#!/bin/bash mkdir encryp_so20230309145220 /usr/local/python3/bin/cython te.py gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/usr/local/python3/include/python3.7m -c te.c -o te.o gcc -pthread -shared te.o -o te.so find . -name '*.c' -delete
find . -name '*.o' -delete mkdir encryp_so20230309145220/bbb mv te.so encryp_so20230309145220/te.so
cp __init__.py encryp_so20230309145220/__init__.py
直接执行该文件即可得到想要的so文件
py文件转换为so文件的更多相关文章
- python之模块py_compile用法(将py文件转换为pyc文件)
# -*- coding: cp936 -*- #python 27 #xiaodeng #python之模块py_compile用法(将py文件转换为pyc文件):二进制文件,是由py文件经过编译后 ...
- TensorFlow的checkpoint文件转换为pb文件
由于项目需要,需要将TensorFlow保存的模型从ckpt文件转换为pb文件. import os from tensorflow.python import pywrap_tensorflow f ...
- 使用VirtualBox把IMG文件转换为VDI文件
使用VirtualBox把IMG文件转换为VDI文件 首先确保已安装VirtualBox. 需要使用的命令: 语法:$ VBoxManage convertdd input.img output.vd ...
- APNS .p12文件转换为 .pem文件
1:先用mac的钥匙串工具,把APN的推送证书转换为 .p12文件: 2:在mac的终端下 把.p12文件转换为 .pem文件 openssl pkcs12 -in apns-dev-cert.p12 ...
- Python如何实现doc文件转换为docx文件?
Python如何实现doc文件转换为docx文件? 在开发过程中遇到一个关于读写doc和docx的问题: 一个文件夹中有两种文件, 一种为doc结尾, 一种为docx结尾, 需要将这些文件全部重命名. ...
- GIF文件转换为头文件工具
目的: GIF文件转为头文件 举例: 用UE打开GIF文件,如下图所示:图1 test.gif文件将上面文件内容转化为头文件,放到一个数组里面,内容如下:图2 test.h文件 思路: 从上面可知,将 ...
- bat文件转换为exe文件
批处理文件转换为exe文件(简单的处理文件),点击下载 使用超简单的了,不多说.
- 实战FFmpeg--iOS平台使用FFmpeg将视频文件转换为YUV文件
做播放器的开发这里面涉及的东西太多,我只能一步步往前走,慢慢深入.播放器播放视频采用的是渲染yuv文件.首先,要知道yuv文件是怎么转换得来的,其次,要知道怎么把视频文件保存为yuv文件.雷神的文章1 ...
- php如何将base64数据流文件转换为图片文件?
2017-03-07 在开发中,自己遇到一个前端在上传图片的时候,使用的base64数据流文件显示的图片. 也就是说 <img src="data:image/jpg;base64,& ...
- openoffice启动服务并将office文件转换为pdf文件
1.首先下载最新版的openoffice工具,安装完成之后安装服务,, win+r打开命令提示符 输入cmd,cd C:\Program Files (x86)\OpenOffice 4\progra ...
随机推荐
- LocalDateTime接收前端String格式
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime; 实体类属性添加注解,前 ...
- 确定linux系统网卡对应的物理网口
没插网线的情况下系统上执行ethtool -p eth0 #eth0 对应的物理口一个灯在不停的闪烁ethtool --identify eth0 30 #eth0 对应的物理口闪烁30秒 ... ...
- UG二次开发-CAM-执行方式
以C#开发为例,通常先用[操作记录]功能录制关键代码,得到一个.cs文件. (1)可以直接使用[播放操作记录]的方式执行该.cs文件. (2)可以建立一个C#的类库工程,将上述.cs文件加载进去,添加 ...
- Spring Framework学习总结
一.Spring 概述 Spring 有两个核心部分: IoC 和 AOP. Spring 是一种基于 Bean 的编程技术,它深刻地改变着 Java 开发世界.Spring 使用简单.基本的 Jav ...
- 7&的2022年终总结
7&的2022年终总结 文章目录 7&的2022年终总结 1.前言 2.技术 3.生活 4.展望未来 博客搬家的需要: var code = "49d515c3-0238-4 ...
- CH573 CH582 CH579外设ADC例程讲解
在adc的例程中共有六种AD测量,1.温度测量,2.单通道测量,3.DMA单通道测量,4.差分通道测量,5.触摸按键测量,6.中断方式单通道测量,接下来我们逐一描述. 粗调:粗调使得用0db测量VIN ...
- SpringBoot之基于idea搭建ssm(maven)
此次目标为搭建一个简单的基于springboot的ssm(spring + springmvc + mybatis)的maven项目,使用的数据库为mysql. 最终项目目录结构 创建过程 1.项目创 ...
- 关于 Android sdk manager 的安装问题
最近刚刚接触小程序测试,故此需要搭建环境 我用的是python3.6+appium1.8.2+Android sdk manager 关于应用程序,大家有需要的话,可以找我要. 1.关于安装Andro ...
- 网络安全(中职组)-B模块:Web隐藏信息获取
Web隐藏信息获取任务环境说明:服务器场景名:web20200604服务器场景用户名:未知(关闭链接) 1. 通过本地PC中渗透测试平台Kali使用Nmap扫描目标靶机HTTP服务子目录,将扫描 ...
- Python学习笔记--循环的知识以及应用
while循环 代码: 结果: 案例:求1-100的和 实现: 案例:while循环猜数字 实现: while循环的嵌套使用 案例:打印九九乘法表 (注意:要是想要输出不换行,代码可以这样写:prin ...