1 Star 0 Fork 5

BlackHawk / gogame

forked from 暖阳 / gogame 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pb2go.py 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/python
# coding:utf-8
import os
import shutil
import time
_root = os.path.abspath(os.path.dirname(__file__)).replace('\\', '/')
# 打包用
# _root = os.path.dirname(os.path.realpath(sys.executable))
_basePath = _root + "/pb/proto"
# 需要转换的语言
languageList = [
"go",
# "python"
]
# 生成python.go存放目录
if "python" in languageList:
_chkFolder = _root + "/pb/pb_python"
if os.path.exists(_chkFolder):
shutil.rmtree(_chkFolder)
os.mkdir(_chkFolder)
with open(f"{_chkFolder}/__init__.py", "w") as f:
f.write(" ")
# 命令执行
def command(base_path, go_out_path, folder, file):
_dir = f"{base_path}/{folder}"
for language in languageList:
_command = None
if language == "go":
_importPath = f"{base_path}/{folder}/{file}" if folder else f"{base_path}/{file}"
_command = f"protoc --proto_path={base_path} --go_out={go_out_path} --go_opt=paths=import {_importPath}"
# print(_command)
elif language == "python":
# 生成init文件变为可导入
_chkFolder = f"{go_out_path}/pb_python/{folder}"
if os.path.exists(_chkFolder):
shutil.rmtree(_chkFolder)
os.mkdir(_chkFolder)
with open(f"{_chkFolder}/__init__.py", "w") as f:
f.write(" ")
_command = f"protoc_python --proto_path={base_path} --{language}_out={go_out_path}/pb_python {base_path}/{folder}/*.proto"
if _command:
os.system(_command)
print(f"{folder}{file}】转换【{language}】成功")
# 是否是pb文件
def isPBFile(path, file):
if ".proto" not in file:
return False
_filePath = f"{path}/{file}"
# 文件不能为空
if not os.path.getsize(_filePath):
print(f"{_filePath}不能为空!!!!!!!!!!!!!!!!!!")
return False
return True
def pb2go():
_insertList = []
_folderList = os.listdir(_basePath)
_goOutPath = _root + "/pb"
for folder in _folderList:
_toPath = _basePath + "/" + folder
# 是文件夹
if os.path.isdir(_toPath):
_fileList = os.listdir(_toPath)
# 当前目录下没有文件
if not _fileList:
continue
for file in _fileList:
# 不是pb文件
if not isPBFile(_toPath, file):
continue
command(_basePath, _goOutPath, folder, file)
# pb文件
else:
if isPBFile(_basePath, folder):
command(_basePath, _goOutPath, "", folder)
# time.sleep(5)
if __name__ == '__main__':
pb2go()
Go
1
https://gitee.com/hyhhui/gogame.git
git@gitee.com:hyhhui/gogame.git
hyhhui
gogame
gogame
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891