Package flumotion :: Package admin :: Package command :: Module main
[hide private]

Source Code for Module flumotion.admin.command.main

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  import optparse 
 23  import sys 
 24  import re 
 25   
 26  from twisted.internet import reactor 
 27   
 28  from flumotion.admin.admin import AdminModel 
 29  from flumotion.admin import connections 
 30  from flumotion.common import log, errors 
 31  # make Message proxyable 
 32  from flumotion.common import messages 
 33  from flumotion.configure import configure 
 34  from flumotion.twisted import pb as fpb 
 35   
 36  from flumotion.admin.command.commands import commands 
 37   
38 -def err(string):
39 sys.stderr.write('Error: ' + string + '\n') 40 sys.exit(1)
41
42 -def warn(string):
43 sys.stderr.write('Warning: ' + string + '\n')
44
45 -def command_usage():
46 for name, desc, argspecs, proc in commands: 47 sys.stdout.write(' %s -- %s\n' % (name, desc)) 48 sys.stdout.write(' usage: %s' % name) 49 for spec in argspecs: 50 if len(spec) > 2: 51 sys.stdout.write(' [%s]' % spec[0].upper()) 52 else: 53 sys.stdout.write(' %s' % spec[0].upper()) 54 sys.stdout.write('\n')
55
56 -def usage(args, exitval=0):
57 print 'usage: %s [OPTIONS] -m MANAGER COMMAND COMMAND-ARGS...' % args[0] 58 print '' 59 print 'Available commands:' 60 print '' 61 command_usage() 62 print '' 63 print 'See %s -h for help on the available options.' % args[0] 64 sys.exit(exitval)
65
66 -def parse_commands(args):
67 op = args[1] 68 matching = [x for x in commands if x[0] == op] 69 if not matching: 70 print 'Error: Unknown command: %s' % op 71 usage(args, exitval=1) 72 commandspec = matching[0] 73 74 argspecs = commandspec[2] 75 reqspecs = [spec for spec in argspecs if len(spec) < 3] 76 nreq = len(reqspecs) 77 optspecs = [spec for spec in argspecs if len(spec) == 3 or \ 78 len(spec) > 3 and not spec[3]] 79 nopt = len(optspecs) 80 81 vararg = filter(lambda spec: len(spec) > 3 and spec[3], argspecs) 82 83 # pop off argv[0] and the command name 84 cargs = args[2:] 85 86 if len(cargs) < nreq or len(cargs) > nreq + nopt and not vararg: 87 print 'Error: Invalid arguments to operation %s: %r' % (op, cargs) 88 usage(args, exitval=1) 89 90 vals = [] 91 for name, parse in reqspecs: 92 arg = cargs.pop(0) 93 try: 94 vals.append(parse(arg)) 95 except Exception: 96 err('Error: Operation %s\'s arg %s="%s" could not be ' 97 'parsed as type "%s"' 98 % (op, name, arg, parse.__name__)) 99 for name, parse, default in optspecs: 100 if cargs: 101 arg = cargs.pop(0) 102 try: 103 vals.append(parse(arg)) 104 except Exception: 105 err('Error: Operation %s\'s arg %s="%s" could not be ' 106 'parsed as type "%s"' 107 % (op, name, arg, parse.__name__)) 108 else: 109 vals.append(default) 110 111 if vararg: 112 vals.extend(cargs) 113 114 proc = commandspec[3] 115 116 def command(model, quit): 117 proc(model, quit, *vals)
118 119 return command 120
121 -def setup_reactor(info):
122 model = AdminModel(info.authenticator) 123 d = model.connectToHost(info.host, info.port, not info.use_ssl) 124 125 def failed(failure): 126 if failure.check(errors.ConnectionRefusedError): 127 print "Manager refused connection. Check your user and password." 128 elif failure.check(errors.ConnectionFailedError): 129 message = "".join(failure.value.args) 130 print "Connection to manager failed: %s" % message 131 else: 132 print ("Exception while connecting to manager: %s" 133 % log.getFailureMessage(failure)) 134 return failure
135 136 d.addErrback(failed) 137 138 return d 139 140 pat = re.compile('^(([^:@]*)(:([^:@]+))?@)?([^:@]+)(:([0-9]+))?$') 141
142 -def main(args):
143 parser = optparse.OptionParser() 144 parser.add_option('-d', '--debug', 145 action="store", type="string", dest="debug", 146 help="set debug levels") 147 parser.add_option('-u', '--usage', 148 action="store_true", dest="usage", 149 help="show a usage message") 150 parser.add_option('-m', '--manager', 151 action="store", type="string", dest="manager", 152 help="the manager to connect to, e.g. localhost:7531") 153 parser.add_option('', '--no-ssl', 154 action="store_true", dest="no_ssl", 155 help="disable encryption when connecting to the manager") 156 parser.add_option('', '--version', 157 action="store_true", dest="version", 158 default=False, 159 help="show version information") 160 161 options, args = parser.parse_args(args) 162 163 if options.version: 164 from flumotion.common import common 165 print common.version("flumotion-command") 166 return 0 167 168 if options.debug: 169 log.setFluDebug(options.debug) 170 171 if options.usage or not args[1:]: 172 usage(args) 173 174 connection = connections.parsePBConnectionInfo(options.manager, 175 not options.no_ssl) 176 177 command = parse_commands(args) 178 quit = lambda: reactor.callLater(0, reactor.stop) 179 180 d = setup_reactor(connection) 181 182 d.addCallback(lambda model: command(model, quit)) 183 # assume that whatever raised the error already printed -- this is a 184 # bit geto 185 d.addErrback(lambda failure: quit()) 186 187 reactor.run()
188