def process(self, abspathitem): db = dbapi.connect(self.dbpath) if os.path.isdir(abspathitem): breadth_scan(abspathitem, db, self.queues, self.condition, True) else: if abspathitem in self.recents: db.close() return logging.debug('Collecting metadata') collect_metadata(abspathitem, db, self.recentartists, self.recentalbums, self.recentgenres, self.queues, self.condition) if len(self.recents) >= 20: self.recents.pop(0) self.recents.append(abspathitem) if len(self.recentalbums) > 20: self.recentalbums.clear() if len(self.recentgenres) > 20: self.recentgenres.clear() if len(self.recentsongs) > 20: self.recentsongs.clear() if len(self.recentartists) > 20: self.recentartists.clear() db.close()
def process(self, abspathitem): db = dbapi.connect(self.dbpath) if os.path.isdir(abspathitem): breadth_scan(abspathitem, db, self.queues, self.condition, True) else: if abspathitem in self.recents: db.close() return logging.debug('Collecting metadata') collect_metadata( abspathitem, db, self.recentartists, self.recentalbums, self.recentgenres, self.queues, self.condition) if len(self.recents) >= 20: self.recents.pop(0) self.recents.append(abspathitem) if len(self.recentalbums) > 20: self.recentalbums.clear() if len(self.recentgenres) > 20: self.recentgenres.clear() if len(self.recentsongs) > 20: self.recentsongs.clear() if len(self.recentartists) > 20: self.recentartists.clear() db.close()
def process_IN_ISDIR(self, evt): if evt.name[0] == '.': return logging.debug("IN_ISDIR %s" % evt.path) newpath = "%s/%s" % (evt.path, evt.name) db = dbapi.connect(self.dbpath) breadth_scan(newpath, db, self.queues, self.condition, True) db.close()
def process_event(self, evt): if evt.name == self.dbpath or os.path.basename(evt.name)[0] == '.': return elif evt.mask == IN_CREATE: logging.debug("IN_CREATE %s" % evt.name) with self._lock: fd = os.open(evt.name, os.O_RDONLY) events = [kevent(fd, filter=select.KQ_FILTER_READ, flags=select.KQ_EV_ADD | select.KQ_EV_CLEAR, fflags=select.KQ_NOTE_EXTEND)] #events_stop = [kevent(fd, filter=select.KQ_FILTER_READ, flags=select.KQ_EV_DELETE, fflags=select.KQ_NOTE_EXTEND)] kq = kqueue() size = -1 while True: evts = kq.control(events, 10, 5) newsize = os.stat(evt.name).st_size if newsize == size and size > 0: # evts = kq.control(events_stop, 0) break size = newsize logging.debug("SIZE %d" % os.stat(evt.name).st_size) os.close(fd) self.process(evt.name) elif evt.mask == IN_MODIFY: logging.debug("IN_MODIFY %s" % evt.name) with self._lock: self.process(evt.name) elif evt.mask == IN_ATTRIB: logging.debug("IN_ATTRIB %s" % evt.name) #self.process(evt.name) elif evt.mask == IN_MOVED_TO: logging.debug("IN_MOVED_TO %s" % evt.name) if os.path.isdir(evt.name): db = dbapi.connect(self.dbpath) with self._lock: breadth_scan( evt.name, db, self.lf_queue, self.di_queue, self.fd_queue, self.condition, True) db.close() else: self.process(evt.name) elif evt.mask in (IN_DELETE, IN_MOVED_FROM): logging.debug("IN_DELETE %s" % evt.name) if evt.name in (self.dbpath, "%s-journal" % self.dbpath): return with self.condition: db = dbapi.connect(self.dbpath) try: songs = db.execute( "select id from song where path = ?;", (evt.name.decode(FS_ENCODING),)) song_id = [songs.fetchone()] if not song_id[0]: logging.debug("DELETION DIRECTORY (?): %s" % evt.name) songs = db.execute( "select id from song where path like ?;", ("%s%%" % evt.name.decode(FS_ENCODING),)) song_id = songs.fetchall() self.recents = [] if song_id and song_id[0]: logging.debug("SONG_ID: %s" % song_id) for s_i in song_id: db.execute( "delete from song where id = ?;", s_i) db.execute( "delete from song_x_tag " "where song_id = ?;", s_i) db.commit() except Exception as e: logging.error(e) finally: db.close() if evt.name in self.recents: self.recents.remove(evt.name) else: logging.debug("IN_??? %s" % evt.mask)
def start_scan(path, db, queues, condition, depth=1): breadth_scan(path, db, queues, condition, depth)
def process_event(self, evt): if evt.name == self.dbpath or os.path.basename(evt.name)[0] == '.': return elif evt.mask == IN_CREATE: logging.debug("IN_CREATE %s" % evt.name) with self._lock: fd = os.open(evt.name, os.O_RDONLY) events = [ kevent(fd, filter=select.KQ_FILTER_READ, flags=select.KQ_EV_ADD | select.KQ_EV_CLEAR, fflags=select.KQ_NOTE_EXTEND) ] #events_stop = [kevent(fd, filter=select.KQ_FILTER_READ, flags=select.KQ_EV_DELETE, fflags=select.KQ_NOTE_EXTEND)] kq = kqueue() size = -1 while True: evts = kq.control(events, 10, 5) newsize = os.stat(evt.name).st_size if newsize == size and size > 0: # evts = kq.control(events_stop, 0) break size = newsize logging.debug("SIZE %d" % os.stat(evt.name).st_size) os.close(fd) self.process(evt.name) elif evt.mask == IN_MODIFY: logging.debug("IN_MODIFY %s" % evt.name) with self._lock: self.process(evt.name) elif evt.mask == IN_ATTRIB: logging.debug("IN_ATTRIB %s" % evt.name) #self.process(evt.name) elif evt.mask == IN_MOVED_TO: logging.debug("IN_MOVED_TO %s" % evt.name) if os.path.isdir(evt.name): db = dbapi.connect(self.dbpath) with self._lock: breadth_scan(evt.name, db, self.lf_queue, self.di_queue, self.fd_queue, self.condition, True) db.close() else: self.process(evt.name) elif evt.mask in (IN_DELETE, IN_MOVED_FROM): logging.debug("IN_DELETE %s" % evt.name) if evt.name in (self.dbpath, "%s-journal" % self.dbpath): return with self.condition: db = dbapi.connect(self.dbpath) try: songs = db.execute("select id from song where path = ?;", (evt.name.decode(FS_ENCODING), )) song_id = [songs.fetchone()] if not song_id[0]: logging.debug("DELETION DIRECTORY (?): %s" % evt.name) songs = db.execute( "select id from song where path like ?;", ("%s%%" % evt.name.decode(FS_ENCODING), )) song_id = songs.fetchall() self.recents = [] if song_id and song_id[0]: logging.debug("SONG_ID: %s" % song_id) for s_i in song_id: db.execute("delete from song where id = ?;", s_i) db.execute( "delete from song_x_tag " "where song_id = ?;", s_i) db.commit() except Exception as e: logging.error(e) finally: db.close() if evt.name in self.recents: self.recents.remove(evt.name) else: logging.debug("IN_??? %s" % evt.mask)