2025-03-26 00:02:56 +08:00

323 lines
9.3 KiB
Python

from email.policy import strict
from inspect import getcomments
import os
from re import S
import sys
import json
import urllib3
DirPath = os.path.split(os.path.realpath(__file__))
# DirPath = "E:\\Jenkins3Agent\\workspace\\qiuqiuTest"
# 白名单
whiteList = ['svnUpdate.txt','status.txt','gitPull.txt']
# DIR = sys.argv[1] # 方向
GitPath = sys.argv[1] # git地址
GITBRANCH = sys.argv[2] # git分支
SVN_USER = sys.argv[3] # svn用户
SVN_PASSWORD = sys.argv[4] # svn密码
GIT_USER = sys.argv[5] # git用户
GIT_PASSWORD = sys.argv[6] #git密码
GIT_PASSWORD_TEMP = sys.argv[7] #git临时密码
GIT_USER_TEMP = sys.argv[8] #git临时用户
# svn命令行
SVNRevertCommand = "svn revert -R *"
SVNUpdateCommand = "svn update >.\svnUpdate.txt"
SVNStatusCommand = "svn status > .\status.txt"
SVNDeleteCommand = "svn del "
SVNAddOneCommand = "svn add "
SVNCommitCommand = 'svn commit --username ' + SVN_USER + ' --password ' + SVN_PASSWORD + ' -m "git提交"'
# git命令行
addstr = "."
GitCheckoutCommand = "git checkout ."
GitPullCommand = "git pull > .\gitPull.txt"
GitAddCommand = "git add " + addstr
GitCommitCommand = 'git commit -m "' + "svn提交" + '"'
GitPushCommand = r"git push " + GIT_USER_TEMP + ":" + GIT_PASSWORD_TEMP + "@" + GitPath + " " + GITBRANCH
# 前缀函数
def NormalFunc():
# 路径跳转
# os.chdir(listPath[0])
os.chdir(DirPath[0]) #跳转
# testarg = open("./testArgs.txt",'w')
# for i in range(0,len(sys.argv)):
# testarg.write(sys.argv[i] + '\n')
# testarg.close()
# svn发到git
def SvnToGit():
os.system(SVNRevertCommand)
os.system(SVNUpdateCommand)
# os.system(GitCheckoutCommand)
os.system(GitPullCommand)
# os.system(SVNRevertCommand)
# os.system(SVNStatusCommand)
# statusLog = open("./status.txt")
# for line in statusLog.readlines():
# if line.strip()[0] == "?": #新增的文件: (实际上在svn已经删除的)
# os.remove(DirPath + "\\" + line.strip()[8:len(line.strip())])
# statusLog.close()
# os.system(GitAddCommand)
# os.system(GitCommitCommand)
# os.system(GitPushCommand)
# git发到svn
def GitToSVN():
os.system(SVNRevertCommand)
os.system(SVNUpdateCommand)
# os.system(GitCheckoutCommand)
os.system(GitPullCommand)
# os.system(SVNStatusCommand)
# statusLog = open("./status.txt")
# for line in statusLog.readlines():
# if line.strip()[0] == "!": #被删除的文件
# os.system(SVNDeleteCommand + '"' + ".\\" + line.strip()[8:len(line.strip())] + '"')
# elif line.strip()[0] == "?": #新增的文件:
# os.system(SVNAddOneCommand + '"' + ".\\" + line.strip()[8:len(line.strip())] + '"')
# os.system(SVNCommitCommand)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36', 'Accept-Charset': 'utf-8', 'Content-Type': 'application/json'}
webhook = "https://open.feishu.cn/open-apis/bot/v2/hook/eb0834e0-4d2d-4d2c-a20c-90ce9079566e"
href = ""
# git pull失败
def notify():
content = {
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "git和svn合并通知",
"content":
[
[
{
"tag": "text",
"text": "git拉取失败,有修改了相同文件: "
},
{
"tag": "at",
"user_id": "7109285249619836931"
}
]
]
}
}
}
}
http = urllib3.PoolManager(retries=3, timeout=10)
resp = http.request(method="POST", url=webhook,
headers=headers, body=json.dumps(content))
# print(resp.data)
# svn要删除文件,但是git修改了这个文件
def AlreadySVNDel(message,allMessage):
content = {
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "git和svn合并通知",
"content":
[
[
{
"tag": "text",
"text": "svn要删除文件,但是git修改了这个文件:\n "
},
{
"tag": "text",
"text": f"{message}"
},
{
"tag": "text",
"text": "\n所有svn删除的文件列表为:\n "
},
{
"tag": "text",
"text": f"{allMessage}"
},
{
"tag": "at",
"user_id": "7109285249619836931"
}
]
]
}
}
}
}
http = urllib3.PoolManager(retries=3, timeout=10)
resp = http.request(method="POST", url=webhook,
headers=headers, body=json.dumps(content))
# print(resp.data)
# svn和git重命名了同一个文件
def Rename(message):
content = {
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "git和svn合并通知",
"content":
[
[
{
"tag": "text",
"text": "svn重命名了这个文件,git也重命名了这个文件,会产生多余的文件:\n "
},
{
"tag": "text",
"text": f"{message}"
},
{
"tag": "at",
"user_id": "7109285249619836931"
}
]
]
}
}
}
}
http = urllib3.PoolManager(retries=3, timeout=10)
resp = http.request(method="POST", url=webhook,
headers=headers, body=json.dumps(content))
# print(resp.data)
def DelOther():
os.remove(DirPath[0] + "\\" + "gitPull.txt")
os.remove(DirPath[0] + "\\" + "status.txt")
os.remove(DirPath[0] + "\\" + "svnUpdate.txt")
# 双向发送
def Both():
# os.system(GitCheckoutCommand)
os.system(SVNRevertCommand)
os.system(SVNUpdateCommand)
os.system(GitPullCommand)
os.system(SVNStatusCommand)
svndelList = ""
svnDelTrueList = []
svnLog = open("./svnUpdate.txt",'r')
for svnLine in svnLog.readlines():
if svnLine.strip()[0] == "D": #被删除的文件
svndelList = svndelList + svnLine + "\n"
splitList = svnLine.split("\\")
svnDelTrueList.append(splitList[len(splitList)-1])
svnLog.close()
gitLog = open("./gitPull.txt",'r')
gitNormal = False
isRename = False
for gitLine in gitLog.readlines():
if len(gitLine.split('|')) > 1:
gitNormal = True
if(gitLine.find("rename",0,len(gitLine)-1) != -1):
for i in range(0,len(svnDelTrueList)):
cleanLine = gitLine.replace("\"",'')
if(cleanLine.strip().find(str(svnDelTrueList[i]).strip(),0,len(cleanLine)-1) != -1):
isRename = True
Rename(svnDelTrueList[i])
if(gitLine.find("Already",0,len(gitLine)-1) != -1):
gitNormal = True
break
gitLog.close()
if(isRename == True):
print("git something wrong!!!!!!!!!!!")
return
if(gitNormal == False):
print("git something wrong!!!!!!!!!!!")
notify()
return
else:
print("git normal ula!!!!!!!!!!!!!!!!!!!!")
statusLog = open("./status.txt")
for line in statusLog.readlines():
if whiteList.count(line.strip()[8:len(line.strip())]) != 0 :
continue
if line.strip()[0] == "!": #被删除的文件
os.system(SVNDeleteCommand + '"' + ".\\" + line.strip()[8:len(line.strip())] + '"')
elif line.strip()[0] == "?": #新增的文件:
if(svndelList.find(line.strip()[8:len(line.strip())],0,len(svndelList)-1) != -1):
AlreadySVNDel(line.strip()[8:len(line.strip())],svndelList)
print("git something wrong!!!!!!!!!!!")
return
else:
os.system(SVNAddOneCommand + '"' + ".\\" + line.strip()[8:len(line.strip())] + '"')
statusLog.close()
DelOther()
os.system(SVNCommitCommand)
os.system(GitAddCommand)
os.system(GitCommitCommand)
os.system(GitPushCommand)
direction = {"svn发给git":SvnToGit,"git发给svn":GitToSVN,"双向更新":Both}
NormalFunc()
# direction.get(DIR,Both)()
Both()