diff --git a/rattail/commands.py b/rattail/commands.py index 5dc1dfe2e62e4173a46b5c36054196265c86aab5..7f1d92789da0aeba6f401c08a9409fb5cf2201f9 100644 --- a/rattail/commands.py +++ b/rattail/commands.py @@ -80,22 +80,82 @@ class DatabaseSyncCommand(commands.Subcommand): dbsync.stop_daemon() -class FileMonitorCommand(commands.FileMonitorCommand): +class FileMonitorCommand(commands.Subcommand): """ Interacts with the file monitor service; called as ``rattail filemon``. + This command expects a subcommand; one of the following: - See :class:`edbob.commands.FileMonitorCommand` for more information. + * ``rattail filemon start`` + * ``rattail filemon stop`` + + On Windows platforms, the following additional subcommands are available: + + * ``rattail filemon install`` + * ``rattail filemon uninstall`` (or ``rattail filemon remove``) + + .. note:: + The Windows Vista family of operating systems requires you to launch + ``cmd.exe`` as an Administrator in order to have sufficient rights to + run the above commands. + + .. See :doc:`howto.use_filemon` for more information. """ - appname = 'rattail' + name = 'filemon' + description = "Manage the file monitor service" - def get_win32_module(self): - from rattail import filemon - return filemon + def add_parser_args(self, parser): + subparsers = parser.add_subparsers(title='subcommands') - def get_win32_service(self): - from rattail.filemon import RattailFileMonitor - return RattailFileMonitor + start = subparsers.add_parser('start', help="Start service") + start.set_defaults(subcommand='start') + stop = subparsers.add_parser('stop', help="Stop service") + stop.set_defaults(subcommand='stop') + + if 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', + help="Configure service to start automatically") + remove = subparsers.add_parser('remove', help="Uninstall (remove) service") + remove.set_defaults(subcommand='remove') + uninstall = subparsers.add_parser('uninstall', help="Uninstall (remove) service") + uninstall.set_defaults(subcommand='remove') + + def run(self, args): + if sys.platform == 'linux2': + from edbob.filemon import linux as filemon + + if args.subcommand == 'start': + filemon.start_daemon('rattail') + + elif args.subcommand == 'stop': + filemon.stop_daemon('rattail') + + elif sys.platform == 'win32': + from edbob import win32 + from rattail import filemon + from rattail.win32 import require_elevation + + require_elevation() + + # Execute typical service command. + options = [] + if args.subcommand == 'install' and args.auto_start: + options = ['--startup', 'auto'] + win32.execute_service_command(filemon, args.subcommand, *options) + + # If installing auto-start service on Windows 7, we should update + # its startup type to be "Automatic (Delayed Start)". + # TODO: Improve this check to include Vista? + if args.subcommand == 'install' and args.auto_start: + if platform.release() == '7': + name = filemon.RattailFileMonitor._svc_name_ + win32.delayed_auto_start_service(name) + + else: + sys.stderr.write("File monitor is not supported on platform: {0}\n".format(sys.platform)) + sys.exit(1) class LoadHostDataCommand(commands.Subcommand):