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.4 by eulisse, Fri May 18 08:47:48 2007 UTC vs.
Revision 1.5 by eulisse, Thu Jan 31 16:24:50 2008 UTC

# Line 3 | Line 3 | from Framework import BonsaiServer
3   from Framework import Context
4   from optparse import OptionParser
5   from Framework import CmdLineArgs
6 + import sys
7  
8   class Cfg:
9      def __init__ (self):
10          # TODO: make it a property.
11          self.installRoot = __file__.rsplit ("/", 1)[0]
12  
13 + class CommandFactory (object):
14 +    def __init__ (self, context):
15 +        self.context = context
16 +        self.registry = {"start": StartCommand}
17 +        pass
18      
19 +    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   if __name__ == '__main__':
79      context = Context ()
80      context.addService (OptionParser ())
81 <    context.OptionParser ().add_option ("--profile",
82 <                                        help="start server in profiler mode",
83 <                                        default=False,
84 <                                        action="store_true",
85 <                                        dest="profile")
81 >    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      def stripTrailingSlash (option, opt_str, value, parser, *args, **kwargs):
88          setattr(parser.values, option.dest, value.rstrip ("/"))
89          
90 <    context.OptionParser ().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)
32 <    
33 <    app = BonsaiServer (context)
34 <    
35 <    opts, args = context.OptionParser ().parse_args ()
36 <    context.addService (CmdLineArgs (context.OptionParser ()))
37 <    context.addService (Cfg ())
90 >    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 <    if opts.profile:
100 <        import pstats
101 <        try:
102 <            import cProfile as profile
103 <        except ImportError:
104 <            import profile
105 <        profile.run ('app.start ()', 'bonsaiProfiler')
106 <        
107 <        p = pstats.Stats('bonsaiProfiler')
108 <        p.strip_dirs().sort_stats(-1).print_stats()        
109 <    else:
50 <        app.start ()
99 >    validOptions = getValidOptions (sys.argv)
100 >    
101 >    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 ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines