Cheng-Yi Chiang | 3f9cb97 | 2015-11-23 17:43:59 +0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """A simple utility to connect to Chameleond in an interactive shell.""" |
| 7 | |
| 8 | import argparse |
| 9 | import code |
| 10 | import logging |
| 11 | import readline |
| 12 | import rlcompleter |
| 13 | import xmlrpclib |
| 14 | |
| 15 | |
| 16 | def ShowMessages(proxy): |
| 17 | """Shows the messages for usage. |
| 18 | |
| 19 | Args: |
| 20 | proxy: The xmlrpclib.ServerProxy to chameleond. |
| 21 | """ |
| 22 | logging.info('In interactive shell, p is the proxy to chameleond server') |
| 23 | supported_ports = proxy.GetSupportedPorts() |
| 24 | linein_port = None |
| 25 | hdmi_port = None |
| 26 | port_messages = [] |
| 27 | for port in supported_ports: |
| 28 | port_type = proxy.GetConnectorType(port) |
| 29 | if port_type == 'LineIn': |
| 30 | linein_port = port |
| 31 | if port_type == 'HDMI': |
| 32 | hdmi_port = port |
| 33 | port_messages.append('Port %d is %s.' % (port, port_type)) |
| 34 | |
| 35 | logging.info(''' |
| 36 | %s |
| 37 | E.g. |
| 38 | p.StartCapturingAudio(%d) to capture from LineIn. |
| 39 | p.StopCapturingAudio(%d) to stop capturing from LineIn. |
| 40 | p.Plug(%d) to plug HDMI. |
| 41 | p.UnPlug(%d) to unplug HDMI.''', |
| 42 | '\n '.join(port_messages), linein_port, linein_port, |
| 43 | hdmi_port, hdmi_port) |
| 44 | |
| 45 | |
| 46 | def StartInteractiveShell(p): |
| 47 | """Starts an interactive shell. |
| 48 | |
| 49 | Args: |
| 50 | p: The xmlrpclib.ServerProxy to chameleond. |
| 51 | """ |
| 52 | vars = globals() |
| 53 | vars.update(locals()) |
| 54 | readline.set_completer(rlcompleter.Completer(vars).complete) |
| 55 | readline.parse_and_bind("tab: complete") |
| 56 | shell = code.InteractiveConsole(vars) |
| 57 | shell.interact() |
| 58 | |
| 59 | |
| 60 | def ParseArgs(): |
| 61 | """Parses the arguments. |
| 62 | |
| 63 | Returns: the namespace containing parsed arguments. |
| 64 | |
| 65 | """ |
| 66 | parser = argparse.ArgumentParser( |
| 67 | description='Connect to Chameleond and use interactive shell.', |
| 68 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 69 | parser.add_argument('--chameleon_host', type=str, dest='host', required=True, |
| 70 | help='host address of Chameleond') |
| 71 | parser.add_argument('--port', type=int, dest='port', default=9992, |
| 72 | help='port number of Chameleond') |
| 73 | return parser.parse_args() |
| 74 | |
| 75 | |
| 76 | def Main(): |
| 77 | """The Main program.""" |
| 78 | logging.basicConfig( |
| 79 | format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG) |
| 80 | |
| 81 | options = ParseArgs() |
| 82 | |
| 83 | address = 'http://%s:%s' % (options.host, options.port) |
| 84 | proxy = xmlrpclib.ServerProxy(address) |
| 85 | logging.info('Connected to %s with MAC address %s', |
| 86 | address, proxy.GetMacAddress()) |
| 87 | ShowMessages(proxy) |
| 88 | StartInteractiveShell(proxy) |
| 89 | |
| 90 | |
| 91 | if __name__ == '__main__': |
| 92 | Main() |