예제 #1
0
파일: dirsend.py 프로젝트: cutchin/dirsend
def releaseLock():
    global fileDesc

    logger.debug("Releasing lock")
    fcntl.flock(fileDesc, fcntl.LOCK_UN)
    os.close(fileDesc)
    os.unlink(args.lockFile)
예제 #2
0
파일: db.py 프로젝트: cutchin/dirsend
def createDB():
    global logger
    logger.debug('Initializing new database at location {}'.format(args.dbFile))

    cursor.execute('''CREATE TABLE files
      (idx INTEGER PRIMARY KEY ASC,
      fileName TEXT NOT NULL)''')

    connection.commit()
예제 #3
0
파일: dirsend.py 프로젝트: cutchin/dirsend
def acquireLock():
    global fileDesc
    fileDesc = os.open(args.lockFile, os.O_CREAT)

    logger.debug("Acquiring lock using file {}".format(args.lockFile))
    try:
        fcntl.flock(fileDesc, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except:
        logger.error("Error - unable to acquire lock.  (Is another instance running?)")
        sys.exit()
예제 #4
0
파일: db.py 프로젝트: cutchin/dirsend
def openDB():
    global connection
    global cursor

    needsInit = not os.path.isfile(args.dbFile)

    connection = sqlite3.connect(args.dbFile)
    cursor = connection.cursor()

    if needsInit:
        createDB()

    logger.debug('Database {} opened'.format(args.dbFile))
예제 #5
0
파일: db.py 프로젝트: cutchin/dirsend
def deleteSet(deleteSet):
    global connection
    global cursor

    logger.debug('Removing {} entries from database'.format(len(deleteSet)))

    while len(deleteSet) > 0:
        currentSet = deleteSet[:100]
        deleteStr = ", ".join(map(str, currentSet));
        logger.debug('Removing batch of {} entries'.format(len(currentSet)))

        cursor.execute('DELETE FROM files WHERE rowid in ({})'.format(deleteStr))
        deleteSet = deleteSet[100:]
    connection.commit()
예제 #6
0
파일: dirsend.py 프로젝트: cutchin/dirsend
def cleanDB():
    logger.debug("Purging missing files from database")
    
    files = [ os.path.abspath(args.directory + '/' + f) for f in os.listdir(args.directory)]
    
    deleteFiles = []

    entries = db.selectAll()

    while len(entries) > 0:
        for nextEntry in entries:
            if not nextEntry[1] in files:
                logger.debug("Marking file {} for removal from database".format(nextEntry[1]))
                deleteFiles.append(nextEntry[0])

        entries = db.fetchNext()

    db.deleteSet(deleteFiles)
예제 #7
0
파일: dirsend.py 프로젝트: cutchin/dirsend
def processDir():
    fileList = os.listdir(args.directory)

    for fileName in fileList:
        absPath = os.path.abspath(args.directory + '/' + fileName)

        if args.skipDotFiles and fileName.startswith('.'):
            continue

        if db.findFile(absPath):
            logger.debug("File {} already processed".format(absPath))
            continue

        if args.storeOnly:
            print "Marking {} as processed".format(absPath)
            db.addFile(absPath)
            continue

        print "Processing file {}".format(absPath)
        cmd = args.command.format(absPath)
        logger.debug("Executing command \"{}\"".format(cmd))
        returnCode = call(cmd, shell=True)

        if (returnCode == 0):
            logger.debug("Processing file {}".format(absPath))
            db.addFile(absPath)
        else:
            logger.error("Error synchronizing file {}".format(absPath))
            return False
    
    return True
예제 #8
0
파일: db.py 프로젝트: cutchin/dirsend
def deleteIndex(idx):
    logger.debug("Deleting entry {} from file database".format(idx))
    rslt = cursor.execute('DELETE FROM files WHERE rowid=?', (idx,))
    if (rslt.rowcount == 0):
        logger.error("No file with index {} was present".format(idx))
    connection.commit()