4 Star 2 Fork 1

Plato / plato

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
util.py 6.22 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import subprocess
import platform
import os
import sys
import json
import shutil
import re
def get_pyexec():
print(sys.executable)
return sys.executable
def getCurrentRepo():
BINPATH = os.path.abspath(os.path.dirname(__file__)).replace('\\', '/')
setting_path = os.path.join(BINPATH, '.settings', 'repo-settings.json')
if not os.path.exists(setting_path):
return None
json_str = open(setting_path, 'r', encoding='utf-8').read()
if len(json_str) == 0:
print("repo-settings.json corruption")
exit(-1)
settings = json.loads(open(setting_path, 'r', encoding='utf-8').read())
if len(settings['cur_repo']) == 0:
return None
return settings['cur_repo'].replace('\\', '/')
def do_command(type):
cur_repo = getCurrentRepo()
if cur_repo is None:
print("No repo")
exit(-1)
REPO_ROOT = cur_repo.replace("\\", "/")
PYEXEC = get_pyexec()
#cmd = PYEXEC +" "+REPO_ROOT+"/repo.py -t "+type+" "
arg_list = [PYEXEC, REPO_ROOT+"/repo.py", "-t", type]
arg_list.extend(sys.argv[1:])
cmd = subprocess.Popen(arg_list, encoding='utf-8')
ret = cmd.communicate()
if cmd.returncode != 0:
print(type, " failed")
print("err message:", [ line.decode("utf-8") for line in ret if line is not None])
return
#os.system(cmd + ' '.join(sys.argv[1:]))
def switchRepo(repo_path):
print(repo_path)
BINPATH = os.path.abspath(os.path.dirname(__file__)).replace('\\', '/')
setting_path = os.path.join(BINPATH, '.settings', 'repo-settings.json')
if not os.path.exists(setting_path):
raise Exception("setting path not exists !")
json_str = open(setting_path, 'r', encoding='utf-8').read()
if len(json_str) == 0:
print("repo-settings.json corruption")
exit(-1)
settings = json.loads(json_str)
settings['cur_repo'] = repo_path
open(setting_path, 'w', encoding='utf-8').write(json.dumps(settings))
# 正则对应处理对应import
branch_re = re.compile(r'v([0-9].[0-9]).*')
def list_repo_tags(repo_url, branch):
bres = branch_re.match(branch)
if bres == None:
raise Exception('分支格式错误,期望格式 vx.x.x-*, 输入{0}'.format(branch))
re_rule= r'refs/tags/v({0}.[0-9]*)'.format(bres.groups()[0])
tags_re = re.compile(re_rule)
check_cmd = 'git ls-remote --tags --refs {0}'.format(repo_url)
try:
res_msg=subprocess.check_output(check_cmd, shell=True)
tags_lists = res_msg.decode('utf-8').split("\n")
re_list = []
for tl in tags_lists:
if tl != "":
tm = tags_re.match(tl.split('\t')[1])
if tm != None:
ver = tm.groups()[0]
re_list.append(("v"+ver,ver))
return re_list
except subprocess.CalledProcessError as call_e:
#url 错误或者git 错误等错误需要处理
print(call_e.output.decode(encoding="utf-8"))
return None
except:
print('unexpected error with check_cmd:', check_cmd, sys.exc_info()[0])
return None
clone_cmd='git clone -b {0} {1} --depth=1'
clone_with_tag = 'git clone --depth 1 --branch {0} {1}'
def version_tuple(ver):
return tuple(map(int, (ver.split("."))))
def clone_repo_tags(repo_url, branch, tags=None):
# 分支格式错误会报错
latest="0.0.0"
# 如果 tags 是 latest 则 不要拉分支
# 启用 tags 机制,如果填写tag默认为latest, 当tag 为v0.0.0 时才会拉最新分支
tags = "latest" if not tags else tags
if tags != "v0.0.0":
re_lists = list_repo_tags(repo_url, branch)
# 如果没有指定tag 拉最新的
if re_lists is None:
raise Exception('仓库: {0} 分支: {1} 不存在tags'.format(repo_url, branch))
if tags == "latest":
for rl in re_lists:
if version_tuple(latest) < version_tuple(rl[1]):
latest = rl[1]
else:
for rl in re_lists:
print (rl[0], tags)
if rl[0] == tags:
latest = tags
break
if latest == "0.0.0":
raise Exception('repo 仓库分支 {0} {1} 无指定tag'.format(branch, tags))
#恢复分支所需格式
latest = "v"+latest
git_cmd = ""
if latest == "v0.0.0":
git_cmd = clone_cmd.format(branch, repo_url)
print("仓库 ", repo_url, " 没有发布tag, 拉取最新提交")
else:
git_cmd = clone_with_tag.format(latest, repo_url)
print("仓库", repo_url, " 更新到tag ", latest)
if os.system(git_cmd) != 0:
print("初始话仓库失败", git_cmd)
return False
return True
update_cmd='git fetch origin {0}'
update_tag_cmd = 'git fetch origin tag {0}'
def fetch_repo_tags(repo_url, branch, tags=None):
# 分支格式错误会报错
latest="0.0.0"
# 如果 tags 是 latest 则 不要拉分支
# 启用 tags 机制,如果填写tag默认为latest, 当tag 为v0.0.0 时才会拉最新分支
tags = "latest" if not tags else tags
if tags != "v0.0.0":
re_lists = list_repo_tags(repo_url, branch)
# 如果没有指定tag 拉最新的
if re_lists is None:
raise Exception('仓库: {0} 分支: {1} 不存在tags'.format(repo_url, branch))
if tags == "latest":
for rl in re_lists:
if version_tuple(latest) < version_tuple(rl[1]):
latest = rl[1]
else:
for rl in re_lists:
print (rl[0], tags)
if rl[0] == tags:
latest = tags
break
if latest == "0.0.0":
raise Exception('repo 仓库分支 {0} {1} 无指定tag'.format(branch, tags))
latest = "v"+latest
git_cmd = ""
if latest == "v0.0.0":
git_cmd = update_cmd.format(branch)
print("仓库 ", repo_url, "不指定发布tag, 拉取最新提交")
else:
git_cmd = update_tag_cmd.format(latest)
print("仓库", repo_url, " 更新到tag ", latest)
os.system('git checkout .')
os.system(git_cmd)
os.system('git reset --hard FETCH_HEAD')
return True
Python
1
https://gitee.com/dennis-kk/plato.git
git@gitee.com:dennis-kk/plato.git
dennis-kk
plato
plato
v0.4.0-alpha

搜索帮助