コード例 #1
0
ファイル: test_fs.py プロジェクト: salsa-dev/MiGBox
 def test_walkOS(self):
     print "walk os"
     fs = OSFileSystem()
     for pathname in fs.walk(fs.root):
         print "path " + pathname
         rel_path = fs.get_relative_path(pathname)
         print "rel_path " + rel_path
         sync_path = fs.join_path(fs.root, rel_path)
         print "sync_path " + sync_path
     pass
コード例 #2
0
ファイル: test_fs.py プロジェクト: bertl4398/MiGBox
 def setUpClass(cls):
     try:
         os.mkdir(".testdir")
     except:
         pass
     finally:
         cls.fs = OSFileSystem(root=".testdir")
コード例 #3
0
ファイル: syncd.py プロジェクト: salsa-dev/MiGBox
def run(mode, source, destination, sftp_host, sftp_port, hostkey, userkey,
         username='', password='', logfile=None, loglevel='INFO',
         event=threading.Event(), **kargs):

    logger = logging.getLogger("sync")
    logger.setLevel(getattr(logging, loglevel))
    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)
        logger.addHandler(fh)
    else:
        logger.addHandler(ch)

    paramiko_logger = logging.getLogger("paramiko.transport")
    paramiko_logger.addHandler(logging.NullHandler())

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

    local = OSFileSystem(root=source)
    if mode == 'local':
        remote = OSFileSystem(root=destination)
    elif mode == 'remote':
        client = SFTPClient.connect(sftp_host, sftp_port, hostkey, userkey)
        if not client:
            client = SFTPClient.connect(sftp_host, sftp_port, hostkey, userkey,
                                        username, password)
        remote = SFTPFileSystem(client)
        try:
            remote.remove(os.path.join(remote.root, username))
        except:
            pass

    # copy all new files from local to remote
    # sync all modifications from local/remote to local/remote
    # modifications are compared by modification time, the latest wins
    sync.sync_all_files(local, remote, local.root)
    # copy all new files from remote to local
    # sync no modifications, already synced
    sync.sync_all_files(remote, local, remote.root, modified=False)

    lock = threading.Lock()
    event_handler = EventHandler(local, remote, lock)
    observer = Observer()
    observer.schedule(event_handler, path=source, recursive=True)
    observer.start()

    stop_polling = threading.Event()
    threading.Timer(2, poll, [local, remote, lock, stop_polling]).start()
    
    while not event.isSet():
        time.sleep(1)

    stop_polling.set()
    time.sleep(2)

    observer.stop()
    observer.join()
コード例 #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()