Example #1
1
def sync(devices=[]):
    '''
    Run continuous synchronization between CouchDB instances.
    '''
    if len(devices) == 0:
        devices = local_config.get_default_devices()

    for name in devices:
        (url, path) = local_config.get_config(name)
        (device_id, device_password) = local_config.get_device_config(name)
        (db_login, db_password) = local_config.get_db_credentials(name)

        print 'Start continuous replication from Cozy to device.'
        replication.replicate(name, url, name, device_password, device_id,
                              db_login, db_password, to_local=True)
        print 'Start continuous replication from device to Cozy.'
        replication.replicate(name, url, name, device_password, device_id,
                              db_login, db_password)

        print 'Continuous replications started.'
        print 'Running daemon for binary synchronization...'
        try:
            context = local_config.get_daemon_context(name, 'sync')
            with context:
                replication.BinaryReplication(name)
        except KeyboardInterrupt:
            print ' Binary Synchronization interrupted.'
Example #2
0
def unmount_folder(name, path=None):
    '''
    Unmount folder linked to given device.
    '''
    if path is None:
        (url, path) = local_config.get_config(name)
    couchmount.unmount(path)
Example #3
0
def remove_device_remotely(name, password):
    '''
    Delete given device form target Cozy.
    '''
    (url, path) = local_config.get_config(name)
    (device_id, device_password) = local_config.get_device_config(name)
    remote.remove_device(url, device_id, password)
Example #4
0
def mount_folder(devices=[]):
    '''
    Mount folder linked to given device.
    '''
    if len(devices) == 0:
        devices = local_config.get_default_devices()

    for name in devices:
        try:
            (url, path) = local_config.get_config(name)
            # try to create the directory if it does not exist
            try:
                os.makedirs(path)
                couchmount.unmount(path)
            except OSError as e:
                if e.errno == errno.EACCES:
                    print 'You do not have sufficient access, ' \
                          'try running sudo %s' % (' '.join(sys.argv[:]))
                    sys.exit(1)
                elif e.errno == errno.EEXIST:
                    pass
                else:
                    continue
            couchmount.mount(name, path)
        except KeyboardInterrupt:
            unmount_folder(name)
Example #5
0
def init_replication(name):
    '''
    Run initial replications then start continutous replication.
    Write device information in database.
    '''
    (url, path) = local_config.get_config(name)
    (device_id, password) = local_config.get_device_config(name)
    (db_login, db_password) = local_config.get_db_credentials(name)

    print 'Replication from remote to local...'
    replication.replicate(
        name, url, name, password, device_id, db_login, db_password,
        to_local=True, continuous=False, deleted=False)
    print 'Init device...'
    dbutils.init_device(name, url, path, password, device_id)
    print 'Replication from local to remote...'
    replication.replicate(
        name, url, name, password, device_id, db_login, db_password,
        to_local=False, continuous=False)

    print 'Continuous replication from remote to local setting...'
    replication.replicate(name, url, name, password, device_id,
                          db_login, db_password, to_local=True)

    print 'Continuous replication from local to remote setting...'
    replication.replicate(name, url, name, password, device_id,
                          db_login, db_password)
    print 'Metadata replications are done.'
Example #6
0
def remove_device_remotely(name):
    '''
    Delete given device form target Cozy.
    '''
    (url, path) = local_config.get_config(name)
    (device_id, password) = local_config.get_device_config(name)
    password = getpass.getpass('Type your Cozy password to remove your '
                               'device remotely:\n')
    remote.remove_device(url, device_id, password)
Example #7
0
def mount_folder(name):
    '''
    Mount folder linked to given device.
    '''
    try:
        (url, path) = local_config.get_config(name)
        couchmount.unmount(path)
        couchmount.mount(name, path)
    except KeyboardInterrupt:
        unmount_folder(name)
Example #8
0
def unmount_folder(devices=[], path=None):
    '''
    Unmount folder linked to given device.
    '''
    if len(devices) == 0:
        devices = local_config.get_default_devices()

    for name in devices:
        if path is None:
            (url, path) = local_config.get_config(name)
        couchmount.unmount(path)
Example #9
0
def register_device_remotely(name):
    '''
    Register device to target Cozy
    '''
    (url, path) = local_config.get_config(name)
    if url[-1:] == '/':
        url = url[:-(len(name)+1)]
    password = getpass.getpass('Type your Cozy password to register your '
                               'device remotely:\n')
    (device_id, device_password) = remote.register_device(name, url,
                                                          path, password)
    local_config.set_device_config(name, device_id, device_password)
Example #10
0
    def on_select(self, list_adapter, *args):
        if len(list_adapter.selection) > 0:
            name = list_adapter.selection[0].text
            self.selected_device = name
            (url, path) = get_config(name)
            self.device_url.text = url
            self.device_mount_path.text = path
            self.device_details.opacity = 1.0

        else:
            self.device_details.opacity = 0.0
            self.selected_device = None
Example #11
0
def register_device_remotely(name, password=None):
    '''
    Register device to target Cozy
    '''
    (url, path) = local_config.get_config(name)
    # Remove trailing slash
    url = url.rstrip('/')
    if password is None:
        password = getpass.getpass('Type your Cozy password to register your '
                                   'device remotely:\n')
    (device_id, device_password) = remote.register_device(name, url,
                                                          path, password)
    local_config.set_device_config(name, device_id, device_password)
Example #12
0
def cache_folder(device, path, add=True):
    '''
    Download target file from remote Cozy to local folder.
    '''

    # Get configuration.
    (device_url, device_mount_path) = local_config.get_config(device)
    (device_id, device_password) = local_config.get_device_config(device)
    (db_username, db_password) = local_config.get_db_credentials(device)

    # Built target device url.
    device_url = "http://%s:%s@localhost:5984/%s" % (
        db_username,
        db_password,
        device
    )

    # Ensure that path corresponds to a mounted folder.
    abs_path = os.path.abspath(path)
    device_mount_path = os.path.abspath(device_mount_path)
    device_mount_path_len = len(device_mount_path)
    device_config_path = os.path.join(local_config.CONFIG_FOLDER, device)

    if add:
        print "Start %s caching folder." % abs_path
    else:
        print "Start %s uncaching folder." % abs_path

    if abs_path[:device_mount_path_len] == device_mount_path:

        # Cache object
        binary_cache = binarycache.BinaryCache(
            device, device_config_path, device_url, device_mount_path)

        # Walk through given folder and run cache operation on each file found.
        for (dirpath, dirnames, filenames) in os.walk(abs_path):
            for filename in filenames:
                file_path = os.path.join(dirpath, filename)
                file_path = file_path[device_mount_path_len:]
                file_path = couchmount._normalize_path(file_path)

                if add:
                    binary_cache.add(file_path)
                    print "File %s successfully cached." % file_path
                else:
                    binary_cache.remove(file_path)
                    print "File %s successfully uncached." % file_path
    else:
        print 'This is not a folder synchronized with your Cozy'
Example #13
0
def register_device_remotely(name, password):
    '''
    Register device to target Cozy
    '''
    (url, path) = local_config.get_config(name)
    if url[-1:] == '/':
        url = url[:-(len(name)+1)]
    try:
        (device_id, device_password) = remote.register_device(name, url,
                                                          path, password)

        local_config.set_device_config(name, device_id, device_password)

    except remote.WrongCozyURL, e:
        local_config.remove_config(name)
        raise ProcessedException(e)
Example #14
0
def unregister_device(device):
    '''
    Remove device from local configuration, destroy corresponding database
    and unregister it from remote Cozy.
    '''
    (url, path) = local_config.get_config(device)
    (device_id, device_password) = local_config.get_device_config(device)

    print 'Cozy connection removal for %s.' % device
    local_config.remove(device)
    print '- Local configuration removed.'
    dbutils.remove_db(device)
    print '- Local files removed.'
    password = getpass.getpass('Please type the password of your Cozy:\n')
    remote.remove_device(url, device_id, password)
    print '- Remote configuration removed.'
    print 'Removal succeeded, everything clean!' % device
Example #15
0
def sync(name):
    '''
    Run continuous synchronization between CouchDB instances.
    '''
    (url, path) = local_config.get_config(name)
    (device_id, device_password) = local_config.get_device_config(name)
    (db_login, db_password) = local_config.get_db_credentials(name)

    replication.replicate(name, url, name, device_password, device_id,
                          db_login, db_password, to_local=True)
    replication.replicate(name, url, name, device_password, device_id,
                          db_login, db_password)

    print 'Continuous replications started.'
    print 'Running daemon for binary synchronization...'
    try:
        replication.BinaryReplication(name)
    except local_config.DaemonAlreadyRunning, e:
        print e
Example #16
0
def remove_device(device):
    '''
    Remove device from local and remote configuration by:

    * Unmounting device folder.
    * Removing device on corresponding remote cozy.
    * Removing device from configuration file.
    * Destroying corresponding DB.
    '''
    (url, path) = local_config.get_config(device)

    couchmount.unmount(path)
    remove_device_remotely(device)

    # Remove database
    dbutils.remove_db(device)
    dbutils.remove_db_user(device)

    local_config.remove_config(device)
    print 'Configuration %s successfully removed.' % device
Example #17
0
def sync(name):
    '''
    Run continuous synchronization between CouchDB instances.
    '''
    (url, path) = local_config.get_config(name)
    (device_id, device_password) = local_config.get_device_config(name)
    (db_login, db_password) = local_config.get_db_credentials(name)

    replication.replicate(name, url, name, device_password, device_id,
                          db_login, db_password, to_local=True)
    replication.replicate(name, url, name, device_password, device_id,
                          db_login, db_password)

    print 'Continuous replications started.'
    print 'Running daemon for binary synchronization...'
    try:
        context = local_config.get_daemon_context(name, 'sync')
        with context:
            replication.BinaryReplication(name)
    except KeyboardInterrupt:
        print ' Binary Synchronization interrupted.'
Example #18
0
def cache_file(device, path, add=True):
    '''
    Download target file from remote Cozy to local cache.
    '''

    # Get configuration.
    (device_url, device_mount_path) = local_config.get_config(device)
    (device_id, device_password) = local_config.get_device_config(device)
    (db_username, db_password) = local_config.get_db_credentials(device)

    # Built target device url.
    device_url = "http://%s:%s@localhost:5984/%s" % (
        db_username,
        db_password,
        device
    )

    # Ensure that path corresponds to a mounted file.
    abs_path = os.path.abspath(path)
    device_mount_path = os.path.abspath(device_mount_path)
    device_mount_path_len = len(device_mount_path)
    device_config_path = os.path.join(local_config.CONFIG_FOLDER, device)
    path = abs_path[device_mount_path_len:]
    path = couchmount._normalize_path(path)

    print "Start %s caching." % abs_path
    if abs_path[:device_mount_path_len] == device_mount_path:
        binary_cache = binarycache.BinaryCache(
            device, device_config_path, device_url, device_mount_path)
        if add:
            binary_cache.add(path)
            print "File %s successfully cached." % abs_path
        else:
            binary_cache.remove(path)
            print "File %s successfully uncached." % abs_path

    else:
        print "Wrong path, that doesn't match any file in your device folder"