def releaseLock(): global fileDesc logger.debug("Releasing lock") fcntl.flock(fileDesc, fcntl.LOCK_UN) os.close(fileDesc) os.unlink(args.lockFile)
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()
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()
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))
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()
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)
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
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()