ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/WEBTOOLS/cmsWeb.py
Revision: 1.5
Committed: Thu Jan 31 16:24:50 2008 UTC (17 years, 3 months ago) by eulisse
Content type: text/x-python
Branch: MAIN
Changes since 1.4: +90 -31 lines
Log Message:
Command line now allows for commands (e.g. start, stop, status).
* Only start implemented.

File Contents

# User Rev Content
1 eulisse 1.1 #!/usr/bin/env python
2     from Framework import BonsaiServer
3 eulisse 1.2 from Framework import Context
4     from optparse import OptionParser
5     from Framework import CmdLineArgs
6 eulisse 1.5 import sys
7 eulisse 1.1
8 eulisse 1.3 class Cfg:
9     def __init__ (self):
10     # TODO: make it a property.
11     self.installRoot = __file__.rsplit ("/", 1)[0]
12    
13 eulisse 1.5 class CommandFactory (object):
14     def __init__ (self, context):
15     self.context = context
16     self.registry = {"start": StartCommand}
17     pass
18 eulisse 1.4
19 eulisse 1.5 def create (self, cls):
20     obj = cls ()
21     obj.context = self.context
22     return obj
23    
24     def createByName (self, name):
25     try:
26     obj = self.registry[name] ()
27     except KeyError:
28     return None
29     obj.context = self.context
30     return obj
31    
32     class Command (object):
33     def __init__ (self):
34     self.context = None
35    
36     class StartCommand (Command):
37     def __init__ (self):
38     self.context = None
39     pass
40    
41     def run (self):
42     app = BonsaiServer (self.context)
43    
44     opts, args = self.context.OptionParser ().parse_args ()
45     context.addService (CmdLineArgs (context.OptionParser ()))
46     context.addService (Cfg ())
47     if opts.profile:
48     import pstats
49     try:
50     import cProfile as profile
51     except ImportError:
52     import profile
53     profile.run ('app.start ()', 'bonsaiProfiler')
54    
55     p = pstats.Stats('bonsaiProfiler')
56     p.strip_dirs().sort_stats(-1).print_stats()
57     else:
58     app.start ()
59    
60     def getValidOptions (args):
61     validArguments = ["start",
62     "stop",
63     "restart"]
64     validOptions = ["--cfg"]
65    
66     result = []
67     for i in range (0, len (args)):
68     arg = sys.argv[i]
69     if arg in validArguments:
70     result.append (args[i])
71    
72     for i in range (0, len (args)):
73     option = args[i]
74     if option in validOptions:
75     result.append (option)
76     result.append (args[i+1])
77    
78 eulisse 1.1 if __name__ == '__main__':
79 eulisse 1.2 context = Context ()
80     context.addService (OptionParser ())
81 eulisse 1.5 parser = context.OptionParser ()
82     parser.add_option ("--profile",
83     help="start server in profiler mode",
84     default=False,
85     action="store_true",
86     dest="profile")
87 eulisse 1.4 def stripTrailingSlash (option, opt_str, value, parser, *args, **kwargs):
88     setattr(parser.values, option.dest, value.rstrip ("/"))
89    
90 eulisse 1.5 parser.add_option ("--base-url",
91     help="Base URL for the server (for usage behind a proxy).",
92     default="http://localhost:8030",
93     dest="baseUrl",
94     action="callback",
95     callback=stripTrailingSlash,
96     type="str",
97     nargs=1)
98    
99     validOptions = getValidOptions (sys.argv)
100 eulisse 1.2
101 eulisse 1.5 opts, args = parser.parse_args (args=validOptions)
102     if not len (args):
103     args = ["start"]
104     factory = CommandFactory (context)
105     startCommand = factory.createByName (args[0])
106     if not startCommand:
107     print "Command %s not known." % args[0]
108     sys.exit (1)
109     startCommand.run ()