Exemple #1
0
def access_resources(*keys, short_circuit_fail=False):            # TODO: consistency with if and brackets
    """Access the resources identified by the given keywords.

    Return a list of booleans for the success of each resource access.
    Set the list element to True if access was successful and False otherwise."""

    success = []
    global map_head, mapping
    for key in keys:
        try:
            if key == '.psyignore':
                gnc.access('r', '.psyignore')         
            elif key == 'config':
                gnc.access('r+', 'config')
            elif key == 'history':
                gnc.access('r+', 'history')
            elif key == 'mapping':            
                gnc.access('r+', 'map_head')
                new_map = gnc.db['map_head'].readline().strip()
                if mapping is None or mapping.name != new_map:
                    if not release_resources('mapping'):
                        raise PermissionError('cannot close previous mapping')
                    gnc.access_db('mapping', 'mappings/' + new_map + '.db')                    
                    mapping = SqlMapping('mapping', new_map)
            elif key == 'lindex':
                if mapping is None:
                    raise FileNotFoundError('no mapping loaded')
                if mapping.lindex_name is None:
                    raise FileNotFoundError('no lindex specified')
                if mapping.lindex is None or mapping.lindex.name != mapping.lindex_name:
                    release_resources('lindex')
                    gnc.access_db('lindex', 'indexes/local/' + mapping.lindex_name + '.db')
                    mapping.lindex = SqlIndex('lindex', mapping.lindex_name)
            elif key == 'rindex':
                if mapping is None:
                    raise FileNotFoundError('no mapping loaded')
                if mapping.rindex_name is None:
                    raise FileNotFoundError('no rindex specified')
                if mapping.rindex is None or mapping.rindex.name != mapping.rindex_name:
                    release_resources('rindex')
                    gnc.access_db('rindex', 'indexes/remote/' + mapping.rindex_name + '.db')
                    mapping.rindex = SqlIndex('rindex', mapping.rindex_name)
            elif key == 'remote':
                if mapping is None:
                    raise FileNotFoundError('no mapping loaded')
                if mapping.rindex_name is None:
                    raise FileNotFoundError('no rindex specified')
                capcom.access_remote(mapping.rindex_name)
            else:
                gnc.access('r+', key)
            success.append(True)
        except (OSError, sqlite3.DatabaseError) as err:
            logging.warning('access_resources(' + key + '): ' + str(err))
            success.append(False)
            if short_circuit_fail:
                return success
    return success
Exemple #2
0
def pushsync_init(opts, args):
    """Create the .pushsync directory in %APPDATA% and populate it with operation files."""

    try:
        psy_path = gnc.pushsync_init()
        
        # Create dirs and files.

        gnc.mkdir('indexes')
        gnc.mkdir('indexes/local')
        gnc.mkdir('indexes/remote')
        gnc.mkdir('mappings')
        gnc.mkdir('remotes')    
        gnc.mkdir('cwd')
        gnc.mkdir('cwd/local')
        gnc.mkdir('cwd/remote')

        gnc.touch('.psyignore')            # TODO: w or a?
        gnc.touch('.psyrc') 
        gnc.touch('config')
        gnc.touch('history')
        gnc.touch('map_head')
        gnc.touch('cwd/local/local')

        # Initialize

        global config
        access_resources('config')
        write_config('config', config)

        gnc.write("*.tmp\n" + "~$*.doc\n" + "Thumbs.db\n", 'a', '.psyignore')        
        gnc.write("app_key = []\n" + "app_secret = []\n" + "access_token = []\n", 'a', 'remotes/dropbox')
        gnc.write("master\n", 'a', 'map_head')        

        # Initialize SQL stuff

        gnc.access_db('indexes/local/lindex.db')
        dbindex = gnc.db['indexes/local/lindex.db']
        curin = dbindex.cursor()
        curin.execute('''CREATE TABLE inodes
            (
                path TEXT NOT NULL PRIMARY KEY,
                head_id INTEGER NOT NULL,
                refcount INTEGER NOT NULL,
                FOREIGN KEY (head_id) REFERENCES revs (rev_id)
            )''')
        curin.execute('''CREATE TABLE revs
            (
                rev_id INTEGER PRIMARY KEY,
                parent_id INTEGER,
                mtime TEXT,
                size INTEGER,
                refcount INTEGER NOT NULL,
                misc TEXT
            )''')
        dbindex.commit()
        gnc.release_db('indexes/local/lindex.db')

        gnc.access_db('mappings/master.db')
        dbmapping = gnc.db['mappings/master.db']
        curma = dbmapping.cursor()
        curma.execute('''CREATE TABLE mapping
            (
                lpath TEXT NOT NULL,
                rpath TEXT NOT NULL
            )''')
        curma.execute('''CREATE TABLE domains
            (
                local TEXT,
                remote TEXT
            )''')
        curma.execute('''CREATE TABLE syncs
            (
                local_rev INTEGER NOT NULL,
                remote_rev INTEGER NOT NULL
            )''')
        curma.execute('''CREATE INDEX lpath ON mapping(lpath)''')
        curma.execute('''CREATE INDEX rpath ON mapping(rpath)''')
        curma.execute('''CREATE INDEX local_rev ON syncs(local_rev)''')
        curma.execute('''CREATE INDEX remote_rev ON syncs(remote_rev)''')
        curma.execute('''INSERT INTO domains(local, remote) VALUES (?, NULL)''', ('lindex',))

        dbmapping.commit()
        gnc.release_db('mappings/master.db')

        init()
    except (OSError, sqlite3.DatabaseError):
        return None

    return psy_path