前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >同步工具

同步工具

作者头像
超级大猪
发布2019-11-22 14:31:24
6740
发布2019-11-22 14:31:24
举报
文章被收录于专栏:大猪的笔记

常常修改网站,写了个服务器与本机之间的同步工具。把本机修改了的文件同步到服务器去。其实就像自动化脚本。

客户端

main.py

代码语言:javascript
复制
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

代码语言:javascript
复制
#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

代码语言:javascript
复制
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

代码语言:javascript
复制
import lib
import autocmd
lib.filedict.clear()
lib.walk_dir("/root/dazhu",lib.exportMd5)

lib.export_dict("/root/dict.json",lib.filedict)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-08-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 客户端
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档