4 Star 2 Fork 1

Plato / plato

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
plato.py 11.91 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import util
import json
import os
import sys
import subprocess
import shutil
import platform
class Plato:
def __init__(self, argv):
self.__checkEnv()
self.__installCommand()
self.cur_repo = util.getCurrentRepo()
self.settings = {}
self.execpy = sys.executable
self.BINPATH = os.path.abspath(os.path.dirname(__file__)).replace('\\', '/')
self.__parseSettings()
if self.settings is not None:
if 'cur_repo' in self.settings:
self.cur_repo = self.settings['cur_repo']
self.__doCommand(argv)
def __doCommand(self, argv):
command = argv[0]
if command not in self.cmd_table:
print('Command '+command + " not found")
self.__help(None)
exit(-1)
self.cmd_table[command](argv[1:])
def __parseSettings(self):
setting_path = os.path.join(self.BINPATH, '.settings', 'repo-settings.json')
if not os.path.exists(setting_path):
return
json_str = open(setting_path, 'r', encoding='utf-8').read()
if len(json_str) == 0:
os.remove(json_str)
return
self.settings = json.loads(open(setting_path, 'r', encoding='utf-8').read())
def __pull(self, argv):
branch = ""
if len(argv) != 0:
branch = argv[0]
else:
print('Need a branch name')
exit(-1)
tags = None
if len(argv) >= 2:
tags = argv[1]
repo_root_path = os.path.join(self.BINPATH, 'repo', branch)
if not os.path.exists(repo_root_path):
os.makedirs(repo_root_path)
else:
# 查询文件是否为空
if os.path.exists(repo_root_path+'/rpc-repo'):
print(os.path.getsize(repo_root_path+'/rpc-repo'))
if os.path.getsize(repo_root_path+'/rpc-repo') > 0:
print('Branch already pulled')
exit(-1)
os.chdir(repo_root_path)
repo_url = 'https://gitee.com/dennis-kk/rpc-repo.git'
if os.path.exists('%s/rpc-repo'%(repo_root_path)):
os.chdir('rpc-repo')
util.fetch_repo_tags(repo_url, branch, tags)
os.chdir("..")
else:
if util.clone_repo_tags(repo_url, branch, tags) == False:
print("查询分支错误")
exit(-1)
repo_path = os.path.join(repo_root_path, 'rpc-repo')
self.settings['cur_repo'] = repo_path
if 'repo_settings' not in self.settings:
self.settings['repo_settings'] = []
inster = True
for setting in self.settings['repo_settings']:
if setting['repo_root'] == repo_path:
inster = False
if inster:
self.settings['repo_settings'].append({'repo_root':repo_path, 'version':branch})
setting_path = os.path.join(self.BINPATH, '.settings', 'repo-settings.json')
if not os.path.exists(os.path.join(self.BINPATH, '.settings')):
os.makedirs(os.path.join(self.BINPATH, '.settings'))
open(setting_path, 'w', encoding='utf-8').write(json.dumps(self.settings))
self.cur_repo = repo_path
def __remove(self, argv):
branch = ""
if len(argv) != 0:
branch = argv[0]
else:
print('Need a branch name')
exit(-1)
repo_root_path = os.path.join(self.BINPATH, 'repo', branch)
if not os.path.exists(repo_root_path):
print("Branch not found")
exit(-1)
repo_path = os.path.join(repo_root_path, 'rpc-repo')
try:
shutil.rmtree(repo_root_path)
except:
print('remove '+branch+' failed')
exit(-1)
index = 0
for setting in self.settings["repo_settings"]:
if setting['repo_root'] == repo_path:
del self.settings["repo_settings"][index]
break
index += 1
if self.settings['cur_repo'] == repo_path:
if len(self.settings["repo_settings"]) > 0:
self.settings['cur_repo'] = self.settings["repo_settings"][0]['repo_root']
else:
self.settings['cur_repo'] = ""
setting_path = os.path.join(self.BINPATH, '.settings', 'repo-settings.json')
open(setting_path, 'w', encoding='utf-8').write(json.dumps(self.settings))
self.cur_repo = repo_path
def __switch(self, argv):
if len(argv) == 0:
print('Need a branch')
exit(-1)
util.switchRepo(os.path.join(self.BINPATH, 'repo', argv[0], 'rpc-repo'))
def __current_repo(self, argv):
cur_repo = util.getCurrentRepo()
if cur_repo is None:
print("No repo")
exit(-1)
split_str = cur_repo.split('/')
branch = split_str[len(split_str)-2]
print(branch)
def __help(self, argv):
print(' ___ __ _ _____ ___ ')
print(' / _ \\/ / /_\\ /__ \\/___\\')
print(' / /_)/ / //_\\\ / /\\// //')
print(' / ___/ /___/ _ \\/ / / \\_// ')
print(' \/ \\____/\\_/ \\_/\\/ \\___/ ')
print('')
print(" Plato " + open(os.path.join(self.BINPATH, "VERSION")).read())
print("")
print(" plato pull [branch] [tags] Pull repository as given branch and tags")
print(" plato switch [branch] Switch repository as given branch")
print(" plato remove [branch] Remove branch")
print(" plato current Show current branch")
print(" plato upgrade Upgrade manage tool of repository")
print(" plato reinstall update and re-install plato command, be careful, it will delete all repo of plato")
print(" plato repo Show all repo")
print(" plato help Help Doc.")
print(" plato new Create a workspace")
print(" plato build Build workspace")
print(" plato publish Publish workspace excutable")
def __installCommand(self):
self.cmd_table = {}
self.cmd_table['pull'] = self.__pull
self.cmd_table['switch'] = self.__switch
self.cmd_table['remove'] = self.__remove
self.cmd_table['current'] = self.__current_repo
self.cmd_table['repo'] = self.__showRepo
self.cmd_table['help'] = self.__help
self.cmd_table['upgrade'] = self.__upgrade
self.cmd_table['reinstall'] = self.__reinstall
self.cmd_table['new'] = self.__new
self.cmd_table['build'] = self.__build
self.cmd_table['publish'] = self.__publish
def __new(self, argv):
if os.path.exists("plato-workspace.json"):
print("Cannot create workspace in a workspace")
return
work_json = {
"workspace" : {
"all-project" : []
}
}
open('plato-workspace.json', "w").write(json.dumps(work_json, indent=4))
def __build(self, argv):
if not os.path.exists("plato-workspace.json"):
print("Not a plato workspace")
return
work_json = json.loads(open("plato-workspace.json").read())
if "workspace" in work_json:
if "all-project" in work_json["workspace"]:
self._process_project_build(work_json)
def __publish(self, argv):
if not os.path.exists("plato-workspace.json"):
print("Not a plato workspace")
return
work_json = json.loads(open("plato-workspace.json").read())
if "workspace" in work_json:
if "all-project" in work_json["workspace"]:
self._process_project_publish(work_json)
def _process_project_build(self, work_json):
for proj in work_json["workspace"]["all-project"]:
if not os.path.exists(proj):
print(proj+' not found')
return
proj_json = json.loads(open(proj).read())
if not "project" in proj_json:
print("No project configure")
return
if not "build" in proj_json["project"]:
print("No build configure")
return
if not "cmd" in proj_json["project"]["build"]:
print("No build command")
return
os.system(proj_json["project"]["build"]["cmd"])
def _process_project_publish(self, work_json):
for proj in work_json["workspace"]["all-project"]:
if not os.path.exists(proj):
print(proj+' not found')
return
proj_json = json.loads(open(proj).read())
if not "project" in proj_json:
print("No project configure")
return
if not "publish" in proj_json["project"]:
print("No publish configure")
return
if not "cmd" in proj_json["project"]["publish"]:
print("No publish command")
return
os.system(proj_json["project"]["publish"]["cmd"])
if "install" in proj_json["project"]["publish"]:
for install_cmd in proj_json["project"]["publish"]["install"]:
os.system(install_cmd)
def __upgrade(self, argv):
tag = None
if len(argv) > 0:
tag = argv[0]
if tag is None:
os.system(self.execpy+" "+os.path.join(self.cur_repo, "repo.py") + " --upgrade")
else:
os.system(self.execpy+" "+os.path.join(self.cur_repo, "repo.py") + ' --option={0} --upgrade'.format(tag))
def __showRepo(self, argv):
_dirs = []
list = os.listdir(os.path.join(self.BINPATH, "repo"))
for i in range(0, len(list)):
print(list[i])
if len(list) == 0:
print('No repo')
def __checkEnv(self):
self.__check_git_env()
self.__check_cmake_evn()
def __check_git_env(self):
error = os.system("git --version")
if error != 0:
print("git not found")
exit(-1)
return True
def __check_cmake_evn(self):
error = os.system("cmake --version")
if error != 0:
print("cmake not found")
exit(-1)
else:
out_bytes = subprocess.check_output(['cmake', '--version'])
out_text = out_bytes.decode('utf-8')
strs = out_text.split(" ")
if int(strs[2].split(".")[0]) < 3:
print("Need cmake version >= 3")
exit(-1)
else:
if int(strs[2].split(".")[1]) < 5:
print("Need cmake version >= 3.5")
exit(-1)
def __reinstall(self, argv):
# todo 只更新到最新的tag
if platform.system() == "Windows":
print("not support windows platform")
return False
# 缓存 setting
setting_path = os.path.join(self.BINPATH, '.settings', 'repo-settings.json')
json_str = ""
if os.path.exists(setting_path):
json_str = open(setting_path, 'r', encoding='utf-8').read()
# 切换到plato的目录
os.chdir(self.BINPATH)
os.system("git clean -fd")
os.system("git checkout .")
error = os.system("git pull")
if error != 0:
print("update plato failed!")
exit(-1)
os.system("bash install.sh")
# 重建repo 记录
if len(json_str) > 0:
os.mkdir(os.path.join(self.BINPATH, '.settings'))
open(setting_path, 'w', encoding='utf-8').write(json_str)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("type 'plato help' for more information")
exit(-1)
Plato(sys.argv[1:])
Python
1
https://gitee.com/dennis-kk/plato.git
git@gitee.com:dennis-kk/plato.git
dennis-kk
plato
plato
v0.4.0-alpha

搜索帮助