1 Star 0 Fork 11

太子0007/rpc-repo

forked from Plato/rpc-repo 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cpp_driver.py 44.22 KB
一键复制 编辑 原始数据 按行查看 历史
dennis-kk 提交于 2022-05-18 09:50 . DEFAULT CPU COUNT
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import shutil
import json
import platform
import sys
import subprocess
from driver import Initializer
from driver import Builder
from util import *
from option import Options
from string import Template
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}'
class CMakeTemplate(Template):
delimiter = '%'
def CallCMake(build_type, cpu_core=0):
if cpu_core == 0:
cpu_core = get_cpu_core() - 1
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(cpu_core))
os.system('cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER='+c_name+' -DCMAKE_CXX_COMPILER='+cxx_name+' .')
os.system("make -j"+str(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(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(cpu_core))
class Initializer_cpp(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 os.path.exists("repo.init"):
return True
if platform.system() == "Linux":
error = os.system('curl https://gitee.com/dennis-kk/service-box/raw/{0}/build/initgcc.sh | bash'.format(self.get_version()))
if error != 0:
print("Initialize compile environment failed")
return False
if not Initializer.checkPrecondition(self):
return False
return True
def build_backend(self):
os.chdir("rpc-backend-cpp")
shutil.copyfile("src/cppgen.py", "../../bin/cppgen.py")
shutil.copyfile("src/cppgen_pb_layer.py", "../../bin/cppgen_pb_layer.py")
shutil.copyfile("src/cppgen_sdk.py", "../../bin/cppgen_sdk.py")
shutil.copyfile("src/rpc_root.h", "../../src/include/root/rpc_root.h")
shutil.copyfile("src/object_pool.h", "../../src/include/root/object_pool.h")
shutil.copyfile("src/rpc_defines.h", "../../src/include/root/rpc_defines.h")
shutil.copyfile("src/rpc_logger.h", "../../src/include/root/rpc_logger.h")
shutil.copyfile("src/rpc_proxy.h", "../../src/include/root/rpc_proxy.h")
shutil.copyfile("src/rpc_service.h", "../../src/include/root/rpc_service.h")
shutil.copyfile("src/rpc_singleton.h", "../../src/include/root/rpc_singleton.h")
shutil.copyfile("src/rpc_stub.h", "../../src/include/root/rpc_stub.h")
shutil.copyfile("src/rpc_transport.h", "../../src/include/root/rpc_transport.h")
shutil.copyfile("src/rpc_statistics.hh", "../../src/include/root/rpc_statistics.hh")
shutil.copyfile("src/coroutine/coroutine.h", "../../src/include/root/coroutine/coroutine.h")
shutil.copyfile("src/coroutine/detail/list.h", "../../src/include/root/coroutine/detail/list.h")
shutil.copyfile("src/coroutine/detail/timer.h", "../../src/include/root/coroutine/detail/timer.h")
shutil.copyfile("src/rpc_interface_descriptor.hh", "../../src/include/root/rpc_interface_descriptor.hh")
shutil.copyfile("src/rpc_probe.h", "../../src/include/root/rpc_probe.h")
shutil.copyfile("src/rpc_interface.h", "../../src/include/root/rpc_interface.h")
shutil.copyfile("src/rpc_limiter.h", "../../src/include/root/rpc_limiter.h")
shutil.copyfile("src/rpc_proxy_call_interface.h", "../../src/include/root/rpc_proxy_call_interface.h")
shutil.copyfile("src/rpc_proxy_call_manager_interface.h", "../../src/include/root/rpc_proxy_call_manager_interface.h")
shutil.copyfile("src/rpc_proxy_creator_interface.h", "../../src/include/root/rpc_proxy_creator_interface.h")
shutil.copyfile("src/rpc_proxy_factory_interface.h", "../../src/include/root/rpc_proxy_factory_interface.h")
shutil.copyfile("src/rpc_proxy_interface.h", "../../src/include/root/rpc_proxy_interface.h")
shutil.copyfile("src/rpc_proxy_manager_interface.h", "../../src/include/root/rpc_proxy_manager_interface.h")
shutil.copyfile("src/rpc_stub_call_interface.h", "../../src/include/root/rpc_stub_call_interface.h")
shutil.copyfile("src/rpc_stub_call_manager_interface.h", "../../src/include/root/rpc_stub_call_manager_interface.h")
shutil.copyfile("src/rpc_stub_interface.h", "../../src/include/root/rpc_stub_interface.h")
shutil.copyfile("src/rpc_stub_manager_interface.h", "../../src/include/root/rpc_stub_manager_interface.h")
shutil.copyfile("src/rpc_stub_factory_interface.h", "../../src/include/root/rpc_stub_factory_interface.h")
shutil.copyfile("src/rpc_proxy_impl.h", "../../src/include/root/rpc_proxy_impl.h")
shutil.copyfile("src/rpc_proxy_impl.cpp", "../../src/include/root/rpc_proxy_impl.cpp")
shutil.copyfile("src/rpc_proxy_call_impl.h", "../../src/include/root/rpc_proxy_call_impl.h")
shutil.copyfile("src/rpc_proxy_call_impl.cpp", "../../src/include/root/rpc_proxy_call_impl.cpp")
shutil.copyfile("src/rpc_stub_impl.h", "../../src/include/root/rpc_stub_impl.h")
shutil.copyfile("src/rpc_stub_impl.cpp", "../../src/include/root/rpc_stub_impl.cpp")
shutil.copyfile("src/rpc_stub_call_impl.cpp", "../../src/include/root/rpc_stub_call_impl.cpp")
shutil.copyfile("src/rpc_stub_call_impl.h", "../../src/include/root/rpc_stub_call_impl.h")
shutil.copyfile("src/rpc_stub_creator_interface.h", "../../src/include/root/rpc_stub_creator_interface.h")
shutil.copyfile("src/rpc_stub_creator_impl.h", "../../src/include/root/rpc_stub_creator_impl.h")
shutil.copyfile("src/rpc_stub_creator_impl.cpp", "../../src/include/root/rpc_stub_creator_impl.cpp")
shutil.copyfile("src/rpc_stub_dynamic_factory.h", "../../src/include/root/rpc_stub_dynamic_factory.h")
shutil.copyfile("src/rpc_stub_dynamic_factory.cpp", "../../src/include/root/rpc_stub_dynamic_factory.cpp")
shutil.copyfile("src/rpc_statistics_impl.hh", "../../src/include/root/rpc_statistics_impl.hh")
shutil.copyfile("src/rpc_statistics_impl.cpp", "../../src/include/root/rpc_statistics_impl.cpp")
if platform.system() == "Windows":
shutil.copyfile("src/coroutine/windows/coroutine.h", "../../src/include/root/coroutine/windows/coroutine.h")
else:
shutil.copyfile("src/coroutine/linux/coroutine.h", "../../src/include/root/coroutine/linux/coroutine.h")
os.chdir("..")
def inspectpath(self):
if not os.path.exists("tmp"):
os.mkdir("tmp")
if not os.path.exists("bin"):
os.mkdir("bin")
if not os.path.exists("usr/impl"):
os.makedirs("usr/impl")
if not os.path.exists("lib/root"):
os.makedirs("lib/root")
if not os.path.exists("lib/proxy"):
os.makedirs("lib/proxy")
if not os.path.exists("lib/stub"):
os.makedirs("lib/stub")
if not os.path.exists("src/idl"):
os.makedirs("src/idl")
if not os.path.exists("src/include/root/coroutine/detail"):
os.makedirs("src/include/root/coroutine/detail")
if platform.system() == "Windows":
if not os.path.exists("src/include/root/coroutine/windows"):
os.makedirs("src/include/root/coroutine/windows")
else:
if not os.path.exists("src/include/root/coroutine/linux"):
os.makedirs("src/include/root/coroutine/linux")
if not os.path.exists("src/proxy"):
os.makedirs("src/proxy")
if not os.path.exists("src/stub"):
os.makedirs("src/stub")
def init(self):
print("Initializing repo...")
if not Initializer.checkPrecondition(self):
exit(-1)
if not check_git_repo([frontend_url, sbcpp_url, rpccpp_url] , self.get_version()):
print("check dependency failed")
exit(-1)
self.inspectpath()
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)
self.build_frontend()
os.chdir("..")
os.chdir("tmp")
if os.path.exists("rpc-backend-cpp"):
os.chdir("rpc-backend-cpp")
os.system('git pull --no-rebase "origin" ' + self.get_version())
os.chdir("..")
else:
if os.system(clone_cmd.format(self.get_version(), rpccpp_url)) != 0:
print('git clone {0} with {1} failed'.format(rpccpp_url, self.get_version()))
exit(-1)
#backend has been compiled in service-box as source code
#FIXME: proxy stub 存在依赖
self.build_backend()
os.chdir("..")
if not self.installFramework():
print("Install cpp framework failed")
exit(-1)
open("repo.init", "w+").close()
return True
def checkEnv(self):
os.system("protoc --version")
os.system("git --version")
os.system("cmake --version")
if platform.system() == "Linux":
if os.system("gcc --version") != 0:
os.system("clang --version")
else:
print("Visual Studio 16 2019")
# 安装对应的service box framework
# get tag from VERSION file
def installFramework(self):
# 切换中间文件进行编译
if not os.path.exists('./tmp'):
os.makedirs('./tmp')
os.chdir('./tmp')
# 拉取最新的tag
if os.path.exists('service-box'):
## 不一定在当前分支需要切换一下
os.chdir("service-box")
os.system('git checkout .')
os.system(update_cmd.format(self.get_version()))
os.system('git reset --hard FETCH_HEAD')
os.chdir("..")
elif os.system(clone_cmd.format(self.get_version(), sbcpp_url)) != 0:
print("Install rpc-backend-cpp failed")
exit(-1)
if os.path.exists('./service-box/thirdparty/src/rpc-backend-cpp/src'):
shutil.rmtree('./service-box/thirdparty/src/rpc-backend-cpp/src')
shutil.copytree('./rpc-backend-cpp/src', './service-box/thirdparty/src/rpc-backend-cpp/src')
if os.path.exists('./service-box/thirdparty/include/root'):
shutil.rmtree('./service-box/thirdparty/include/root')
shutil.copytree('./rpc-backend-cpp/src', './service-box/thirdparty/include/root')
os.chdir('./service-box/build')
#产生一个子进程,针对不同平台去获取对应的权限
if platform.system() == "Windows":
cmd = subprocess.Popen([self.pyexec, "build_local_env_win.py", self.get_version()], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
ret = cmd.communicate()
if cmd.returncode != 0:
print("".join(ret))
return False
os.system('cmake -DCMAKE_INSTALL_PREFIX=..\\..\\..\\framework\\service-box-cpp-framework -G "Visual Studio 16 2019" -A x64 ../src')
os.system('cmake --build . --config "Debug"')
os.system('cmake --build . --config "Release"')
os.system('cmake --install . --config "Debug"')
os.system('cmake --install . --config "Release"')
elif platform.system() == "Linux":
#using root user
os.system('sudo bash install_linux.sh')
err=os.system('{0} build_local_env_linux.py {1}'.format(self.pyexec, self.get_version()))
if err > 0:
exit(-1)
#using cur user cur dir repo/tmp/service-box/build
#install to repo/framework/service-box-cpp-framework
os.system('bash build_repo_linux.sh ../../../framework/service-box-cpp-framework')
#切换回主目录拷贝
shutil.copyfile("../thirdparty/src/rpc-backend-cpp/src/cppgen.py", "../../../bin/cppgen.py")
shutil.copyfile("../thirdparty/src/rpc-backend-cpp/src/cppgen_pb_layer.py", "../../../bin/cppgen_pb_layer.py")
shutil.copyfile("../thirdparty/src/rpc-backend-cpp/src/cppgen_sdk.py", "../../../bin/cppgen_sdk.py")
#change to repo
os.chdir('../../../')
return True
class Builder_cpp(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 list_all_dir(self, rootdir):
_dirs = []
list = os.listdir(rootdir)
for i in range(0, len(list)):
path = rootdir + '/' + list[i]
if os.path.isdir(path):
_dirs.append(path)
_dirs.extend(self.list_all_dir(path))
return _dirs
def build_sdk(self, name, sname=None, build_type="all"):
idl_name = self.check_idl_name(name)
if not os.path.exists("src/idl/" + idl_name):
print (name + " not found in repo.")
return
(base_name, _) = os.path.splitext(os.path.basename(idl_name))
if not os.path.exists("tmp/" + base_name + "/proxy"):
os.makedirs("tmp/" + base_name + "/proxy")
if not os.path.exists("tmp/" + base_name + "/stub"):
os.makedirs("tmp/" + base_name + "/stub")
if not os.path.exists("lib/" + base_name):
os.makedirs("lib/" + base_name)
if not os.path.exists("lib/proxy/" + base_name):
os.makedirs("lib/proxy/" + base_name)
if not os.path.exists("lib/stub/" + base_name):
os.makedirs("lib/stub/" + base_name)
json_str = json.load(open("src/idl/" + base_name + ".idl.cpp.json"))
if json_str["serviceNames"] :
for service_name in json_str["serviceNames"]:
if not os.path.exists("tmp/" + base_name + "/proxy/" + service_name):
os.makedirs("tmp/" + base_name + "/proxy/" + service_name)
if not os.path.exists("tmp/" + base_name + "/stub/" + service_name):
os.makedirs("tmp/" + base_name + "/stub/" + service_name)
os.chdir("tmp/" + base_name)
file = open("CMakeLists.txt", "w")
tcmake = CMakeTemplate(open(self.root_path+'/template/cpp/common_pb_layer-CMakeLists.txt.template', 'r').read())
# rpc-backend-cpp CMakeLists.txt
file.write(tcmake.substitute(IDL_NAME=base_name, REPO_BASE_DIR=self.root_path, PROJECT_BASE_DIR=self.proj_root))
file.close()
CallCMake(build_type)
if json_has_attr(json_str, "services"):
for service in json_str["services"]:
if sname is not None:
if sname != service["name"]:
continue
os.chdir("proxy/" + service["name"])
file = open("CMakeLists.txt", "w")
tcmake = CMakeTemplate(open(self.root_path+'/template/cpp/service_proxy_sdk-CMakeLists.txt.template', 'r').read())
file.write(tcmake.substitute(REPO_BASE_DIR=self.root_path, IDL_NAME=base_name, SERVICE_NAME=service["name"], PROJECT_BASE_DIR=self.proj_root))
file.close()
CallCMake(build_type)
os.chdir("../../")
os.chdir("../../")
def build_idl(self, name, sname=None, build_type="all", make_service_type="all",version=""):
idl_name = self.check_idl_name(name)
if not os.path.exists("src/idl/" + idl_name):
print (name + " not found in repo.")
return
(base_name, _) = os.path.splitext(os.path.basename(idl_name))
if not os.path.exists("tmp/" + base_name + "/proxy"):
os.makedirs("tmp/" + base_name + "/proxy")
if not os.path.exists("tmp/" + base_name + "/stub"):
os.makedirs("tmp/" + base_name + "/stub")
if not os.path.exists("lib/" + base_name):
os.makedirs("lib/" + base_name)
if not os.path.exists("lib/proxy/" + base_name):
os.makedirs("lib/proxy/" + base_name)
if not os.path.exists("lib/stub/" + base_name):
os.makedirs("lib/stub/" + base_name)
json_str = json.load(open("src/idl/" + base_name + ".idl.cpp.json"))
if json_str["serviceNames"] :
for service_name in json_str["serviceNames"]:
if not os.path.exists("tmp/" + base_name + "/proxy/" + service_name):
os.makedirs("tmp/" + base_name + "/proxy/" + service_name)
if not os.path.exists("tmp/" + base_name + "/stub/" + service_name):
os.makedirs("tmp/" + base_name + "/stub/" + service_name)
os.chdir("tmp/" + base_name)
file = open("CMakeLists.txt", "w")
tcmake = CMakeTemplate(open(self.root_path+'/template/cpp/common_pb_layer-CMakeLists.txt.template', 'r').read())
# common_pb_layer CMakeLists.txt
file.write(tcmake.substitute(REPO_BASE_DIR=self.root_path, IDL_NAME=base_name, PROJECT_BASE_DIR=self.proj_root))
file.close()
CallCMake(build_type)
if json_has_attr(json_str, "services"):
for service in json_str["services"]:
if sname is not None:
if sname != service["name"]:
continue
os.chdir("proxy/" + service["name"])
file = open("CMakeLists.txt", "w")
tcmake = CMakeTemplate(open(self.root_path+'/template/cpp/service_proxy-CMakeLists.txt.template', 'r').read())
# service_proxy CMakeLists.txt
file.write(tcmake.substitute(REPO_BASE_DIR=self.root_path, IDL_NAME=base_name, SERVICE_NAME=service["name"], PROJECT_BASE_DIR=self.proj_root))
file.close()
if make_service_type == "all" or make_service_type == "proxy":
CallCMake(build_type)
os.chdir("../../")
if json_has_attr(json_str, "services"):
for service in json_str["services"]:
if sname is not None:
if sname != service["name"]:
continue
os.chdir("stub/" + service["name"])
file = open("CMakeLists.txt", "w")
if service["loadType"] == "dynamic":
tcmake = CMakeTemplate(open(self.root_path+'/template/cpp/service_stub_dynamic-CMakeLists.txt.template', 'r').read())
# service_stub CMakeLists.txt
file.write(tcmake.substitute(REPO_BASE_DIR=self.root_path, IDL_NAME=base_name, SERVICE_NAME=service["name"], PROJECT_BASE_DIR=self.proj_root))
else:
tcmake = CMakeTemplate(open(self.root_path+'/template/cpp/service_stub_static-CMakeLists.txt.template', 'r').read())
# service_stub CMakeLists.txt
file.write(tcmake.substitute(REPO_BASE_DIR=self.root_path, IDL_NAME=base_name, SERVICE_NAME=service["name"], PROJECT_BASE_DIR=self.proj_root))
file.close()
if make_service_type == "all" or make_service_type == "stub":
CallCMake(build_type)
os.chdir("../../")
os.chdir("../../")
def beforCreate(self, file_name, sname, add):
(base_name, _) = os.path.splitext(os.path.basename(file_name))
json_str = json.load(open(base_name + ".idl.cpp.json"))
if ("serviceNames" in json_str) and (json_str["serviceNames"] is not None):
for service_name in json_str["serviceNames"]:
if sname is not None:
if sname != service_name:
continue
file_name = self.cwd+"/usr/impl/" + base_name + "/" + service_name + "/" + base_name + '.service.' + service_name + ".impl.h"
if not os.path.exists(file_name) or os.path.exists(base_name + ".service." + service_name + ".impl.h"):
continue
shutil.move(file_name, base_name + ".service." + service_name + ".impl.h")
# @title 构建测试server 用于测试
def build_beta_server(self, file_name, sname=None):
# 检查服务 是否被编译
file_name = os.path.basename(file_name)
file_name = file_name.split('.')[0]
if not os.path.exists('./src/idl/' + file_name + '.idl'):
print('[Error] idl ', file_name, "not import yet !!!!")
return False
json_str = json.load(open('./src/idl/' + file_name + ".idl.cpp.json"))
if not json_str:
print('[Error] open idl descript file error !!')
return False
# idl 没有service 我生成个蛋蛋锤
if not json_has_attr(json_str, 'services'):
print('[Info] idl {file_name} not contains any service '.format(file_name=file_name), file=sys.stderr)
return True
depst = {}
if json_has_attr(json_str, 'structNames'):
depst = json_str['structNames']
depen = None
if json_has_attr(json_str, 'enumNames'):
depen= json_str['enumNames']
for service in json_str["services"]:
if (sname is not None) and (sname != service["name"]):
continue
# 准备好必要的 变量
uuid = service['uuid']
service_name = service['name']
#is_dynamic = (service['loadType'] == 'dynamic')
# 检查依赖中间文件 比如dll 和 .a 这种文件
if not check_service_build(file_name, service_name, True):
print('[Error] service:{sname} in {file_name}.idl not build yet'.format(sname=service_name, file_name=file_name), file=sys.stderr)
return False
# 检查需要的目录 准备生成工程
# 检查必要目录 tmp/idlname/test/service_name/proxy or server 如果目录存在说明生成过了 不存在就新建
test_path = self.proj_root + './test/mock/{idl_name}/{service_name}'.format(idl_name=file_name, service_name=service_name)
if not os.path.exists(test_path):
print('Mock dir {name} not exit'.format(name=test_path))
os.makedirs(test_path)
# 拷贝thirdparty 过去反正没有多大懒得改工程CMake结构了
if not os.path.exists(test_path + '/thirdparty'):
shutil.copytree('./mock/cmock/thirdparty', test_path+'/thirdparty')
# 生成 proxy 相关的文件 还有CMake
try:
gen_test_proxy(test_path, file_name, service_name, service['methods'], depst, depen)
except Exception as err:
print('[Error] generate server test project caller error {0} {1}'.format(err, err.args))
return False
# # 读取server template 生成serve.cpp cmakelist 拷贝transport
# test_path = './tmp/{idl_name}/test/{service_name}/server'.format(idl_name=file_name, service_name=service_name)
# if not os.path.exists(test_path):
# print('Mock dir {name} not exit'.format(name=test_path))
# os.makedirs(test_path)
# # 拷贝thirdparty 过去反正没有多大懒得改工程CMake结构了
# if not os.path.exists(test_path+'/thirdparty'):
# shutil.copytree('./mock/cmock/thirdparty', test_path + '/thirdparty')
# try:
# # 生成server.cpp
# genfile = open(test_path+'/server.cpp', 'w')
# tfile = open('./mock/template/dynamic_server.template', 'r').read()
# server_template = Template(tfile)
# genfile.write(server_template.substitute(base_name=file_name, service_name=service_name, uuid=uuid))
# genfile.close()
# #拷贝mock trans port
# shutil.copyfile('./mock/cmock/src/mock_transport.h', test_path + '/mock_transport.h')
# shutil.copyfile('./mock/cmock/src/mock_transport.cpp', test_path + '/mock_transport.cpp')
# shutil.copyfile('./mock/cmock/src/mock_logger.h', test_path + '/mock_logger.h')
# # 生成 cmakelist
# tcmake = None
# tcmake = Template(open('./mock/template/server_cmake_dynamic.template', 'r').read())
# gen_cmake = open(test_path+'/CMakeLists.txt', 'w')
# gen_cmake.write(tcmake.substitute(base_name=file_name, service_name=service_name, execname=service_name.lower()))
# gen_cmake.close()
# except Exception as err:
# print('[Error] generate server test project error {0}'.format(err))
# return False
def do_lua_template(self, base_name, service_name, idl_name):
lua_impl_h_file_path = self.proj_root+"/usr/impl/" + base_name + "/" + idl_name + '/' + base_name + '.service.' + service_name + '.impl.h'
hpp_replace_file_name = base_name + '.service.' + service_name + '.h'
#shutil.copyfile(self.cwd+"/usr/lua_template/lua_service_template.h", lua_impl_h_file_path)
hpp_template = Template(open(self.cwd+"/usr/lua_template/lua_template_hpp.template", 'r').read())
hpp_file = open(lua_impl_h_file_path, 'w')
hpp_file.write(hpp_template.substitute(idl_name=base_name, service_name=service_name, head_file=hpp_replace_file_name))
lua_impl_file_path = self.proj_root+"/usr/impl/" + base_name + "/" + idl_name + '/' + base_name + '.service.' + service_name + '.cpp'
cpp_replace_file_name = base_name + '.service.' + service_name + '.impl.h'
#shutil.copyfile(self.cwd+"/usr/lua_template/lua_service_template.cpp", lua_impl_file_path)
cpp_template = Template(open(self.cwd+"/usr/lua_template/lua_template_cpp.template", 'r').read())
cpp_file = open(lua_impl_file_path, 'w')
cpp_file.write(cpp_template.substitute(idl_name=base_name, service_name=service_name, head_file=cpp_replace_file_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)
# 递归处理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/")
# GET OLD VERSION
old_md5 = ""
md5file = os.path.basename(file_name) + ".cpp.json.md5"
if os.path.exists(md5file):
old_md5 = open(md5file).read()
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("exec frontend error ", "".join(ret))
return
new_md5 = ""
if os.path.exists(md5file):
new_md5 = open(md5file).read()
old_version = ""
version_file = os.path.basename(file_name) + ".cpp.json.version"
if old_md5 != new_md5:
if not os.path.exists(version_file):
old_version = "1.0.0"
else:
old_version = open(version_file).read()
major, minor, patch = [int(i) for i in old_version.split(".")]
patch += 1
if patch + 1 > 10:
patch = 0
minor += 1
if minor > 10:
minor = 0
major += 1
open(version_file, "w").write(str(major)+"."+str(minor)+"."+str(patch))
# self.beforCreate(file_name, sname, add)
# gen proxy and stub files
cmd = subprocess.Popen([self.pyexec, "../../bin/cppgen.py", os.path.basename(file_name) + ".cpp.json"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ret = cmd.communicate()
if cmd.returncode != 0:
print("cppgen.py failed")
print(ret[1].decode("utf-8"))
return
# gen sdk files
cmd = subprocess.Popen([self.pyexec, "../../bin/cppgen_sdk.py", os.path.basename(file_name) + ".cpp.json"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ret = cmd.communicate()
if cmd.returncode != 0:
print("cppgen_sdk.py failed")
print(ret[1].decode("utf-8"))
return
# gen PB files
cmd = subprocess.Popen([self.pyexec, "../../bin/cppgen_pb_layer.py", os.path.basename(file_name) + ".cpp.json",
os.path.basename(file_name) + ".protobuf.json"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
ret = cmd.communicate()
if cmd.returncode != 0:
print("cppgen_pb_layer.py failed")
print(ret[1].decode("utf-8"))
return
(base_name, _) = os.path.splitext(os.path.basename(file_name))
json_str = json.load(open(base_name + ".idl.cpp.json"))
if not os.path.exists("../include/" + base_name + "/protobuf"):
os.makedirs("../include/" + base_name + "/protobuf")
if not os.path.exists("../" + base_name + "/protobuf"):
os.makedirs("../" + base_name + "/protobuf")
if not os.path.exists("../include/common"):
os.makedirs("../include/common")
if json_has_attr(json_str, "structs"):
shutil.copyfile(base_name + ".struct.h", "../include/common/" + base_name + ".struct.h")
orm_h_path = base_name + '.orm.h'
orm_cpp_path = base_name +'.orm.cpp'
if os.path.exists(orm_h_path):
shutil.copyfile(orm_h_path, "../include/common/" + orm_h_path)
shutil.copyfile(orm_cpp_path, "../" + base_name + "/" + orm_cpp_path)
for struct_name in json_str["structNames"]:
shutil.copyfile(struct_name + "_struct.h", "../include/common/" + struct_name + "_struct.h")
if json_has_attr(json_str, "enums"):
shutil.copyfile(base_name + ".enum.h", "../include/common/" + base_name + ".enum.h")
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("../include/" + base_name + "/" + service_name + "/proxy"):
os.makedirs("../include/" + base_name + "/" + service_name + "/proxy")
if not os.path.exists("../include/" + base_name + "/" + service_name + "/stub"):
os.makedirs("../include/" + base_name + "/" + service_name + "/stub")
if not os.path.exists("../proxy/" + base_name + "/" + service_name):
os.makedirs("../proxy/" + base_name + "/" + service_name)
if not os.path.exists("../stub/" + base_name + "/" + service_name):
os.makedirs("../stub/" + base_name + "/" + service_name)
if not os.path.exists(self.proj_root+"/usr/impl/" + base_name + "/" + service_name):
os.makedirs(self.proj_root+"/usr/impl/" + base_name + "/" + service_name)
shutil.copyfile(base_name + ".service." + service_name + ".h",
"../include/" + base_name + "/" + base_name + ".service." + service_name + ".h")
shutil.copyfile(base_name + '.service.' + service_name + ".proxy.h",
"../include/" + base_name + "/" + service_name + "/proxy/" + base_name + '.service.' + service_name + ".proxy.h")
shutil.copyfile(base_name + '.service.' + service_name + ".proxy.sdk.h",
"../include/" + base_name + "/" + service_name + "/proxy/" + base_name + '.service.' + service_name + ".proxy.sdk.h")
shutil.copyfile(base_name + '.service.' + service_name + ".proxy.serializer.h",
"../include/" + base_name + "/" + service_name + "/proxy/" + base_name + '.service.' + service_name + ".proxy.serializer.h")
shutil.copyfile(base_name + '.service.' + service_name + ".proxy.serializer.sdk.h",
"../include/" + base_name + "/" + service_name + "/proxy/" + base_name + '.service.' + service_name + ".proxy.serializer.sdk.h")
shutil.copyfile(base_name + '.service.' + service_name + ".proxy.cpp",
"../proxy/" + base_name + "/" + service_name + "/" + base_name + '.service.' + service_name + ".proxy.cpp")
shutil.copyfile(base_name + '.service.' + service_name + ".proxy.sdk.cpp",
"../proxy/" + base_name + "/" + service_name + "/" + base_name + '.service.' + service_name + ".proxy.sdk.cpp")
shutil.copyfile(base_name + '.service.' + service_name + ".proxy.serializer.cpp",
"../proxy/" + base_name + "/" + service_name + "/" + base_name + '.service.' + service_name + ".proxy.serializer.cpp")
shutil.copyfile(base_name + '.service.' + service_name + ".stub.h",
"../include/" + base_name + "/" + service_name + "/stub/" + base_name + '.service.' + service_name + ".stub.h")
shutil.copyfile(base_name + '.service.' + service_name + ".stub.serializer.h",
"../include/" + base_name + "/" + service_name + "/stub/" + base_name + '.service.' + service_name + ".stub.serializer.h")
shutil.copyfile(base_name + '.service.' + service_name + ".stub.cpp",
"../stub/" + base_name + "/" + service_name + "/" + base_name + '.service.' + service_name + ".stub.cpp")
shutil.copyfile(base_name + '.service.' + service_name + ".stub.serializer.cpp",
"../stub/" + base_name + "/" + service_name + "/" + base_name + '.service.' + service_name + ".stub.serializer.cpp")
shutil.copyfile(base_name + '.' + service_name + '.event.h',
"../include/" + base_name + "/" + service_name + "/stub/" + base_name + '.' + service_name + ".event.h")
if os.path.exists(base_name + '.' + service_name + ".unittest.cpp"):
shutil.copyfile(base_name + '.' + service_name + ".unittest.cpp",
"../../sdk_unittest/cpp/src/test/" + base_name + '.' + service_name + ".unittest.cpp")
if not os.path.exists(
self.proj_root+"/usr/impl/" + base_name + "/" + service_name + "/" + base_name + '.service.' + service_name + ".cpp"):
shutil.move(base_name + ".service." + service_name + ".impl.h",
self.proj_root+"/usr/impl/" + base_name + "/" + service_name + "/" + base_name + '.service.' + service_name + ".impl.h")
shutil.copyfile(base_name + '.service.' + service_name + ".cpp",
self.proj_root+"/usr/impl/" + base_name + "/" + service_name + "/" + base_name + '.service.' + service_name + ".cpp")
self.genImplCmake(base_name, service_name, self.proj_root+"/usr/impl/"+base_name+"/"+service_name)
if not os.path.exists(self.proj_root+"/usr/impl/" + base_name + "/orm"):
os.makedirs(self.proj_root+"/usr/impl/" + base_name + "/orm")
if os.path.exists('../'+base_name+'/'+base_name+'.orm.cpp'):
shutil.copyfile('../'+base_name+'/'+base_name+'.orm.cpp', self.proj_root+"/usr/impl/" + base_name + "/orm/"+base_name+'.orm.cpp')
if ("services" in json_str) and (json_str["services"] is not None):
for service in json_str["services"]:
if sname is not None:
if sname != service["name"]:
continue
if service["loadType"] == "dynamic":
service_name = service["name"]
shutil.copyfile(base_name + '.service.' + service_name + ".so.h",
"../include/" + base_name + "/" + service_name + "/stub/" + base_name + '.service.' + service_name + ".so.h")
shutil.copyfile(base_name + '.service.' + service_name + ".so.cpp",
"../stub/" + base_name + "/" + service_name + "/" + base_name + '.service.' + service_name + ".so.cpp")
if service['type'] == 'generic':
if service["notations"] is None:
continue
is_lua = False
for notation in service["notations"]:
if notation["script_type"] is not None:
for script_type in notation["script_type"]:
if script_type == "lua":
is_lua = True
if not is_lua:
continue
if not os.path.exists(self.proj_root+"/usr/impl/" + base_name + "/" + service['name']):
os.makedirs(self.proj_root+"/usr/impl/" + base_name + "/" + service['name'])
self.do_lua_template(base_name, service_name, service['name'])
if os.path.exists(base_name + '.service.pb.h'):
shutil.copyfile(base_name + '.service.pb.h',
"../include/" + base_name + "/protobuf/" + base_name + '.service.pb.h')
if os.path.exists(base_name + '.service.pb.cc'):
shutil.copyfile(base_name + '.service.pb.cc', "../" + base_name + "/protobuf/" + base_name + '.service.pb.cc')
remove_files(".", "*.h")
remove_files(".", "*.cpp")
remove_files(".", "*.cc")
#remove_files(".", "*.proto")
os.chdir("../../")
def updateRoot(self):
initializer = Initializer_cpp(Options(), self.get_version(), self.proj_root)
initializer.inspectpath()
os.chdir("tmp/rpc-frontend/")
os.system('git pull origin')
os.chdir("..")
initializer.build_frontend()
os.chdir("..")
os.chdir("tmp/rpc-backend-cpp/")
os.system('git pull --no-rebase "origin" ' + self.get_version())
os.chdir("..")
initializer.build_backend()
os.chdir("..")
initializer.installFramework()
os.chdir("../../")
def do_clean(self, file_name, service_name=None):
dst_path = os.path.join('.', 'tmp', file_name)
prx_path = None
stub_path = None
if service_name is not None:
prx_path = os.path.join(dst_path, 'proxy', service_name)
stub_path = os.path.join(dst_path, 'stub', service_name)
if prx_path is not None:
if os.path.exists(prx_path):
shutil.rmtree(prx_path)
if stub_path is not None:
if os.path.exists(stub_path):
shutil.rmtree(stub_path)
else:
if os.path.exists(dst_path):
shutil.rmtree(dst_path)
def build_apitest(self, out_dir, file_name, service_name=None):
os.chdir('./sdk_unittest')
if not os.path.exists('./cpp/tmp'):
os.makedirs('./cpp/tmp')
os.chdir('./cpp/tmp')
open('include.cmake', 'w').write('INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/../../src/include/'+file_name+')')
if platform.system() == "Windows":
os.system('cmake -DCMAKE_INSTALL_PREFIX='+self.proj_root+'/usr-bin/cpp -G "Visual Studio 16 2019" -A x64 ..')
os.system('cmake --build . --config "Debug"')
os.system('cmake --build . --config "Release"')
os.system('cmake --install . --config "Debug"')
os.system('cmake --install . --config "Release"')
else:
os.system('cmake -DCMAKE_INSTALL_PREFIX='+self.proj_root+'/usr-bin/cpp ..')
os.system('make install')
os.chdir("../../../")
if not os.path.exists(self.proj_root+'/usr-bin/cpp/sdk'):
os.makedirs(self.proj_root+'/usr-bin/cpp/sdk')
if not os.path.exists(self.proj_root+'/usr-bin/cpp/bin/'):
os.makedirs(self.proj_root+'/usr-bin/cpp/bin/')
shutil.copyfile("./framework/service-box-cpp-framework/bin/libsdk_frame.so", self.proj_root+'/usr-bin/cpp/bin/libsdk_frame.so')
# 拷贝SDK到./usr-bin/sdk目录下
for cur_service_name in os.listdir('./lib/proxy/'+file_name):
# 拷贝release版本
sdk_file_name = './lib/proxy/'+file_name +'/'+cur_service_name+ '/release/sdk'+cur_service_name+'_proxy.so'
if os.path.exists(sdk_file_name):
shutil.copyfile(sdk_file_name, self.proj_root+'/usr-bin/cpp/sdk/'+'sdk'+cur_service_name+'_proxy.so')
sdk_stub_name = './lib/stub/'+file_name +'/'+cur_service_name+ '/release/lib'+cur_service_name+'_module_sdk.so'
if os.path.exists(sdk_stub_name):
shutil.copyfile(sdk_stub_name, self.proj_root+'/usr-bin/cpp/sdk/'+'lib'+cur_service_name+'_module_sdk.so')
sdk_service_name = './lib/stub/'+file_name +'/'+cur_service_name+ '/release/lib'+cur_service_name+'_sdk.so'
if os.path.exists(sdk_service_name):
shutil.copyfile(sdk_service_name, self.proj_root+'/usr-bin/cpp/sdk/'+'lib'+cur_service_name+'_sdk.so')
# 生成配置文件
open(self.proj_root+'/usr-bin/cpp/bin/sdk.cfg', 'w').write('''
; SDK测试相关配置
sdk = {
;
; 日志
;
log_cfg = "file://.;file=api_test_%y%m%d.log;line=[%y%m%d-%H:%M:%S:%U];flush=true;mode=day;maxSize=10M"
;
; 远程容器IP,运行需要测试的服务
;
remote_host = "127.0.0.1"
;
; 远程容器端口,运行需要测试的服务
;
remote_port = 6888
;
; 连接到远程容器的超时时间,毫秒
;
conn_timeout = 2000
;
; SDK插件存放目录
;
plugin_dir = "../sdk"
}
''')
def run_apitest(self, file_name, service_name=None):
os.chdir(self.proj_root+"/usr-bin/cpp/bin")
if service_name is None:
os.system('sdk_frame_unittest')
else:
os.system('sdk_frame_unittest -f test_' + service_name)
def genImplCmake(self, base_name, service_name, target_path):
if os.path.exists(target_path+'/CMakeLists.txt'):
return True
file = open(target_path+"/CMakeLists.txt", "w")
tcmake = CMakeTemplate(open(self.root_path+'/template/cpp/usr_impl-CMakeLists.txt.template', 'r').read())
# usr/impl CMakeLists.txt
file.write(tcmake.substitute(REPO_BASE_DIR=self.root_path, IDL_NAME=base_name, SERVICE_NAME=service_name, PROJECT_BASE_DIR=self.proj_root))
return True
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/TaiZi0007/rpc-repo.git
[email protected]:TaiZi0007/rpc-repo.git
TaiZi0007
rpc-repo
rpc-repo
v0.3.0-alpha

搜索帮助