| | 1 | # Copyright (c) 2007 Twisted Matrix Laboratories. |
| | 2 | # See LICENSE for details. |
| | 3 | |
| | 4 | from zope.interface import implements |
| | 5 | |
| | 6 | from twisted.plugin import IPlugin |
| | 7 | from twisted.python.reflect import namedAny |
| | 8 | from twisted.application.service import IServiceMaker |
| | 9 | from twisted.python.usage import Options |
| | 10 | |
| | 11 | |
| | 12 | class RunOptions(Options): |
| | 13 | """ |
| | 14 | Options for runner. |
| | 15 | """ |
| | 16 | synopsis = "package [package options]" |
| | 17 | |
| | 18 | def parseArgs(self, runner, *opts): |
| | 19 | """ |
| | 20 | Parse command arguments: the service, and its options. |
| | 21 | """ |
| | 22 | self['runner'] = runner |
| | 23 | self['runner_options'] = opts |
| | 24 | |
| | 25 | |
| | 26 | class RunPlugin(object): |
| | 27 | """ |
| | 28 | A plugin for running other services. |
| | 29 | """ |
| | 30 | implements(IPlugin, IServiceMaker) |
| | 31 | |
| | 32 | name = "Twisted Run Plugin" |
| | 33 | description = "A plugin to run services" |
| | 34 | tapname = "run" |
| | 35 | |
| | 36 | def options(self): |
| | 37 | """ |
| | 38 | Return current options for runner. |
| | 39 | """ |
| | 40 | return RunOptions() |
| | 41 | |
| | 42 | def makeService(self, options): |
| | 43 | """ |
| | 44 | If runner is defined, try to launch it. |
| | 45 | """ |
| | 46 | if options['runner']: |
| | 47 | runner = namedAny(options['runner']) |
| | 48 | if hasattr(runner, 'options'): |
| | 49 | subOptions = runner.options() |
| | 50 | else: |
| | 51 | # Compatibility with tap module |
| | 52 | subOptions = runner.Options() |
| | 53 | subOptions.parseOptions(options['runner_options']) |
| | 54 | return namedAny(options['runner']).makeService(subOptions) |
| | 55 | |
| | 56 | |
| | 57 | run = RunPlugin() |
| | 58 | |