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.10 by eulisse, Thu May 29 07:33:21 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 137 | Line 140 | class StartCommand (Command):
140   def getValidOptions (args):
141      validArguments = ["start",
142                        "stop",
143 <                      "restart"]
144 <    validOptions = ["--cfg"]
143 >                      "restart",
144 >                      "status"]
145 >    validOptions = ["--cfg", "--force-kill", "--pid-file",
146 >                    "--log-file", "--log-level"]
147  
148      result = []
149      for i in range (0, len (args)):
# Line 151 | Line 156 | def getValidOptions (args):
156          if option in validOptions:
157              result.append (option)
158              result.append (args[i+1])
159 +    return result
160  
161 < if __name__ == '__main__':
162 <    context = Context ()
163 <    context.addService (OptionParser ())
164 <    parser = context.OptionParser ()
165 <    parser.add_option ("--profile",
166 <                       help="start server in profiler mode",
167 <                       default=False,
168 <                       action="store_true",
169 <                       dest="profile")
170 <    def stripTrailingSlash (option, opt_str, value, parser, *args, **kwargs):
171 <        setattr(parser.values, option.dest, value.rstrip ("/"))
172 <        
173 <    parser.add_option ("--base-url",
174 <                       help="Base URL for the server (for usage behind a proxy).",
175 <                       default="http://localhost:8030",
176 <                       dest="baseUrl",
177 <                       action="callback",
178 <                       callback=stripTrailingSlash,
179 <                       type="str",
180 <                       nargs=1)
161 > class CmsWebApplication (object):
162 >    def __init__ (self):
163 >        self.context = Context ()
164 >        self.context.addService (OptionParser ())
165 >        self.parser = self.context.OptionParser ()
166 >        self.__addOptions ()
167 >        
168 >    def __addOptions (self):
169 >        self.parser.add_option ("--profile",
170 >                           help="start server in profiler mode",
171 >                           default=False,
172 >                           action="store_true",
173 >                           dest="profile")
174 >
175 >        self.parser.add_option ("--pid-file",
176 >                           help="File in which it is specified the pid of wanted instance",
177 >                           default="pid.txt",
178 >                           dest="pidFile",
179 >                           metavar="FILE")
180 >
181 >        self.parser.add_option ("--force-kill",
182 >                           help="Uses SIGKILL rather than SIGTERM",
183 >                           default=False,
184 >                           action="store_true",
185 >                           dest="forceKill",
186 >                           metavar="FILE")
187 >                          
188 >        def openFilename (option, opt_str, value, parser, *args, **kwargs):
189 >            try:
190 >                f=open (value, 'a')
191 >            except IOError:
192 >                print "WARNING: Unable to open log file %s. Using stderr." % value
193 >                f=sys.stderr
194 >            setattr (parser.values, option.dest, f)
195 >        
196 >        self.parser.add_option ("--log-file",
197 >                           help="FILE to which redirect log messages",
198 >                           dest="logFile",
199 >                           default=sys.stderr,
200 >                           action="callback",
201 >                           callback=openFilename,
202 >                           metavar="FILENAME",
203 >                           type="str",
204 >                           nargs=1)
205 >                          
206 >        self.parser.add_option ("--log-level",
207 >                            help="detail LEVEL for the main log",
208 >                            dest="logLevel",
209 >                            default=10,
210 >                            metavar="LEVEL",
211 >                            type="int")
212      
213 <    parser.add_option ("--pid-file",
214 <                       help="File in which it is specified the pid of wanted instance",
215 <                       default="pid.txt",
216 <                       dest="pidFile",
217 <                       metavar="FILE")
218 <
219 <    parser.add_option ("--force-kill",
220 <                       help="Uses SIGKILL rather than SIGTERM",
221 <                       default=False,
222 <                       action="store_true",
223 <                       dest="forceKill",
224 <                       metavar="FILE")
225 <
213 >    def run (self):
214 >        if "--help" in sys.argv:
215 >            g_Logger.detailLevel = -100
216 >        validOptions = getValidOptions (sys.argv)
217 >
218 >        opts, args = self.parser.parse_args (args=validOptions)
219 >
220 >        g_Logger.stream = opts.logFile
221 >        if "--help" not in sys.argv:
222 >            g_Logger.detailLevel = opts.logLevel
223 >        
224 >        if not len (args):
225 >            args = ["start"]
226 >        
227 >        factory = CommandFactory (self.context, opts, args)
228 >        startCommand = factory.createByName (args[0])
229 >        if not startCommand:
230 >            "Command %s not known." % args[0]
231 >            sys.exit (1)
232 >        startCommand.run ()
233 >        startCommand.finish ()
234  
235 <    validOptions = getValidOptions (sys.argv)
236 <    
237 <    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 ()
235 > if __name__ == '__main__':
236 >    app = CmsWebApplication ()
237 >    app.run ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines