首页 > 其他分享 >py文件转换为so文件

py文件转换为so文件

时间:2023-03-09 15:35:44浏览次数:26  
标签:文件 join fw py write so file print path

将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文件

 

标签:文件,join,fw,py,write,so,file,print,path
From: https://www.cnblogs.com/moon3496694/p/17198578.html

相关文章

  • vue中将base64流数据转成pdf文件可打印
    vue中将base64流数据转成pdf文件可打印这次后端返参不是oss那边传回来的,而是传给我一串base64编码的字符串,让我去生成可打印的pdf,返参如下所示。一、base编码转为pdf方......
  • 实验1 Python开发环境使用和编程初体验
    实验任务1task1_1程序源码#task1_1print输出的几种用法#用法1:用于输出单个字符串或单个变量print('hey,u')#用法2:用于输出多个数据项,用逗号分隔print('hey','u......
  • python小题目
    1、有一个列表,去除其中的重复项a=[1,3,12,7,3,1,5,8,12,5,21,44]new_a=list(set(a)) 2、实现IloveChina输出ChinaloveIdeftest_reverse(s......
  • SourceTree安装部署
    SourceTree1、下载地址https://www.sourcetreeapp.com/注意:使用sourcetree必须要先安装git客户端才可以git客户端的下载地址https://git-scm.com/downloads2、git......
  • 实验1 Python开发环境使用和编程初体验
    print(x,y,z)print('x=%d,y=%d,z=%d'%(x,y,z))print('x={},y={},z={}'.format(x,y,z))print(f'x={x},y={y},z={z}')print(......
  • JS关于导出文件流方法封装的使用
    如果需要频繁地在JavaScript中导出文件流,可以将上述的方法封装为函数,以便在不同的代码中调用。/***导出文件*@param{string|Uint8Array}data-文件内容*@para......
  • python+playwright 学习-28 定位多个元素
    前言我们一般定位到页面上唯一的元素再进行操作,有时候一个元素的属性是一样的,会定位到多个元素click方法当定位到页面唯一元素的时候,可以调用click方法<div>......
  • Python分析嵌入式日志小工具开发
    背景数据可度量,无人可值守,问题早发现功能点关键字分析自动压缩LOG自动发送邮件代码#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time    : 2022/8/2......
  • python不设置环境变量 如何安装第三方库
    电脑安装的python环境有好多个,有的是用conda建立的虚拟环境,有的不是,用conda建立的管理起来没什么好说的,自定义的python环境如果没有设置环境变量,在控制台管理起来就不是那......
  • Oracle 查询超级慢之buffer sort
    查询超级慢之buffersort在视图中增加了一个临时表作为一个数据源进行id和名称的转换,没加的时候一秒不到,加了以后14秒,感觉有点问题,于是打开了解释计划看了下,发现这个buffe......