1#!/usr/bin/env python3.4 2# 3# Copyright 2016 Google Inc. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16"""Tool to interactively call methods of code snippets. 17 18Mobly Code Snippet Lib (https://github.com/google/mobly-snippet-lib/) is a 19library for triggering custom actions on Android devices by means of an RPC 20service. 21 22Usage: 23$ snippet_shell com.my.package.snippets 24>>> s.mySnippet('example') 25u'You said: example' 26""" 27 28import argparse 29import logging 30import sys 31 32from mobly.controllers import android_device 33from mobly.controllers.android_device_lib import jsonrpc_shell_base 34 35 36class SnippetShell(jsonrpc_shell_base.JsonRpcShellBase): 37 38 def __init__(self, package): 39 self._package = package 40 41 def _start_services(self, console_env): 42 """Overrides superclass.""" 43 self._ad.load_snippet(name='snippet', package=self._package) 44 console_env['snippet'] = self._ad.snippet 45 console_env['s'] = self._ad.snippet 46 47 def _get_banner(self, serial): 48 lines = [ 49 'Connected to %s.' % serial, 50 'Call methods against:', 51 ' ad (android_device.AndroidDevice)', 52 ' snippet or s (Snippet)', 53 ] 54 return '\n'.join(lines) 55 56 57if __name__ == '__main__': 58 parser = argparse.ArgumentParser( 59 description='Interactive client for Mobly code snippets.' 60 ) 61 parser.add_argument( 62 '-s', 63 '--serial', 64 help='Device serial to connect to (if more than one device is connected)', 65 ) 66 parser.add_argument( 67 'package', 68 metavar='PACKAGE_NAME', 69 type=str, 70 nargs='?', 71 help='The package name of the snippet to use.', 72 ) 73 parser.add_argument( 74 '--mbs', 75 help='Whether to connect to Mobly Bundled Snippets', 76 action='store_true', 77 ) 78 args = parser.parse_args() 79 if args.package and args.mbs: 80 print('Cannot specify both --package and --mbs', file=sys.stderr) 81 sys.exit(1) 82 if args.mbs: 83 package = android_device.MBS_PACKAGE 84 else: 85 package = args.package 86 87 logging.basicConfig(level=logging.INFO) 88 SnippetShell(package).main(args.serial) 89