Example #1
0
def sync_all(local, remote, stop):
    logger = logging.getLogger("sync")
    local.eventQueue.join()
    thread_lock.acquire()
    #print "sync all"
    logger.debug("Sync all files.<br />")
    try:
        sync_all_files(local, remote, local.root)
    except Exception as e:
        print e
    finally:
        #print "sync all done"
        thread_lock.release()
    if not stop.isSet():
        sync_all_thread = threading.Timer(5, sync_all, [local, remote, stop])
        sync_all_thread.start()
Example #2
0
def sync_all(local, remote, stop):
    logger = logging.getLogger("sync")
    local.eventQueue.join()
    thread_lock.acquire()
    #print "sync all"
    logger.debug("Sync all files.<br />")
    try:
        sync_all_files(local, remote, local.root)
    except Exception as e:
        print e
    finally:
        #print "sync all done"
        thread_lock.release()
    if not stop.isSet():
        sync_all_thread = threading.Timer(5, sync_all, [local, remote, stop])
        sync_all_thread.start()
Example #3
0
def run(mode, source, destination, sftp_host, sftp_port, hostkey, userkey,
        keypass=None, username=None, password=None, logfile=None, loglevel='INFO',
        stopsync=threading.Event(), **kargs):
    loglevel="DEBUG"
    sync_logger = logging.getLogger("sync")
    event_logger = logging.getLogger("event")
    sync_logger.setLevel(getattr(logging, loglevel))
    event_logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(levelname)s: %(asctime)s %(message)s')
    ch = logging.StreamHandler()
    ch.setLevel(getattr(logging, loglevel))
    ch.setFormatter(formatter)
    if logfile:
        fh = logging.FileHandler(logfile, mode='w')
        fh.setLevel(getattr(logging, loglevel))
        fh.setFormatter(formatter)
        sync_logger.addHandler(fh)
    else:
        sync_logger.addHandler(ch)
    event_logger.addHandler(logging.FileHandler("events", mode="w"))
    paramiko_logger = logging.getLogger("paramiko.transport")
    paramiko_logger.addHandler(logging.NullHandler())

    sync_logger.info("Connect source and destination ...<br />")

    local = OSFileSystem(root=source)
    remote = None
    if mode == 'local':
        remote = OSFileSystem(root=destination)
    elif mode == 'remote':
        try:
            client = SFTPClient.connect(sftp_host, sftp_port, hostkey, userkey, keypass,
                                        username, password)
        except:
            sync_logger.error("Connection failed!<br />")
            local.observer.stop()
            local.observer.join()
            raise
        remote = SFTPFileSystem(client)
    if not remote:
        sync_logger.error("Connection failed!<br />")
        raise Exception("Connection failed.")

    sync_events_thread = threading.Thread(target=sync_events, args=[local, remote,
                                          local.eventQueue, stopsync, thread_lock])
    sync_events_thread.name = "SyncEvents"


    sync_all_files(local, remote, local.root)
    sync_all_files(remote, local, remote.root)

    poll_events(local, remote, stopsync)

    sync_events_thread.start()

    #print threading.enumerate()
    while not stopsync.isSet():
        time.sleep(1)

    if mode == 'local':
        remote.observer.stop()
    local.observer.stop()
    local.eventQueue.put(FileSystemEvent("SyncStopEvent", ""))
    sync_events_thread.join()
    if sync_all_thread:
        sync_all_thread.cancel()
        sync_all_thread.join()
    if poll_thread:
        poll_thread.cancel()
        poll_thread.join()
    local.observer.join()
Example #4
0
def run(mode,
        source,
        destination,
        sftp_host,
        sftp_port,
        hostkey,
        userkey,
        keypass=None,
        username=None,
        password=None,
        logfile=None,
        loglevel='INFO',
        stopsync=threading.Event(),
        **kargs):
    loglevel = "DEBUG"
    sync_logger = logging.getLogger("sync")
    event_logger = logging.getLogger("event")
    sync_logger.setLevel(getattr(logging, loglevel))
    event_logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(levelname)s: %(asctime)s %(message)s')
    ch = logging.StreamHandler()
    ch.setLevel(getattr(logging, loglevel))
    ch.setFormatter(formatter)
    if logfile:
        fh = logging.FileHandler(logfile, mode='w')
        fh.setLevel(getattr(logging, loglevel))
        fh.setFormatter(formatter)
        sync_logger.addHandler(fh)
    else:
        sync_logger.addHandler(ch)
    event_logger.addHandler(logging.FileHandler("events", mode="w"))
    paramiko_logger = logging.getLogger("paramiko.transport")
    paramiko_logger.addHandler(logging.NullHandler())

    sync_logger.info("Connect source and destination ...<br />")

    local = OSFileSystem(root=source)
    remote = None
    if mode == 'local':
        remote = OSFileSystem(root=destination)
    elif mode == 'remote':
        try:
            client = SFTPClient.connect(sftp_host, sftp_port, hostkey, userkey,
                                        keypass, username, password)
        except:
            sync_logger.error("Connection failed!<br />")
            local.observer.stop()
            local.observer.join()
            raise
        remote = SFTPFileSystem(client)
    if not remote:
        sync_logger.error("Connection failed!<br />")
        raise Exception("Connection failed.")

    sync_events_thread = threading.Thread(
        target=sync_events,
        args=[local, remote, local.eventQueue, stopsync, thread_lock])
    sync_events_thread.name = "SyncEvents"

    sync_all_files(local, remote, local.root)
    sync_all_files(remote, local, remote.root)

    poll_events(local, remote, stopsync)

    sync_events_thread.start()

    #print threading.enumerate()
    while not stopsync.isSet():
        time.sleep(1)

    if mode == 'local':
        remote.observer.stop()
    local.observer.stop()
    local.eventQueue.put(FileSystemEvent("SyncStopEvent", ""))
    sync_events_thread.join()
    if sync_all_thread:
        sync_all_thread.cancel()
        sync_all_thread.join()
    if poll_thread:
        poll_thread.cancel()
        poll_thread.join()
    local.observer.join()