1 Star 0 Fork 11

太子0007/rpc-repo

forked from Plato/rpc-repo 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cpp_pub_driver.py 12.80 KB
一键复制 编辑 原始数据 按行查看 历史
dennis-kk 提交于 2022-05-17 09:06 +08:00 . FALSE FLAG
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import shutil
import json
import platform
import sys
import subprocess
from pub_driver import Publisher
from option import Options
DEFAULT_CONFIG_FILE = '''
;
; 容器开启的监听器,为一个数组,可以同时开启多个
; host={"127.0.0.1:6888","127.0.0.1:6900"}
;
listener = {
host = ("127.0.0.1:*")
}
;
; 服务发现配置
; 目前只支持zookeeper
;
service_finder = {
type = "zookeeper"
hosts = "127.0.0.1:2181"
}
;
; 容器socket缓冲区大小
;
box_channel_recv_buffer_len = `32*K`
;
; 容器名字
;
box_name = "service_box"
;
; 日志配置
;
`is_debug()` {
logger_config_line = "console://;line=[%y%m%d-%H:%M:%S:%U];flush=true"
}
`is_release()` {
logger_config_line = "file://.;file=box_%y%m%d.log;line=[%y%m%d-%H:%M:%S:%U];flush=true;mode=day;maxSize=10M"
}
;
; 连接到其他容器的超时时间,单位毫秒
;
connect_other_box_timeout = 2000
;
; 连接到服务发现服务的超时时间,单位毫秒
;
service_finder_connect_timeout = 2000
;
; 等待集群内其他服务必须注册后才能启动
;
necessary_service = ()
;
; 是否开启协程,开启协程后,服务方法将在协程内运行
;
open_coroutine = true
;
; 是否开启链路跟踪
;
open_trace = false
;
; 服务相关配置
;
service = {
;
; 本地服务所在目录
;
`os_name() == "windows"` {
`is_debug()` {
service_dir="./service/Debug"
}
`is_release()` {
service_dir="./service/Release"
}
}
`os_name() == "linux"` {
`is_debug()` {
service_dir="./service/debug"
}
`is_release()` {
service_dir="./service"
}
}
;
; 是否自动加载服务目录内所有服务
;
auto_load_service=true
;
; 加载在服务目录内的指定服务
;
preload_service=<>
;
; 远程版本文件API
;
remote_service_repo_version_api = "http://localhost/doc/version.json"
;
; 远程仓库地址
;
remote_service_repo_dir = "http://localhost/doc"
;
; 获取最新版本文件API
;
remote_service_repo_latest_version_api = ""
;
; 是否开启远程更新
;
is_open_remote_update = false
;
; 远程更新的检查周期,单位秒
;
remote_repo_check_interval = 60
;
; [LINUX ONLY] 是否以daemon方式启动
;
is_start_as_daemon = false
}
;
; 模块相关配置
;
module = {
;
; 模块存放的目录
;
`os_name() == "windows"` {
`is_debug()` {
module_dir="./module/Debug"
}
`is_release()` {
module_dir="./module/Release"
}
}
`os_name() == "linux"` {
module_dir="./module/"
}
}
;
; 控制台相关配置
;
command = {
enable = false
listen = "127.0.0.1:6889"
root_page_path = "root.html"
}
;
; 统计信息相关配置
;
statistics = {
is_open_rpc_stat = false
}
;
; 代理(网关)相关配置
;
proxy = {
query_timeout = 5000
call_timeout = 2000
;
; 种子,不同代理需要配置不同的种子,种子范围(0~255], 最多可以同时存在255个代理
;
seed = 1
;
; 代理对外启动的监听器数组
;
listener = ("127.0.0.1:9888")
}
;
; 调用链跟踪相关配置
;
call_tracer = {
;
; 目前只支持zipkin
;
type = "zipkin"
reporter = {
;
; zipkin服务地址
;
zipkin = "127.0.0.1:9411"
}
}
;
; 外部组件
;
component = {
; 存放外部组件的根目录,具体请参考前面的目录结构
root_dir = "component"
; 是否自动加载本地所有外部
auto_load = false
}
;
; lua服务目录
;
lua = {
root_dir = "./service/lua"
}
;
; DEBUG版本下会输出配置信息
;
`is_debug()` {
`print("=======================================")`
`print("| Show configuration in debug mode |")`
`print("=======================================")`
`print("Current working directory: ", cwd())`
`print("listener:", listener)`
`print("service_finder:", service_finder)`
`print("box_channel_recv_buffer_len=", box_channel_recv_buffer_len)`
`print("logger_config_line=", logger_config_line)`
`print("connect_other_box_timeout=", connect_other_box_timeout)`
`print("necessary_service=", necessary_service)`
`print("open_coroutine=", open_coroutine)`
`print("open_trace=", open_trace)`
`print("service:", service)`
`print("module:", module)`
`print("command:", command)`
`print("statistics:", statistics)`
`print("proxy:", proxy)`
`print("call_tracer:", call_tracer)`
}
'''
class Publisher_cpp(Publisher):
def init(self):
self.cwd = os.getcwd().replace('\\', '/')
def publish(self, idl_name, service_name, out_dir, orig_dir):
if not os.path.exists(out_dir):
# 输出目录不存在则建立
os.makedirs(out_dir)
if not os.path.exists(os.path.join('.', 'lib', 'proxy', idl_name)):
return
root_dir = os.path.join(os.path.join('.', 'lib', 'proxy', idl_name))
for cur_service_name in os.listdir(root_dir):
path = os.path.join(root_dir, cur_service_name)
if os.path.isdir(path):
if service_name != None:
if service_name == cur_service_name:
self.pub_service(idl_name, service_name, out_dir)
else:
self.pub_service(idl_name, cur_service_name, out_dir)
if platform.system() == "Windows":
shutil.copyfile(
os.path.join('framework', 'service-box-cpp-framework', 'bin', 'service_box_runtime.exe'),
os.path.join(out_dir, 'sbox.exe'))
shutil.copyfile(
os.path.join('framework', 'service-box-cpp-framework', 'bin', 'service_box_runtimed.exe'),
os.path.join(out_dir, 'sboxd.exe'))
else:
shutil.copyfile(
os.path.join('framework', 'service-box-cpp-framework', 'bin', 'service_box_runtime'),
os.path.join(out_dir, 'sbox'))
os.system('chmod +x ' + os.path.join(out_dir, 'sbox'))
shutil.copyfile(
os.path.join('framework', 'service-box-cpp-framework', 'bin', 'service_box_runtimed'),
os.path.join(out_dir, 'sboxd'))
os.system('chmod +x ' + os.path.join(out_dir, 'sboxd'))
self.create_config_file(out_dir)
com_dir = os.path.join(out_dir, 'component')
if not os.path.exists(com_dir):
os.makedirs(com_dir)
proxy_config_dir = os.path.join(out_dir, "proxy_config")
if not os.path.exists(proxy_config_dir):
os.makedirs(proxy_config_dir)
proxy_config_from_dir = os.path.join('.', 'src', 'idl')
json_file_name = idl_name+'.idl.protobuf.json'
proto_file_name = idl_name+'.service.proto'
json_file_path = os.path.join(proxy_config_from_dir, json_file_name)
proto_file_path = os.path.join(proxy_config_from_dir, proto_file_name)
if os.path.exists(json_file_path):
shutil.copyfile(json_file_path, os.path.join(proxy_config_dir, json_file_name))
if os.path.exists(proto_file_path):
shutil.copyfile(proto_file_path, os.path.join(proxy_config_dir, proto_file_name))
#self.pub_sdk_include(out_dir)
def check(self):
return True
def pub_sdk_include(self, out_dir):
from_dir = os.path.join('.', 'src', 'include')
to_dir = os.path.join(out_dir, 'sdk', 'include')
if os.path.exists(os.path.join(to_dir, 'common')):
shutil.rmtree(os.path.join(to_dir, 'common'))
shutil.copytree(os.path.join(from_dir, 'common'), os.path.join(to_dir, 'common'))
if not os.path.exists(os.path.join(to_dir, 'root')):
os.makedirs(os.path.join(to_dir, 'root'))
for file_name in os.listdir(os.path.join(from_dir, 'root')):
if file_name.endswith('_interface.h') and not file_name.startswith('rpc_stub_'):
shutil.copyfile(os.path.join(from_dir, 'root', file_name), os.path.join(os.path.join(to_dir, 'root', file_name)))
from_sdk_include_dir = os.path.join('.', 'framework', 'service-box-cpp-framework', 'include', 'sdk')
from_sdk_so_dir = os.path.join('.', 'framework', 'service-box-cpp-framework', 'bin')
to_sdk_so_dir = os.path.join(out_dir, 'sdk')
shutil.copyfile(os.path.join(from_sdk_include_dir, 'box_sdk.hh'), os.path.join(to_dir, 'box_sdk.hh'))
shutil.copyfile(os.path.join(from_sdk_include_dir, 'sdk_frame.hh'), os.path.join(to_dir, 'sdk_frame.hh'))
shutil.copyfile(os.path.join(from_sdk_so_dir, 'libsdk_frame.so'), os.path.join(to_sdk_so_dir, 'release', 'libsdk_frame.so'))
shutil.copyfile(os.path.join(from_sdk_so_dir, 'libsdk_framed.so'), os.path.join(to_sdk_so_dir, 'debug', 'libsdk_framed.so'))
def pub_service_sdk(self, idl_name, service_name, out_dir):
from_dir = os.path.join('.', 'src', 'include', idl_name, service_name, 'proxy')
to_dir = os.path.join(out_dir, 'sdk', 'include', idl_name, service_name, 'proxy')
file_name = idl_name + '.service.' + service_name + '.proxy.sdk.h'
if not os.path.exists(from_dir):
return
if not os.path.exists(to_dir):
os.makedirs(to_dir)
shutil.copyfile(os.path.join(from_dir, file_name), os.path.join(to_dir, file_name))
def pub_service(self, idl_name, service_name, out_dir):
proxy_out_dir = os.path.join(out_dir, 'sdk')
module_out_dir = os.path.join(out_dir, 'module')
service_out_dir = os.path.join(out_dir, "service")
if not os.path.exists(proxy_out_dir):
os.makedirs(proxy_out_dir)
if not os.path.exists(module_out_dir):
os.makedirs(module_out_dir)
if not os.path.exists(service_out_dir):
os.makedirs(service_out_dir)
proxy_dir = os.path.join('.', 'lib', 'proxy',idl_name, service_name)
stub_dir = os.path.join('.', 'lib', 'stub',idl_name, service_name)
if platform.system() == "Windows":
self.copy_file_windows(proxy_dir, proxy_out_dir, 'sdk', '_proxy.so', service_name)
self.copy_file_windows(proxy_dir, module_out_dir, 'lib', '_proxy_module.so', service_name)
self.copy_file_windows(stub_dir, module_out_dir, 'lib', '_module.so', service_name)
self.copy_file_windows(stub_dir, service_out_dir, 'lib', '.so', service_name)
self.copy_file_windows(stub_dir, proxy_out_dir, 'lib', '_module.so', service_name)
self.copy_file_windows(stub_dir, proxy_out_dir, 'lib', '_sdk.so', service_name)
else:
self.copy_file_linux(proxy_dir, proxy_out_dir, 'sdk', '_proxy.so', service_name)
self.copy_file_linux(proxy_dir, module_out_dir, 'lib', '_proxy_module.so', service_name)
self.copy_file_linux(stub_dir, module_out_dir, 'lib', '_module.so', service_name)
self.copy_file_linux(stub_dir, service_out_dir, 'lib', '.so', service_name)
self.copy_file_linux(stub_dir, proxy_out_dir, 'lib', '_module.so', service_name)
self.copy_file_linux(stub_dir, proxy_out_dir, 'lib', '_sdk.so', service_name)
#self.pub_service_sdk(idl_name, service_name, out_dir)
def copy_file_windows(self, from_dir, to_dir, prefix, suffix, service_name):
file_name = prefix+service_name+suffix
file_debug_path = os.path.join(from_dir, 'debug', file_name)
file_release_path = os.path.join(from_dir, 'release', file_name)
out_debug_dir = os.path.join(to_dir, 'debug', file_name)
out_release_dir = os.path.join(to_dir, 'release', file_name)
if not os.path.exists(os.path.join(to_dir, 'debug')):
os.makedirs(os.path.join(to_dir, 'debug'))
if not os.path.exists(os.path.join(to_dir, 'release')):
os.makedirs(os.path.join(to_dir, 'release'))
if os.path.exists(file_debug_path):
shutil.copyfile(file_debug_path, out_debug_dir)
if os.path.exists(file_release_path):
shutil.copyfile(file_release_path, out_release_dir)
def copy_file_linux(self, from_dir, to_dir, prefix, suffix, service_name):
file_name = prefix+service_name+suffix
file_path = os.path.join(from_dir, file_name)
out_dir = os.path.join(to_dir, file_name)
if os.path.exists(file_path):
shutil.copyfile(file_path, out_dir)
debug_file_name = prefix+service_name+'d'+suffix
debug_file_path = os.path.join(from_dir, debug_file_name)
debug_out_dir = os.path.join(to_dir, 'debug', debug_file_name)
if not os.path.exists(os.path.join(to_dir, 'debug')):
os.makedirs(os.path.join(to_dir, 'debug'))
if os.path.exists(debug_file_path):
shutil.copyfile(debug_file_path, debug_out_dir)
def create_config_file(self, to_dir):
open(os.path.join(to_dir, "config.default.cfg"), "w").write(DEFAULT_CONFIG_FILE)
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

搜索帮助