2014-06-13 17:04:00 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
# This file is part of Supysonic.
|
|
|
|
#
|
|
|
|
# Supysonic is a Python implementation of the Subsonic server API.
|
|
|
|
# Copyright (C) 2014 Alban 'spl0k' Féron
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import time, sys
|
|
|
|
import logging
|
2014-07-27 10:30:45 +00:00
|
|
|
from threading import Thread, Condition, Timer
|
2014-06-14 12:49:03 +00:00
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
2014-06-13 17:04:00 +00:00
|
|
|
from watchdog.observers import Observer
|
|
|
|
from watchdog.events import PatternMatchingEventHandler
|
|
|
|
|
2014-08-03 17:30:29 +00:00
|
|
|
from supysonic import config, db
|
2014-06-13 17:04:00 +00:00
|
|
|
from supysonic.scanner import Scanner
|
|
|
|
|
2014-08-31 10:12:36 +00:00
|
|
|
OP_SCAN = 1
|
|
|
|
OP_REMOVE = 2
|
2014-08-31 10:47:40 +00:00
|
|
|
OP_MOVE = 4
|
2014-08-31 10:12:36 +00:00
|
|
|
|
2014-06-13 17:04:00 +00:00
|
|
|
class SupysonicWatcherEventHandler(PatternMatchingEventHandler):
|
2014-06-14 14:54:04 +00:00
|
|
|
def __init__(self, queue, logger):
|
2014-06-13 17:04:00 +00:00
|
|
|
extensions = config.get('base', 'scanner_extensions')
|
|
|
|
patterns = map(lambda e: "*." + e.lower(), extensions.split()) if extensions else None
|
|
|
|
super(SupysonicWatcherEventHandler, self).__init__(patterns = patterns, ignore_directories = True)
|
|
|
|
|
2014-06-14 14:54:04 +00:00
|
|
|
self.__queue = queue
|
2014-06-13 17:04:00 +00:00
|
|
|
self.__logger = logger
|
|
|
|
|
2014-08-31 10:47:40 +00:00
|
|
|
def dispatch(self, event):
|
|
|
|
try:
|
|
|
|
super(SupysonicWatcherEventHandler, self).dispatch(event)
|
|
|
|
except Exception, e:
|
|
|
|
self.__logger.critical(e)
|
|
|
|
|
2014-06-13 17:04:00 +00:00
|
|
|
def on_created(self, event):
|
2014-06-14 14:54:04 +00:00
|
|
|
self.__logger.debug("File created: '%s'", event.src_path)
|
2014-08-31 10:12:36 +00:00
|
|
|
self.__queue.put(event.src_path, OP_SCAN)
|
2014-06-13 17:04:00 +00:00
|
|
|
|
|
|
|
def on_deleted(self, event):
|
2014-06-14 14:54:04 +00:00
|
|
|
self.__logger.debug("File deleted: '%s'", event.src_path)
|
2014-08-31 10:12:36 +00:00
|
|
|
self.__queue.put(event.src_path, OP_REMOVE)
|
2014-06-13 17:04:00 +00:00
|
|
|
|
|
|
|
def on_modified(self, event):
|
2014-06-14 14:54:04 +00:00
|
|
|
self.__logger.debug("File modified: '%s'", event.src_path)
|
2014-08-31 10:12:36 +00:00
|
|
|
self.__queue.put(event.src_path, OP_SCAN)
|
2014-06-13 17:04:00 +00:00
|
|
|
|
|
|
|
def on_moved(self, event):
|
2014-08-31 10:47:40 +00:00
|
|
|
self.__logger.debug("File moved: '%s' -> '%s'", event.src_path, event.dest_path)
|
2014-09-06 17:18:01 +00:00
|
|
|
self.__queue.put(event.dest_path, OP_MOVE, src_path = event.src_path)
|
2014-08-31 10:12:36 +00:00
|
|
|
|
|
|
|
class Event(object):
|
2014-09-06 17:18:01 +00:00
|
|
|
def __init__(self, path, operation, **kwargs):
|
2014-08-31 10:12:36 +00:00
|
|
|
if operation & (OP_SCAN | OP_REMOVE) == (OP_SCAN | OP_REMOVE):
|
|
|
|
raise Exception("Flags SCAN and REMOVE both set")
|
|
|
|
|
|
|
|
self.__path = path
|
|
|
|
self.__time = time.time()
|
|
|
|
self.__op = operation
|
2014-09-06 17:18:01 +00:00
|
|
|
self.__src = kwargs.get("src_path")
|
2014-08-31 10:12:36 +00:00
|
|
|
|
2014-09-06 17:18:01 +00:00
|
|
|
def set(self, operation, **kwargs):
|
2014-08-31 10:12:36 +00:00
|
|
|
if operation & (OP_SCAN | OP_REMOVE) == (OP_SCAN | OP_REMOVE):
|
|
|
|
raise Exception("Flags SCAN and REMOVE both set")
|
|
|
|
|
|
|
|
self.__time = time.time()
|
|
|
|
if operation & OP_SCAN:
|
|
|
|
self.__op &= ~OP_REMOVE
|
|
|
|
if operation & OP_REMOVE:
|
|
|
|
self.__op &= ~OP_SCAN
|
|
|
|
self.__op |= operation
|
|
|
|
|
2014-09-06 17:18:01 +00:00
|
|
|
src_path = kwargs.get("src_path")
|
|
|
|
if src_path:
|
|
|
|
self.__src = src_path
|
2014-08-31 14:32:17 +00:00
|
|
|
|
2014-08-31 10:12:36 +00:00
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return self.__path
|
|
|
|
|
|
|
|
@property
|
|
|
|
def time(self):
|
|
|
|
return self.__time
|
|
|
|
|
|
|
|
@property
|
|
|
|
def operation(self):
|
|
|
|
return self.__op
|
2014-06-13 17:04:00 +00:00
|
|
|
|
2014-08-31 14:32:17 +00:00
|
|
|
@property
|
2014-09-06 17:18:01 +00:00
|
|
|
def src_path(self):
|
|
|
|
return self.__src
|
2014-08-31 14:32:17 +00:00
|
|
|
|
2014-06-14 14:54:04 +00:00
|
|
|
class ScannerProcessingQueue(Thread):
|
|
|
|
def __init__(self, logger):
|
|
|
|
super(ScannerProcessingQueue, self).__init__()
|
|
|
|
|
|
|
|
self.__logger = logger
|
2014-07-27 10:30:45 +00:00
|
|
|
self.__cond = Condition()
|
|
|
|
self.__timer = None
|
2014-06-14 14:54:04 +00:00
|
|
|
self.__queue = {}
|
|
|
|
self.__running = True
|
|
|
|
|
|
|
|
def run(self):
|
2014-08-31 10:47:40 +00:00
|
|
|
try:
|
|
|
|
self.__run()
|
|
|
|
except Exception, e:
|
|
|
|
self.__logger.critical(e)
|
|
|
|
|
|
|
|
def __run(self):
|
2014-06-14 14:54:04 +00:00
|
|
|
while self.__running:
|
2014-07-27 10:30:45 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
with self.__cond:
|
|
|
|
self.__cond.wait()
|
2014-06-14 14:54:04 +00:00
|
|
|
|
|
|
|
if not self.__queue:
|
|
|
|
continue
|
|
|
|
|
2014-07-27 11:20:51 +00:00
|
|
|
self.__logger.debug("Instantiating scanner")
|
2014-08-03 17:30:29 +00:00
|
|
|
store = db.get_store(config.get('base', 'database_uri'))
|
|
|
|
scanner = Scanner(store)
|
2014-06-14 14:54:04 +00:00
|
|
|
|
2014-08-31 10:12:36 +00:00
|
|
|
item = self.__next_item()
|
|
|
|
while item:
|
2014-08-31 14:32:17 +00:00
|
|
|
if item.operation & OP_MOVE:
|
2014-09-06 17:18:01 +00:00
|
|
|
self.__logger.info("Moving: '%s' -> '%s'", item.src_path, item.path)
|
|
|
|
scanner.move_file(item.src_path, item.path)
|
2014-08-31 10:12:36 +00:00
|
|
|
if item.operation & OP_SCAN:
|
|
|
|
self.__logger.info("Scanning: '%s'", item.path)
|
|
|
|
scanner.scan_file(item.path)
|
|
|
|
if item.operation & OP_REMOVE:
|
|
|
|
self.__logger.info("Removing: '%s'", item.path)
|
|
|
|
scanner.remove_file(item.path)
|
|
|
|
item = self.__next_item()
|
2014-06-14 14:54:04 +00:00
|
|
|
|
2014-08-31 10:12:36 +00:00
|
|
|
scanner.finish()
|
2014-08-03 17:30:29 +00:00
|
|
|
store.commit()
|
|
|
|
store.close()
|
2014-07-27 11:20:51 +00:00
|
|
|
self.__logger.debug("Freeing scanner")
|
|
|
|
del scanner
|
2014-06-14 14:54:04 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.__running = False
|
2014-07-27 10:30:45 +00:00
|
|
|
with self.__cond:
|
|
|
|
self.__cond.notify()
|
2014-06-14 14:54:04 +00:00
|
|
|
|
2014-09-06 17:18:01 +00:00
|
|
|
def put(self, path, operation, **kwargs):
|
2014-06-14 14:54:04 +00:00
|
|
|
if not self.__running:
|
|
|
|
raise RuntimeError("Trying to put an item in a stopped queue")
|
|
|
|
|
2014-07-27 10:30:45 +00:00
|
|
|
with self.__cond:
|
2014-08-31 10:12:36 +00:00
|
|
|
if path in self.__queue:
|
2014-09-06 17:18:01 +00:00
|
|
|
event = self.__queue[path]
|
|
|
|
event.set(operation, **kwargs)
|
2014-08-31 10:12:36 +00:00
|
|
|
else:
|
2014-09-06 17:18:01 +00:00
|
|
|
event = Event(path, operation, **kwargs)
|
|
|
|
self.__queue[path] = event
|
|
|
|
|
|
|
|
if operation & OP_MOVE and kwargs["src_path"] in self.__queue:
|
|
|
|
previous = self.__queue[kwargs["src_path"]]
|
|
|
|
event.set(previous.operation, src_path = previous.src_path)
|
|
|
|
del self.__queue[kwargs["src_path"]]
|
2014-08-31 10:12:36 +00:00
|
|
|
|
2014-07-27 10:30:45 +00:00
|
|
|
if self.__timer:
|
|
|
|
self.__timer.cancel()
|
|
|
|
self.__timer = Timer(5, self.__wakeup)
|
|
|
|
self.__timer.start()
|
|
|
|
|
|
|
|
def __wakeup(self):
|
|
|
|
with self.__cond:
|
|
|
|
self.__cond.notify()
|
|
|
|
self.__timer = None
|
2014-06-14 14:54:04 +00:00
|
|
|
|
2014-07-27 11:20:51 +00:00
|
|
|
def __next_item(self):
|
|
|
|
with self.__cond:
|
|
|
|
if not self.__queue:
|
|
|
|
return None
|
|
|
|
|
2014-08-31 10:12:36 +00:00
|
|
|
next = min(self.__queue.iteritems(), key = lambda i: i[1].time)
|
2014-09-06 17:47:56 +00:00
|
|
|
if not self.__running or next[1].time + 5 <= time.time():
|
2014-07-27 11:20:51 +00:00
|
|
|
del self.__queue[next[0]]
|
2014-08-31 10:12:36 +00:00
|
|
|
return next[1]
|
2014-07-27 11:20:51 +00:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2014-06-13 17:04:00 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
if not config.check():
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2014-06-14 12:49:03 +00:00
|
|
|
if config.get('daemon', 'log_file'):
|
|
|
|
log_handler = TimedRotatingFileHandler(config.get('daemon', 'log_file'), when = 'midnight')
|
|
|
|
else:
|
|
|
|
log_handler = logging.NullHandler()
|
2014-06-13 17:04:00 +00:00
|
|
|
log_handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(message)s"))
|
|
|
|
logger.addHandler(log_handler)
|
2014-06-14 12:49:03 +00:00
|
|
|
if config.get('daemon', 'log_level'):
|
|
|
|
mapping = {
|
|
|
|
'DEBUG': logging.DEBUG,
|
|
|
|
'INFO': logging.INFO,
|
|
|
|
'WARNING': logging.WARNING,
|
|
|
|
'ERROR': logging.ERROR,
|
|
|
|
'CRTICAL': logging.CRITICAL
|
|
|
|
}
|
|
|
|
logger.setLevel(mapping.get(config.get('daemon', 'log_level').upper(), logging.NOTSET))
|
2014-06-13 17:04:00 +00:00
|
|
|
|
2014-08-03 17:30:29 +00:00
|
|
|
store = db.get_store(config.get('base', 'database_uri'))
|
|
|
|
folders = store.find(db.Folder, db.Folder.root == True)
|
2014-06-13 17:04:00 +00:00
|
|
|
|
2014-08-03 17:30:29 +00:00
|
|
|
if not folders.count():
|
2014-06-13 17:04:00 +00:00
|
|
|
logger.info("No folder set. Exiting.")
|
2014-08-03 17:30:29 +00:00
|
|
|
store.close()
|
2014-06-13 17:04:00 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
2014-06-14 14:54:04 +00:00
|
|
|
queue = ScannerProcessingQueue(logger)
|
|
|
|
handler = SupysonicWatcherEventHandler(queue, logger)
|
2014-06-13 17:04:00 +00:00
|
|
|
observer = Observer()
|
2014-06-14 14:54:04 +00:00
|
|
|
|
2014-08-03 17:30:29 +00:00
|
|
|
for folder in folders:
|
2014-06-13 17:04:00 +00:00
|
|
|
logger.info("Starting watcher for %s", folder.path)
|
|
|
|
observer.schedule(handler, folder.path, recursive = True)
|
|
|
|
|
2014-08-03 17:30:29 +00:00
|
|
|
store.close()
|
2014-06-14 14:54:04 +00:00
|
|
|
queue.start()
|
2014-06-13 17:04:00 +00:00
|
|
|
observer.start()
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
time.sleep(1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
logger.info("Stopping watcher")
|
|
|
|
observer.stop()
|
|
|
|
observer.join()
|
2014-06-14 14:54:04 +00:00
|
|
|
queue.stop()
|
|
|
|
queue.join()
|
2014-06-13 17:04:00 +00:00
|
|
|
|