常常修改网站,写了个服务器与本机之间的同步工具。把本机修改了的文件同步到服务器去。其实就像自动化脚本。
main.py
import lib
import autocmd
lib.make_remote_dict()
lib.scp_dict()
lib.filedict.clear()
lib.walk_dir("/home/yzh/workspace/dazhu",lib.exportMd5)
#lib.walk_dir("/root/dazhu",lib.exportMd5)
#lib.export_dict("/root/dict.json",lib.filedict)
remotejson_dict = lib.import_dict("/home/yzh/remote.json")
compare_result_dict = lib.compare_dict(lib.filedict,remotejson_dict)
#lib.export_dict("/home/yzh/compare_result.json",compare_result_dict)
lib.scp(compare_result_dict)
lib.py
#coding:utf-8
import hashlib
import os
import autocmd
def walk_dir(path,func):
filecount = 0
for root,dirs,files in os.walk(path):
filecount+= len(files)
for item in files:
func(root+"/"+item)
print("count :",filecount)
exclude_list = [".git",".idea","db.sqlite3",".pyc",".log","dgfp_37_hw","migrations"]
prefix = "/home/yzh/workspace"
remote_prefix = "/root"
remote_user = "root"
remote_ip = "108.61.126.179"
remote_pwd = "123321"
filedict = {}
def CalcMD5(filepath):
with open(filepath,'rb') as f:
md5obj = hashlib.md5()
md5obj.update(f.read())
hash = md5obj.hexdigest()
return hash
def exportMd5(filename):
for item in exclude_list:
if item in filename:
return
short_filename = filename.replace(prefix,"")
#print("文件:", short_filename, " "+CalcMD5(filename),"\n")
filedict[short_filename] = CalcMD5(filename)
import json
def export_dict(path,dict):
str = json.dumps(dict, ensure_ascii=False)
file_object = open(path, "w")
file_object.write(str)
file_object.close()
def import_dict(path):
file_object = open(path, "r")
tempdict = json.loads(file_object.read())
return tempdict
def compare_dict(local,remote):
changed_dict = {}
print("start compare")
for item in local:
try:
if local[item] != remote[item]:
#print(item, "is changed")
changed_dict[item] = "ischanged"
except:
#print(item, "not exist")
changed_dict[item] = "notexist"
return changed_dict
def scp(compare_result_dict):
for item in compare_result_dict:
print(item, compare_result_dict[item])
input_str = raw_input("input y to continue")
if input_str != "y":
return
print("start scp files")
for item in compare_result_dict:
cmd = prefix + item
cmd = "scp " + cmd + " " + remote_user + "@" + remote_ip + ":" \
+ remote_prefix + item
def input_yes(obj):
print("input yes")
obj.sendline("yes")
def input_password(obj):
print("input pwd",remote_pwd)
obj.sendline(remote_pwd)
expect_list = ["yes", "assword"]
expect_func = [input_yes,input_password]
autocmd.Cmd(cmd,expect_list,expect_func)
def send_cmd_with_pwd(cmd):
def input_yes(obj):
print("input yes")
obj.sendline("yes")
def input_password(obj):
print("input pwd", remote_pwd)
obj.sendline(remote_pwd)
expect_list = ["yes", "assword"]
expect_func = [input_yes, input_password]
autocmd.Cmd(cmd, expect_list, expect_func)
def scp_dict():
cmd = "scp root@"+remote_ip+":/root/dict.json /home/yzh/remote.json"
send_cmd_with_pwd(cmd)
def make_remote_dict():
cmd = "ssh root@"+remote_ip+" 'python /root/synctool/main.py'"
send_cmd_with_pwd(cmd)
autocmd.py
import pexpect
import traceback
def Cmd(cmd,expectList,funcList):
print(cmd)
pobj = pexpect.spawn(cmd)
while True:
try:
i = pobj.expect(expectList, timeout=100)
funcList[i](pobj)
except pexpect.EOF:
print("over")
print("output",pobj.before)
pobj.close()
return
except pexpect.TIMEOUT:
print("time out")
pobj.close()
return
服务器端
main.py
import lib
import autocmd
lib.filedict.clear()
lib.walk_dir("/root/dazhu",lib.exportMd5)
lib.export_dict("/root/dict.json",lib.filedict)