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 sys 
 23  import re 
 24   
 25  from twisted.internet import reactor 
 26   
 27  from flumotion.admin.admin import AdminModel 
 28  from flumotion.admin import connections 
 29  from flumotion.common import log, errors 
 30  # make Message proxyable 
 31  from flumotion.common import messages 
 32  from flumotion.configure import configure 
 33  from flumotion.twisted import pb as fpb 
 34   
 35  from flumotion.admin.command.commands import commands 
 36  from flumotion.common.options import OptionParser 
 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 def print_traceback(failure): 118 import traceback 119 warn('Operation %s failed:' % op) 120 traceback.print_exc() 121 return failure
122 d = proc(model, quit, *vals) 123 d.addErrback(print_traceback) 124 return d 125 126 return command 127
128 -def setup_reactor(info):
129 model = AdminModel() 130 d = model.connectToManager(info) 131 132 def failed(failure): 133 if failure.check(errors.ConnectionRefusedError): 134 print "Manager refused connection. Check your user and password." 135 elif failure.check(errors.ConnectionFailedError): 136 message = "".join(failure.value.args) 137 print "Connection to manager failed: %s" % message 138 else: 139 print ("Exception while connecting to manager: %s" 140 % log.getFailureMessage(failure)) 141 return failure
142 143 d.addErrback(failed) 144 145 return d 146 147 pat = re.compile('^(([^:@]*)(:([^:@]+))?@)?([^:@]+)(:([0-9]+))?$') 148
149 -def main(args):
150 parser = OptionParser(domain="flumotion-command") 151 parser.add_option('-u', '--usage', 152 action="store_true", dest="usage", 153 help="show a usage message") 154 parser.add_option('-m', '--manager', 155 action="store", type="string", dest="manager", 156 help="the manager to connect to, e.g. localhost:7531") 157 parser.add_option('', '--no-ssl', 158 action="store_true", dest="no_ssl", 159 help="disable encryption when connecting to the manager") 160 161 options, args = parser.parse_args(args) 162 163 if options.usage or not args[1:]: 164 usage(args) 165 166 connection = connections.parsePBConnectionInfo(options.manager, 167 not options.no_ssl) 168 169 command = parse_commands(args) 170 quit = lambda: reactor.callLater(0, reactor.stop) 171 172 d = setup_reactor(connection) 173 174 d.addCallback(lambda model: command(model, quit)) 175 # assume that whatever raised the error already printed -- this is a 176 # bit geto 177 d.addErrback(lambda failure: quit()) 178 179 reactor.run()
180