背景
&snap;&snap;公司neuxs倉(cāng)庫(kù)中maven-metadata 文件經(jīng)常被刪掉,導(dǎo)致開(kāi)發(fā)中拉取不到依賴問(wèn)題搜囱。剛開(kāi)始懷疑是本地settings問(wèn)題或者是neuxs有定時(shí)任務(wù)自動(dòng)清理拱她。為了驗(yàn)證這個(gè)問(wèn)題魂拦,決定每天晚上和第二天早上拉取所有文件做下對(duì)比矢赁。
腳本
爬取文件
import requests
import json
import time
import os
# 訪問(wèn)nexus 遞歸獲取/com/xxx下面文件劣砍,寫入固定文件
def mvn_seenew(path, file_handle):
url = "http://mvn.xxx.info/service/extdirect"
payload = "{\"action\":\"coreui_Browse\",\"method\":\"read\",\"data\":[{\"repositoryName\":\"prod-public\",\"node\":\"" + path + "\"}],\"type\":\"rpc\",\"tid\":9}"
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
result = json.loads(response.text)
data = result.get('result')
if data:
list = data.get('data')
if list:
for meta in list:
file_handle.write(meta.get('id') + "\n")
mvn_seenew(meta.get('id'), file_handle)
if __name__ == '__main__':
num = time.strftime('%Y%m%d', time.localtime(time.time()))
if (os.path.exists("/home/admin/python_tmp/" + num)):
file_name = num + "-2"
else:
file_name = num
file_handle = open(file_name, mode='w')
mvn_seenew('com/xxx', file_handle)
file_handle.close()
對(duì)比文件內(nèi)容
import difflib
import requests
import time
# 將文件對(duì)比結(jié)果輸出到html
def diff_file(out_file):
num = time.strftime('%Y%m%d', time.localtime(time.time()))
# 昨天的文件
a = open((str(int(num)-1)+"-2"), 'r')
ye = a.read()
# 今天的文件
b = open(num, 'r')
td = b.read()
text1_lines = ye.splitlines()
text2_lines = td.splitlines()
d = difflib.HtmlDiff()
result = d.make_file(text1_lines, text2_lines)
with open(out_file, 'w') as f:
f.writelines(result)
# 將html 對(duì)比結(jié)果發(fā)送釘釘機(jī)器人
def send_ding(path):
url = "https://oapi.dingtalk.com/robot/send?access_token=xxx"
payload = "{\"msgtype\": \"text\",\"text\": {\"content\": \"nexus meta-data comparing the results:" + path + "\"}}"
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
if __name__ == "__main__":
diff_file("./diff.html")
send_ding("http://xxxx:9000/diff.html")