Example #1
0
def release_resources(*keys):
    """Release the resources identified by the given keywords.

    Return a list of booleans for the success of each resource release."""

    success = []
    global mapping
    for key in keys:
        try:
            if key == 'remote':
                pass
            elif key == 'mapping' or key == 'lindex' or key == 'rindex':
                gnc.release_db(key)
                if mapping is not None:
                    if key == 'mapping':
                        mapping = None
                    elif key == 'lindex':
                        mapping.lindex = None
                    elif key == 'rindex':
                        mapping.rindex = None
            else:
                gnc.release(key)
            success.append(True)
        except OSError as err:
            logging.warning('release_resources(' + key + '): ' + str(err))
            success.append(False)

    return success
Example #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