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  """flumotion-command entry point, command line parsing and invokation""" 
 23   
 24  import re 
 25  import sys 
 26   
 27  from twisted.internet import reactor 
 28   
 29  from flumotion.admin.admin import AdminModel 
 30  from flumotion.admin.command.commands import commands 
 31  from flumotion.admin.connections import parsePBConnectionInfoRecent 
 32  from flumotion.common import log, errors 
 33  # make Message proxyable 
 34  from flumotion.common import messages 
 35  from flumotion.common.options import OptionParser 
 36   
 37  __version__ = "$Rev: 6961 $" 
 38   
39 -def err(string):
40 sys.stderr.write('Error: ' + string + '\n') 41 sys.exit(1)
42
43 -def warn(string):
44 sys.stderr.write('Warning: ' + string + '\n')
45
46 -def command_usage():
47 for name, desc, argspecs, proc in commands: 48 sys.stdout.write(' %s -- %s\n' % (name, desc)) 49 sys.stdout.write(' usage: %s' % name) 50 for spec in argspecs: 51 if len(spec) > 2: 52 sys.stdout.write(' [%s]' % spec[0].upper()) 53 else: 54 sys.stdout.write(' %s' % spec[0].upper()) 55 sys.stdout.write('\n')
56
57 -def usage(args, exitval=0):
58 print 'usage: %s [OPTIONS] -m MANAGER COMMAND COMMAND-ARGS...' % args[0] 59 print '' 60 print 'Available commands:' 61 print '' 62 command_usage() 63 print '' 64 print 'See %s -h for help on the available options.' % args[0] 65 sys.exit(exitval)
66
67 -def parse_commands(args):
68 op = args[1] 69 matching = [x for x in commands if x[0] == op] 70 if not matching: 71 print 'Error: Unknown command: %s' % op 72 usage(args, exitval=1) 73 commandspec = matching[0] 74 75 argspecs = commandspec[2] 76 reqspecs = [spec for spec in argspecs if len(spec) < 3] 77 nreq = len(reqspecs) 78 optspecs = [spec for spec in argspecs if len(spec) == 3 or \ 79 len(spec) > 3 and not spec[3]] 80 nopt = len(optspecs) 81 82 vararg = [spec for spec in argspecs if len(spec) > 3 and spec[3]] 83 84 # pop off argv[0] and the command name 85 cargs = args[2:] 86 87 if len(cargs) < nreq or len(cargs) > nreq + nopt and not vararg: 88 print 'Error: Invalid arguments to operation %s: %r' % (op, cargs) 89 usage(args, exitval=1) 90 91 vals = [] 92 for name, parse in reqspecs: 93 arg = cargs.pop(0) 94 try: 95 vals.append(parse(arg)) 96 except Exception: 97 err('Error: Operation %s\'s arg %s="%s" could not be ' 98 'parsed as type "%s"' 99 % (op, name, arg, parse.__name__)) 100 for name, parse, default in optspecs: 101 if cargs: 102 arg = cargs.pop(0) 103 try: 104 vals.append(parse(arg)) 105 except Exception: 106 err('Error: Operation %s\'s arg %s="%s" could not be ' 107 'parsed as type "%s"' 108 % (op, name, arg, parse.__name__)) 109 else: 110 vals.append(default) 111 112 if vararg: 113 vals.extend(cargs) 114 115 proc = commandspec[3] 116 117 def command(model, quit): 118 def print_traceback(failure): 119 import traceback 120 warn('Operation %s failed:' % op) 121 traceback.print_exc() 122 return failure
123 d = proc(model, quit, *vals) 124 d.addErrback(print_traceback) 125 return d 126 127 return command 128
129 -def setup_reactor(info):
130 model = AdminModel() 131 d = model.connectToManager(info) 132 133 def failed(failure): 134 if failure.check(errors.ConnectionRefusedError): 135 print >> sys.stderr, \ 136 "Manager refused connection. Check your user and password." 137 elif failure.check(errors.ConnectionFailedError): 138 message = "".join(failure.value.args) 139 print >> sys.stderr, "Connection to manager failed: %s" % message 140 else: 141 print >> sys.stderr, ("Exception while connecting to manager: %s" 142 % log.getFailureMessage(failure)) 143 return failure
144 145 d.addErrback(failed) 146 147 return d 148 149 pat = re.compile('^(([^:@]*)(:([^:@]+))?@)?([^:@]+)(:([0-9]+))?$') 150
151 -def main(args):
152 parser = OptionParser(domain="flumotion-command") 153 parser.add_option('-u', '--usage', 154 action="store_true", dest="usage", 155 help="show a usage message") 156 parser.add_option('-m', '--manager', 157 action="store", type="string", dest="manager", 158 help="the manager to connect to, e.g. localhost:7531") 159 parser.add_option('', '--no-ssl', 160 action="store_true", dest="no_ssl", 161 help="disable encryption when connecting to the manager") 162 163 options, args = parser.parse_args(args) 164 165 if options.usage or not args[1:]: 166 usage(args) 167 168 connection = parsePBConnectionInfoRecent(options.manager, 169 not options.no_ssl) 170 171 command = parse_commands(args) 172 quit = lambda: reactor.callLater(0, reactor.stop) 173 174 reactor.exitStatus = 0 175 176 d = setup_reactor(connection) 177 178 d.addCallback(lambda model: command(model, quit)) 179 # assume that whatever raised the error already printed -- this is a 180 # bit geto 181 def errback(failure): 182 reactor.exitStatus = 1 183 quit()
184 d.addErrback(errback) 185 186 reactor.run() 187 return reactor.exitStatus 188