ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/WEBTOOLS/cmsWeb.py
(Generate patch)

Comparing COMP/WEBTOOLS/cmsWeb.py (file contents):
Revision 1.6 by eulisse, Thu Jan 31 18:08:33 2008 UTC vs.
Revision 1.9 by eulisse, Tue Mar 4 11:59:34 2008 UTC

# Line 1 | Line 1
1   #!/usr/bin/env python
2   from Framework import BonsaiServer
3   from Framework import Context
4 + from Framework.Logger import Logger
5 + from Framework.Logger import g_Logger
6   from optparse import OptionParser
7   from Framework import CmdLineArgs
8   import sys
# Line 15 | Line 17 | class Cfg:
17          self.installRoot = __file__.rsplit ("/", 1)[0]
18  
19   class CommandFactory (object):
20 <    def __init__ (self, context):
20 >    def __init__ (self, context, opts, args):
21          self.context = context
22          self.registry = {"start": StartCommand,
23                           "status": StatusCommand,
24                           "stop": StopCommand}
25 <        self.opts, self.args = self.context.OptionParser ().parse_args ()
25 >        self.opts, self.args = opts, args
26      
27      def createByName (self, name):
28          try:
# Line 73 | Line 75 | class StatusCommand (Command):
75   class StopCommand (Command):
76      def run (self):
77          filename = abspath (self.opts.pidFile)
78 <        username = getpass.getuser()
78 >        username = getpass.getuser ()
79          
80          try:
81              pid = getPidFromFile (filename)
# Line 90 | Line 92 | class StopCommand (Command):
92              else:
93                  print "Pid %s does not exists. Please remove the lock file %s." % (pid, filename)
94          except IOError:
95 <            print "File %s does not exists." % opts.pidFile
95 >            print "File %s does not exists." % self.opts.pidFile
96              print "Cannot detect status."
97              sys.exit (2)
98          
# Line 98 | Line 100 | class StopCommand (Command):
100   class StartCommand (Command):
101      def run (self):
102          app = BonsaiServer (self.context)
103 +        opts, args = self.context.OptionParser ().parse_args ()
104          filename = abspath (self.opts.pidFile)
105          try:
106              pid = getPidFromFile (filename)
# Line 109 | Line 112 | class StartCommand (Command):
112                  os.unlink (filename)
113          except IOError:
114              # TODO: this should be a warning.
115 <            print "File %s does not exists. Will be created." % filename
115 >            g_Logger.trace ("File %s does not exists. Will be created." % filename)
116          
117          open (filename, 'w').write (str (os.getpid ()))
118 <        self.context.addService (CmdLineArgs (context.OptionParser ()))
118 >        self.context.addService (CmdLineArgs (self.context.OptionParser ()))
119          self.context.addService (Cfg ())
120          if opts.profile:
121              import pstats
# Line 138 | Line 141 | def getValidOptions (args):
141      validArguments = ["start",
142                        "stop",
143                        "restart"]
144 <    validOptions = ["--cfg"]
144 >    validOptions = ["--cfg", "--force-kill", "--pid-file",
145 >                    "--log-file", "--log-level"]
146  
147      result = []
148      for i in range (0, len (args)):
# Line 151 | Line 155 | def getValidOptions (args):
155          if option in validOptions:
156              result.append (option)
157              result.append (args[i+1])
158 +    return result
159  
160 < if __name__ == '__main__':
161 <    context = Context ()
162 <    context.addService (OptionParser ())
163 <    parser = context.OptionParser ()
164 <    parser.add_option ("--profile",
165 <                       help="start server in profiler mode",
166 <                       default=False,
167 <                       action="store_true",
168 <                       dest="profile")
169 <    def stripTrailingSlash (option, opt_str, value, parser, *args, **kwargs):
170 <        setattr(parser.values, option.dest, value.rstrip ("/"))
171 <        
172 <    parser.add_option ("--base-url",
173 <                       help="Base URL for the server (for usage behind a proxy).",
174 <                       default="http://localhost:8030",
175 <                       dest="baseUrl",
176 <                       action="callback",
177 <                       callback=stripTrailingSlash,
178 <                       type="str",
179 <                       nargs=1)
160 > class CmsWebApplication (object):
161 >    def __init__ (self):
162 >        self.context = Context ()
163 >        self.context.addService (OptionParser ())
164 >        self.parser = self.context.OptionParser ()
165 >        self.__addOptions ()
166 >        
167 >    def __addOptions (self):
168 >        self.parser.add_option ("--profile",
169 >                           help="start server in profiler mode",
170 >                           default=False,
171 >                           action="store_true",
172 >                           dest="profile")
173 >
174 >        self.parser.add_option ("--pid-file",
175 >                           help="File in which it is specified the pid of wanted instance",
176 >                           default="pid.txt",
177 >                           dest="pidFile",
178 >                           metavar="FILE")
179 >
180 >        self.parser.add_option ("--force-kill",
181 >                           help="Uses SIGKILL rather than SIGTERM",
182 >                           default=False,
183 >                           action="store_true",
184 >                           dest="forceKill",
185 >                           metavar="FILE")
186 >                          
187 >        def openFilename (option, opt_str, value, parser, *args, **kwargs):
188 >            try:
189 >                f=open (value, 'a')
190 >            except IOError:
191 >                print "WARNING: Unable to open log file %s. Using stderr." % value
192 >                f=sys.stderr
193 >            setattr (parser.values, option.dest, f)
194 >        
195 >        self.parser.add_option ("--log-file",
196 >                           help="FILE to which redirect log messages",
197 >                           dest="logFile",
198 >                           default=sys.stderr,
199 >                           action="callback",
200 >                           callback=openFilename,
201 >                           metavar="FILENAME",
202 >                           type="str",
203 >                           nargs=1)
204 >                          
205 >        self.parser.add_option ("--log-level",
206 >                            help="detail LEVEL for the main log",
207 >                            dest="logLevel",
208 >                            default=10,
209 >                            metavar="LEVEL",
210 >                            type="int")
211      
212 <    parser.add_option ("--pid-file",
213 <                       help="File in which it is specified the pid of wanted instance",
214 <                       default="pid.txt",
215 <                       dest="pidFile",
216 <                       metavar="FILE")
217 <
218 <    parser.add_option ("--force-kill",
219 <                       help="Uses SIGKILL rather than SIGTERM",
220 <                       default=False,
221 <                       action="store_true",
222 <                       dest="forceKill",
223 <                       metavar="FILE")
224 <
212 >    def run (self):
213 >        if "--help" in sys.argv:
214 >            g_Logger.detailLevel = -100
215 >        validOptions = getValidOptions (sys.argv)
216 >
217 >        opts, args = self.parser.parse_args (args=validOptions)
218 >
219 >        g_Logger.stream = opts.logFile
220 >        if "--help" not in sys.argv:
221 >            g_Logger.detailLevel = opts.logLevel
222 >        
223 >        if not len (args):
224 >            args = ["start"]
225 >        
226 >        factory = CommandFactory (self.context, opts, args)
227 >        startCommand = factory.createByName (args[0])
228 >        if not startCommand:
229 >            "Command %s not known." % args[0]
230 >            sys.exit (1)
231 >        startCommand.run ()
232 >        startCommand.finish ()
233  
234 <    validOptions = getValidOptions (sys.argv)
235 <    
236 <    opts, args = parser.parse_args (args=validOptions)
193 <    if not len (args):
194 <        args = ["start"]
195 <    factory = CommandFactory (context)
196 <    startCommand = factory.createByName (args[0])
197 <    if not startCommand:
198 <        print "Command %s not known." % args[0]
199 <        sys.exit (1)
200 <    startCommand.run ()
201 <    startCommand.finish ()
234 > if __name__ == '__main__':
235 >    app = CmsWebApplication ()
236 >    app.run ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines