Python文件监听

运行环境:

    本地:Window7 64位,Python 2.7.6,paramiko 1.12.1,watchdog 0.8.1

    远端:Ubuntu 14.04,Openssh-server


from auto_ssh_upload_paramiko import SSHFileUpload

auto_ssh_upload_paramiko 模块参看[这里]


#!/usr/bin/python
# coding:utf8

import os
import os.path
import re
import sys
import time
import logging
from logging.handlers import TimedRotatingFileHandler
from ConfigParser import SafeConfigParser
from pathtools.patterns import match_any_paths
from watchdog.observers import Observer

from auto_ssh_upload_paramiko import SSHFileUpload
from dirs_handlers import RsyncRegexMatchingEventHandler, RsyncPatternMatchingEventHandler


def getSSHConfig(section_name=‘env‘, conf_file=‘ssh-config.ini‘):
    config = SafeConfigParser()
    config.readfp(open(conf_file))
    return dict(config.items(section_name))

CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0

_logLevelNames = {
    CRITICAL : ‘CRITICAL‘,
    ERROR : ‘ERROR‘,
    WARNING : ‘WARNING‘,
    INFO : ‘INFO‘,
    DEBUG : ‘DEBUG‘,
    NOTSET : ‘NOTSET‘,
    ‘CRITICAL‘ : CRITICAL,
    ‘ERROR‘ : ERROR,
    ‘WARN‘ : WARNING,
    ‘WARNING‘ : WARNING,
    ‘INFO‘ : INFO,
    ‘DEBUG‘ : DEBUG,
    ‘NOTSET‘ : NOTSET,
}

if __name__ == "__main__":
    config = getSSHConfig("env")
    log_path = config.get(‘log_path‘).replace(‘\\‘, ‘/‘).replace(‘//‘, ‘/‘)
    log_level = config.get(‘log_level‘)

    if not log_level: 
        log_level = _logLevelNames[‘INFO‘]

    formatter=‘%(asctime)s - %(message)s‘
    datefmt=‘%Y-%m-%d %H:%M:%S‘
    logging.basicConfig(
                        level=_logLevelNames[log_level]
                        ,format=formatter
                        ,datefmt=datefmt
                        # ,filename=log_path
                        # ,filemode=‘a‘
                        )

    fmt = logging.Formatter(formatter)
    log_handler = TimedRotatingFileHandler(log_path, when=‘D‘, interval=1, backupCount=40)
    log_handler.setLevel(logging.INFO)
    log_handler.suffix = r"%Y%m%d-%H%M.log"
    log_handler.setFormatter(fmt)
    logging.getLogger().addHandler(log_handler)

    host = config.get(‘host‘)
    port = int(config.get(‘port‘))
    username = config.get(‘username‘)
    password = config.get(‘password‘)
    
    local_path =  config.get(‘local_path‘)#"E:/pyworkspace/pycode/watchdog"
    remote_path = config.get(‘remote_path‘)#"/home/trevor/python_files"

    # ignore_file = ""
    # if config.get(‘ignore_file‘):
    #     ignore_file = config.get(‘ignore_file‘)
    # else:
    #     ignore_file = os.path.join(local_path, ".gitignore")

    # ignore_patterns = []
    # if not ignore_file:
    #     with open(ignore_file.replace("\\", "/")) as f:
    #         ignore_patterns = set([ os.path.join(local_path, ps.strip("\n")) + "*" for ps in f.xreadlines() if ps and (‘\n‘ != ps)])

    ssh = SSHFileUpload(host, port, username, password)

    # event_handler = RsyncPatternMatchingEventHandler(ssh, local_path, remote_path, ignore_patterns=ignore_patterns)
    event_handler = RsyncRegexMatchingEventHandler(ssh, local_path, remote_path, 
        ignore_regexes=[r".*\.tmp$", r".*\.git.*", r".*\.settings.*", r".*\.project.*", r".*\.buildpath.*"])
    observer = Observer()
    observer.schedule(event_handler, local_path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()


配置文件:

[env]
host=192.168.88.128
port=22
username=root
password=your_passwd
local_path=E:/app
remote_path=/home/app
log_path=./dirs-rsync.log




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。