1 Star 0 Fork 11

hdjsjlbs/rpc-repo

forked from Plato/rpc-repo 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
python_driver.py 15.06 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import shutil
import json
import platform
import sys
import subprocess
import distutils
from driver import Initializer
from driver import Builder
from util import *
from option import Options
from string import Template
DEFAULT_CONFIG_FILE = '''
;
; 容器开启的监听器,为一个数组,可以同时开启多个
; host={"127.0.0.1:6888","127.0.0.1:6900"}
;
listener = {
host = ("127.0.0.1:6888")
}
;
; 服务发现配置
; 目前只支持zookeeper
;
service_finder = {
type = "zookeeper"
hosts = "127.0.0.1:2181"
}
rpc_root_dir = "{config_path}"
;
; 容器socket缓冲区大小
;
box_channel_recv_buffer_len = `32*K`
;
; 连接到其他容器的超时时间,单位毫秒
;
connect_other_box_timeout = 2000
;
; 连接到服务发现服务的超时时间,单位毫秒
;
service_finder_connect_timeout = 2000
'''
INIT_FILE = '''
import os
file_dir = os.path.split(os.path.realpath(__file__))[0]
import sys
sys.path.append(file_dir)
import plato
'''
pybackend_url='https://gitee.com/dennis-kk/rpc-backend-python3.git'
frontend_url='https://gitee.com/dennis-kk/rpc-frontend.git'
sbcpp_url='https://gitee.com/dennis-kk/service-box.git'
rpccpp_url='https://gitee.com/dennis-kk/rpc-backend-cpp.git'
clone_cmd='git clone -b {0} {1} --depth=1'
update_cmd='git fetch origin {0}'
def CallCMake(build_type):
if platform.system() == "Windows":
os.system('cmake -G "Visual Studio 16 2019" -A x64')
if build_type == "all":
os.system('cmake --build . --config "Debug"')
os.system('cmake --build . --config "Release"')
elif build_type == "debug":
os.system('cmake --build . --config "Debug"')
elif build_type == "release":
os.system('cmake --build . --config "Release"')
else:
print("Unknown type")
else:
c_name = get_c_compiler()
cxx_name = get_cxx_compiler()
if build_type == "all":
os.system('cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER='+c_name+' -DCMAKE_CXX_COMPILER='+cxx_name+' .')
os.system("make -j"+str(get_cpu_core()))
os.system('cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER='+c_name+' -DCMAKE_CXX_COMPILER='+cxx_name+' .')
os.system("make -j"+str(get_cpu_core()))
elif build_type == "debug":
os.system('cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER='+c_name+' -DCMAKE_CXX_COMPILER='+cxx_name+' .')
os.system("make -j"+str(get_cpu_core()))
elif build_type == "release":
os.system('cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER='+c_name+' -DCMAKE_CXX_COMPILER='+cxx_name+' .')
os.system("make -j"+str(get_cpu_core()))
class Initializer_python(Initializer):
def __init__(self, options, version, proj_root):
Initializer.__init__(self, options, version, proj_root)
self.root_path = os.path.abspath(os.path.dirname(__file__)).replace('\\', '/')
self.cwd = os.getcwd().replace('\\', '/')
def check(self):
if not os.path.exists("repo.init"):
print("Please initialize cpp repo first")
exit(-1)
return True
def build_backend(self):
pass
def checkEnv(self):
pass
def init(self):
if os.path.exists("repo.init.python"):
return True
print("Initializing repo...")
if not check_git_repo([frontend_url, pybackend_url, sbcpp_url, rpccpp_url] , self.get_version()):
print("check dependency failed")
exit(-1)
if os.path.exists("tmp"):
os.chdir("tmp")
if os.path.exists("rpc-frontend"):
os.chdir("rpc-frontend")
os.system('git pull')
os.chdir("..")
else:
if os.system(clone_cmd.format(self.get_version(), frontend_url)) != 0:
print('git clone {0} with {1} failed'.format(frontend_url, self.get_version()))
exit(-1)
os.chdir("..")
if not self.installFramework():
print("Install python framework failed")
exit(-1)
return True
# 安装对应的runtime framework
def installFramework(self):
if not os.path.exists('./tmp'):
os.makedirs('./tmp')
os.chdir('./tmp')
if os.path.exists("rpc-backend-python3"):
os.chdir("rpc-backend-python3")
os.system('git pull')
os.chdir("..")
else:
if os.system(clone_cmd.format(self.get_version(), pybackend_url)) != 0:
print("Install rpc-backend-python3 failed")
exit(-1)
# copy pygen.py
shutil.copy('./rpc-backend-python3/pygen.py', '../bin/pygen.py')
# copy template
distutils.dir_util.copy_tree('./rpc-backend-python3/template/python', '../template/python')
os.chdir('service-box')
os.system('git pull')
os.chdir('..')
os.chdir('./service-box/src/python_rpc')
CallCMake('all')
os.chdir('./../python_log')
CallCMake('all')
os.chdir('../python_config')
CallCMake('all')
os.chdir('../../../../')
open("repo.init.python", "w")
return True
def get_version(self):
return self.version
class Builder_python(Builder):
def __init__(self, options, version, proj_root):
Builder.__init__(self, options, version, proj_root)
self.root_path = os.path.abspath(os.path.dirname(__file__)).replace('\\', '/')
self.cwd = os.getcwd().replace('\\', '/')
def deleteIdl(self, name):
idl_name = self.check_idl_name(name)
(base_name, _) = os.path.splitext(os.path.basename(idl_name))
print(base_name)
if os.path.exists("src/idl/" + base_name + ".idl"):
os.remove("src/idl/" + base_name + ".idl")
if os.path.exists("src/idl/" + base_name + ".idl.protobuf.json"):
os.remove("src/idl/" + base_name + ".idl.protobuf.json")
if os.path.exists("src/idl/" + base_name + ".service.list"):
os.remove("src/idl/" + base_name + ".service.list")
if os.path.exists("src/idl/" + base_name + ".service.proto"):
os.remove("src/idl/" + base_name + ".service.proto")
if os.path.exists(os.path.join(self.proj_root, 'usr', 'python', 'lib', 'config', base_name + '.idl.protobuf.json')):
os.remove(os.path.join(self.proj_root, 'usr', 'python', 'lib', 'config', base_name + '.idl.protobuf.json'))
if os.path.exists(os.path.join(self.proj_root, 'usr', 'python', 'lib', 'config',base_name + '.service.proto')):
os.remove(os.path.join(self.proj_root, 'usr', 'python', 'lib', 'config',base_name + '.service.proto'))
if os.path.exists(os.path.join(self.proj_root, 'usr', 'python', 'lib', 'rpc_config', base_name + '.py')):
os.remove(os.path.join(self.proj_root, 'usr', 'python', 'lib', 'rpc_config', base_name + '.py'))
print(os.path.join(self.proj_root, 'usr', 'python', 'lib', 'test', base_name))
if os.path.exists(os.path.join(self.proj_root, 'usr', 'python', 'lib', 'test', base_name)):
shutil.rmtree(os.path.join(self.proj_root, 'usr', 'python', 'lib', 'test', base_name))
def addIdl2Repo(self, file_name, sname=None, add=True):
if not add:
file_name = "src/idl/" + file_name
if not os.path.exists(file_name):
print(file_name + " not found")
return
file_name = self.check_idl_name(file_name)
(base_name, _) = os.path.splitext(os.path.basename(file_name))
# 递归处理idl import 依赖文件
idl_list = deal_idl_import(file_name)
# 递归拷贝所有idl依赖文件
# 拷贝idl并生成对应的文件和文件夹 src/idl 目录下 保持原有目录一致
if add:
copy_dep_idl(os.path.dirname(file_name), idl_list, self.root_path+'/src/idl')
shutil.copyfile(file_name, './src/idl/' + os.path.basename(file_name))
os.chdir("src/idl/")
cmd = subprocess.Popen(["../../bin/rpc-frontend", "-f", os.path.basename(file_name)], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, encoding='utf-8')
ret = cmd.communicate()
if cmd.returncode != 0:
print("pygen.py failed", "".join(ret))
return
# gen proxy and stub files
cmd = subprocess.Popen([self.pyexec, "../../bin/pygen.py", os.path.basename(file_name) + ".protobuf.json", '../../template/'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ret = cmd.communicate()
if cmd.returncode != 0:
print(ret[1].decode("utf-8"))
exit(-1)
if not os.path.exists(self.proj_root+"/usr/python/lib"):
os.makedirs(self.proj_root+"/usr/python/lib")
if not os.path.exists(self.proj_root+"/usr/python/lib/test"):
os.makedirs(self.proj_root+"/usr/python/lib/test")
if not os.path.exists(self.proj_root+"/usr/python/lib/test/" + base_name):
os.makedirs(self.proj_root+"/usr/python/lib/test/" + base_name)
shutil.copy('../../tmp/rpc-backend-python3/plato.py', self.proj_root+'/usr/python/lib/plato.py')
open(self.proj_root+'/usr/python/lib/__init__.py', "w").write(INIT_FILE)
if platform.system() == "Windows":
shutil.copy('../../tmp/service-box/src/python_rpc/bin/debug/rpc_d.pyd', self.proj_root+'/usr/python/lib/rpc_d.pyd')
shutil.copy('../../tmp/service-box/src/python_rpc/bin/release/rpc.pyd', self.proj_root+'/usr/python/lib/rpc.pyd')
if os.path.exists('../../tmp/service-box/src/python_log/bin/debug/log_d.pyd'):
shutil.copy('../../tmp/service-box/src/python_log/bin/debug/log_d.pyd', self.proj_root+'/usr/python/lib/log_d.pyd')
shutil.copy('../../tmp/service-box/src/python_log/bin/release/log.pyd', self.proj_root+'/usr/python/lib/log.pyd')
if os.path.exists('../../tmp/service-box/src/python_config/bin/debug/config_d.pyd'):
shutil.copy('../../tmp/service-box/src/python_config/bin/debug/config_d.pyd', self.proj_root+'/usr/python/lib/config_d.pyd')
shutil.copy('../../tmp/service-box/src/python_config/bin/release/config.pyd', self.proj_root+'/usr/python/lib/config.pyd')
else:
shutil.copy('../../tmp/service-box/src/python_rpc/bin/librpc.so', self.proj_root+'/usr/python/lib/rpc.so')
if os.path.exists('../../tmp/service-box/src/python_log/bin/liblog.so'):
shutil.copy('../../tmp/service-box/src/python_log/bin/liblog.so', self.proj_root+'/usr/python/lib/log.so')
shutil.copy('../../tmp/service-box/src/python_log/bin/liblog_d.so', self.proj_root+'/usr/python/lib/log_d.so')
if os.path.exists('../../tmp/service-box/src/python_rpc/bin/librpc_d.so'):
shutil.copy('../../tmp/service-box/src/python_rpc/bin/librpc_d.so', self.proj_root+'/usr/python/lib/rpc_d.so')
if os.path.exists('../../tmp/service-box/src/python_config/bin/libconfig.so'):
shutil.copy('../../tmp/service-box/src/python_config/bin/libconfig.so', self.proj_root+'/usr/python/lib/config.so')
shutil.copy('../../tmp/service-box/src/python_config/bin/libconfig_d.so', self.proj_root+'/usr/python/lib/config_d.so')
if os.path.exists('../../tmp/rpc-backend-python3/asan_python.sh'):
shutil.copy('../../tmp/rpc-backend-python3/asan_python.sh', self.proj_root+'/usr/python/asan_python.sh')
shutil.copy('../../tmp/rpc-backend-python3/leak.sup', self.proj_root+'/usr/python/leak.sup')
os.system('chmod +x ' + self.proj_root+'/usr/python/asan_python.sh')
shutil.copy('../../tmp/rpc-backend-python3/run_test.py', self.proj_root+'/usr/python/lib/test/run_test.py')
shutil.copy('../../tmp/rpc-backend-python3/test.cfg', self.proj_root+'/usr/python/lib/test/test.cfg')
# copy generated files to target folder
json_str = json.load(open(os.path.basename(file_name) + ".cpp.json"))
if json_has_attr(json_str, "serviceNames"):
for service_name in json_str["serviceNames"]:
if sname is not None:
if sname != service_name:
continue
if not os.path.exists(self.proj_root+"/usr/python/lib/rpc_config"):
os.makedirs(self.proj_root+"/usr/python/lib/rpc_config")
if not os.path.exists(self.proj_root+"/usr/python/lib/config"):
os.makedirs(self.proj_root+"/usr/python/lib/config")
if os.path.exists(base_name+'.py'):
shutil.copy(base_name+'.py', self.proj_root+"/usr/python/lib/rpc_config/"+base_name+'.py')
if os.path.exists(base_name+'.idl.protobuf.json'):
shutil.copy(base_name+'.idl.protobuf.json', self.proj_root+"/usr/python/lib/config/"+base_name+'.idl.protobuf.json')
if os.path.exists(base_name+'.service.proto'):
shutil.copy(base_name+'.service.proto', self.proj_root+"/usr/python/lib/config/"+base_name+'.service.proto')
if os.path.exists(base_name+'.py'):
os.remove(base_name+'.py')
open(self.proj_root+"/usr/python/lib/config.default.cfg", "w").write(DEFAULT_CONFIG_FILE.replace('{config_path}', self.proj_root+"/usr/python/lib/config"))
if 'services' in json_str:
for service in json_str['services']:
if 'methods' in service:
for method in service['methods']:
method_api_file_name = service['name'] + '_' + method['name'] + '_api_test.py'
if os.path.exists(method_api_file_name):
if not os.path.exists(self.proj_root+"/usr/python/lib/test/"+ base_name + '/'+method_api_file_name):
shutil.copy(method_api_file_name, self.proj_root+"/usr/python/lib/test/"+ base_name + '/'+method_api_file_name)
os.remove(method_api_file_name)
service_api_file_name = base_name + '_' + service['name'] + '_fixture.py'
if os.path.exists(service_api_file_name):
shutil.copy(service_api_file_name, self.proj_root+"/usr/python/lib/test/"+ base_name + '/'+service_api_file_name)
os.remove(service_api_file_name)
def updateRoot(self):
initializer = Initializer_python(Options(), self.get_version(), self.proj_root)
os.chdir("./tmp/rpc-frontend/")
os.system('git pull origin')
if os.path.exists("../rpc-backend-python3/"):
os.chdir("../rpc-backend-python3/")
os.system('git pull --no-rebase "origin" ' + self.get_version())
os.chdir("../../")
else:
os.chdir("..")
if os.system(clone_cmd.format(self.get_version(), pybackend_url)) != 0:
print("Install rpc-backend-python3 failed")
exit(-1)
os.chdir("..")
initializer.installFramework()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/hdjsjlbs/rpc-repo.git
git@gitee.com:hdjsjlbs/rpc-repo.git
hdjsjlbs
rpc-repo
rpc-repo
v0.3.0-alpha

搜索帮助