Changeset - a73280491145
[Not reviewed]
1 1 3
Lance Edgar (lance) - 11 years ago 2013-06-01 22:13:38
lance@edbob.org
Slight overhaul of Linux file monitor.

This includes the following:

* "More native" Linux file monitor (i.e. less reliant on `edbob`; current code
is more or less copied from that project).
* Addition of `--pidfile` command argument on Linux.
4 files changed with 186 insertions and 6 deletions:
0 comments (0 inline, 0 general)
rattail/commands.py
Show inline comments
 
@@ -112,7 +112,11 @@ class FileMonitorCommand(commands.Subcommand):
 
        stop = subparsers.add_parser('stop', help="Stop service")
 
        stop.set_defaults(subcommand='stop')
 

	
 
        if sys.platform == 'win32':
 
        if sys.platform == 'linux2':
 
            parser.add_argument('-p', '--pidfile',
 
                                help="Path to PID file", metavar='PATH')
 

	
 
        elif sys.platform == 'win32':
 
            install = subparsers.add_parser('install', help="Install service")
 
            install.set_defaults(subcommand='install')
 
            install.add_argument('-a', '--auto-start', action='store_true',
 
@@ -124,17 +128,17 @@ class FileMonitorCommand(commands.Subcommand):
 

	
 
    def run(self, args):
 
        if sys.platform == 'linux2':
 
            from edbob.filemon import linux as filemon
 
            from rattail.filemon import linux as filemon
 

	
 
            if args.subcommand == 'start':
 
                filemon.start_daemon('rattail')
 
                filemon.start_daemon(args.pidfile)
 

	
 
            elif args.subcommand == 'stop':
 
                filemon.stop_daemon('rattail')
 
                filemon.stop_daemon(args.pidfile)
 

	
 
        elif sys.platform == 'win32':
 
            from edbob import win32
 
            from rattail import filemon
 
            from rattail.filemon import win32 as filemon
 
            from rattail.win32 import require_elevation
 

	
 
            require_elevation()
rattail/filemon/__init__.py
Show inline comments
 
new file 100644
 
#!/usr/bin/env python
 
# -*- coding: utf-8  -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2012 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
#  Rattail 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.
 
#
 
#  Rattail 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 Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 

	
 
"""
 
``rattail.filemon`` -- File Monitor
 
"""
rattail/filemon/linux.py
Show inline comments
 
new file 100644
 
#!/usr/bin/env python
 
# -*- coding: utf-8  -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2012 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
#  Rattail 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.
 
#
 
#  Rattail 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 Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 

	
 
"""
 
``rattail.filemon.linux`` -- File Monitor for Linux
 
"""
 

	
 
import sys
 
import os.path
 
import pyinotify
 
import threading
 
import Queue
 
import logging
 

	
 
import edbob
 
from edbob import filemon
 
from edbob.daemon import Daemon
 
from edbob.errors import email_exception
 

	
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
class EventHandler(pyinotify.ProcessEvent):
 
    """
 
    Event processor for file monitor daemon.
 
    """
 

	
 
    def my_init(self, profile=None, **kwargs):
 
        self.profile = profile
 

	
 
    def process_IN_ACCESS(self, event):
 
        log.debug("EventHandler: IN_ACCESS: %s" % event.pathname)
 

	
 
    def process_IN_ATTRIB(self, event):
 
        log.debug("EventHandler: IN_ATTRIB: %s" % event.pathname)
 

	
 
    def process_IN_CLOSE_WRITE(self, event):
 
        log.debug("EventHandler: IN_CLOSE_WRITE: %s" % event.pathname)
 
        if not self.profile.locks:
 
            self.profile.queue.put(event.pathname)
 

	
 
    def process_IN_CREATE(self, event):
 
        log.debug("EventHandler: IN_CREATE: %s" % event.pathname)
 

	
 
    def process_IN_DELETE(self, event):
 
        log.debug("EventHandler: IN_DELETE: %s" % event.pathname)
 
        if self.profile.locks and event.pathname.endswith('.lock'):
 
            self.profile.queue.put(event.pathname[:-5])
 

	
 
    def process_IN_MODIFY(self, event):
 
        log.debug("EventHandler: IN_MODIFY: %s" % event.pathname)
 

	
 
    def process_IN_MOVED_TO(self, event):
 
        log.debug("EventHandler: IN_MOVED_TO: %s" % event.pathname)
 
        if not self.profile.locks:
 
            self.profile.queue.put(event.pathname)
 

	
 

	
 
class FileMonitorDaemon(Daemon):
 

	
 
    def run(self):
 

	
 
        wm = pyinotify.WatchManager()
 
        notifier = pyinotify.Notifier(wm)
 

	
 
        mask = (pyinotify.IN_ACCESS
 
                | pyinotify.IN_ATTRIB
 
                | pyinotify.IN_CLOSE_WRITE
 
                | pyinotify.IN_CREATE
 
                | pyinotify.IN_DELETE
 
                | pyinotify.IN_MODIFY
 
                | pyinotify.IN_MOVED_TO)
 

	
 
        monitored = filemon.get_monitor_profiles('rattail')
 
        for key, profile in monitored.iteritems():
 

	
 
            # Create a file queue for the profile.
 
            profile.queue = Queue.Queue()
 

	
 
            # Perform setup for each of the watched folders.
 
            for path in profile.dirs:
 

	
 
                # Maybe put all pre-existing files in the queue.
 
                if profile.process_existing:
 
                    filemon.queue_existing(profile, path)
 

	
 
                # Create a watch for the folder.
 
                log.debug("start_daemon: profile '%s' watches folder: %s" % (key, path))
 
                wm.add_watch(path, mask, proc_fun=EventHandler(profile=profile))
 

	
 
            # Create an action thread for the profile.
 
            name = 'actions-%s' % key
 
            log.debug("start_daemon: starting action thread: %s" % name)
 
            thread = threading.Thread(target=filemon.perform_actions,
 
                                      name=name, args=(profile,))
 
            thread.daemon = True
 
            thread.start()
 

	
 
        # Fire up the watchers.
 
        notifier.loop()
 

	
 

	
 
def get_daemon(pidfile=None):
 
    """
 
    Get a :class:`FileMonitorDaemon` instance.
 
    """
 

	
 
    if pidfile is None:
 
        pidfile = edbob.config.get('rattail.filemon', 'pid_path',
 
                                   default='/var/run/rattail/filemon.pid')
 
    return FileMonitorDaemon(pidfile)
 

	
 

	
 
def start_daemon(pidfile=None):
 
    """
 
    Start the file monitor daemon.
 
    """
 

	
 
    get_daemon(pidfile).start()
 

	
 

	
 
def stop_daemon(pidfile=None):
 
    """
 
    Stop the file monitor daemon.
 
    """
 

	
 
    get_daemon(pidfile).stop()
rattail/filemon/win32.py
Show inline comments
 
file renamed from rattail/filemon.py to rattail/filemon/win32.py
 
@@ -23,7 +23,7 @@
 
################################################################################
 

	
 
"""
 
``rattail.filemon`` -- Windows File Monitor
 
``rattail.filemon.win32`` -- File Monitor for Windows
 
"""
 

	
 
from edbob.filemon.win32 import FileMonitorService
0 comments (0 inline, 0 general)