Mike Truty | 140757a | 2011-09-08 16:01:55 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Mike Frysinger | c92df47 | 2019-03-01 15:05:41 -0500 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Mike Truty | cfe7697 | 2012-04-23 20:25:46 -0700 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Mike Truty | 140757a | 2011-09-08 16:01:55 -0700 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Simple command line interface for chromeos gesture developers. |
| 8 | |
| 9 | This is heavily dependent/copied on the gsutil cli interface to Google Storage. |
| 10 | http://code.google.com/apis/storage/docs/gsutil.html |
| 11 | Think of it as a version of gsutil with assumptions for more guided use. |
| 12 | |
| 13 | Look here for documentation on Google Storage: |
| 14 | http://code.google.com/apis/storage/docs/gsmanager.html |
| 15 | """ |
| 16 | |
| 17 | import logging |
| 18 | import pprint |
| 19 | import signal |
| 20 | import sys |
| 21 | |
Mike Truty | cfe7697 | 2012-04-23 20:25:46 -0700 | [diff] [blame] | 22 | import cros_gestures_constants |
Mike Truty | 140757a | 2011-09-08 16:01:55 -0700 | [diff] [blame] | 23 | import cros_gestures_logging |
| 24 | import cros_gestures_utils |
| 25 | from exception import CrosGesturesException |
| 26 | |
| 27 | |
| 28 | color = cros_gestures_utils.Color() |
| 29 | LOG = logging.getLogger('cros_gestures') |
| 30 | |
| 31 | |
| 32 | #------------------------------------------------------------------------------ |
| 33 | # Setup gslib and boto by identifying gsutil/boto dirs and .boto location. |
| 34 | #------------------------------------------------------------------------------ |
| 35 | gsutil_bin_dir, boto_config = cros_gestures_utils.FindDependencies() |
| 36 | |
| 37 | import boto |
| 38 | boto.UserAgent += '/cros_gestures' |
| 39 | from boto.exception import GSResponseError |
| 40 | from boto.exception import InvalidUriError |
| 41 | |
| 42 | # We don't use the oauth2 authentication plugin directly; importing it here |
| 43 | # ensures that it's loaded and available by default. |
| 44 | _HAVE_OAUTH2 = False |
| 45 | try: |
| 46 | from oauth2_plugin import oauth2_plugin |
| 47 | _HAVE_OAUTH2 = True |
| 48 | except ImportError: |
| 49 | pass |
| 50 | |
| 51 | import cros_gestures_commands |
| 52 | import cros_gestures_options |
| 53 | |
| 54 | |
| 55 | #------------------------------------------------------------------------------ |
| 56 | # Declare custom commands. |
| 57 | #------------------------------------------------------------------------------ |
| 58 | commands = {'cat': None, 'download': None, 'ls': None, |
| 59 | 'rm': None, 'upload': None, 'ver': None} |
| 60 | COMMANDS_STRING = ', '.join(sorted(commands.keys())) |
| 61 | USAGE_STRING = ( |
| 62 | 'cros_gestures [options] %(command)s\n%(command)s from: %(commands)s.\n\n' |
| 63 | 'The default behavior is to group uploaded files under ' |
| 64 | '%(gs_base)s/model/user/".\nCommon command examples include:\n\n' |
| 65 | '%(cat)s gesture_file [emits contents of %(gs_base)s/user/gesture_file]\n' |
| 66 | '%(download)s gesture_file [copies %(gs_base)s/user/gesture_file to ' |
| 67 | 'current dir]\n' |
| 68 | '%(ls)s [show all files under %(gs_base)s/user]\n' |
| 69 | '%(ls)s -L [show all files with attributes under %(gs_base)s/user]\n' |
| 70 | '%(upload)s dir/gesture_file [strip dir & copy gesture_file to ' |
| 71 | '%(gs_base)s/user/gesture_file]\n' |
| 72 | '%(ver)s [show tool version numbers]' % { |
| 73 | 'command': color.Color(cros_gestures_utils.Color.BLUE, 'command'), |
| 74 | 'commands': color.Color(cros_gestures_utils.Color.BOLD, |
| 75 | COMMANDS_STRING), |
| 76 | 'cat': color.Color(cros_gestures_utils.Color.BOLD, 'cat'), |
| 77 | 'download': color.Color(cros_gestures_utils.Color.BOLD, 'download'), |
| 78 | 'ls': color.Color(cros_gestures_utils.Color.BOLD, 'ls'), |
| 79 | 'upload': color.Color(cros_gestures_utils.Color.BOLD, 'upload'), |
| 80 | 'ver': color.Color(cros_gestures_utils.Color.BOLD, 'ver'), |
| 81 | 'gs_base': 'gs://chromeos-gestures-trusted-dev'} |
| 82 | ) |
| 83 | |
| 84 | |
| 85 | # If user enters no commands just print the usage info. |
| 86 | if len(sys.argv) == 1: |
| 87 | cros_gestures_utils.OutputAndExit(USAGE_STRING) |
| 88 | options, args, command_string = cros_gestures_options.ParseArgs(USAGE_STRING, |
| 89 | commands) |
| 90 | cros_gestures_logging.SetupLogging(options) |
Mike Truty | cfe7697 | 2012-04-23 20:25:46 -0700 | [diff] [blame] | 91 | if cros_gestures_constants.debug > 1: |
Mike Truty | 140757a | 2011-09-08 16:01:55 -0700 | [diff] [blame] | 92 | LOG.debug('Using\n\tGSUTIL_BIN_DIR=%s\n\tBOTO_CONFIG=%s.', gsutil_bin_dir, |
| 93 | boto_config) |
| 94 | LOG.debug('Accepted funtionalities:\n%s.', |
| 95 | pprint.pformat(options.config_options)) |
| 96 | |
| 97 | #------------------------------------------------------------------------------ |
| 98 | # Define custom commands. |
| 99 | #------------------------------------------------------------------------------ |
| 100 | _command_inst = cros_gestures_commands.GestureCommand(gsutil_bin_dir) |
| 101 | |
| 102 | |
| 103 | NO_MAX = sys.maxint |
| 104 | # [command_function, min # args, max # args, file_uri_ok, gs_uri_ok] |
| 105 | commands.update({ |
| 106 | 'cat': [_command_inst.CatGestureCommand, 1, NO_MAX, True, True], |
| 107 | 'download': [_command_inst.DownloadGestureCommand, 1, NO_MAX, True, |
| 108 | False], |
| 109 | 'ls': [_command_inst.ListGesturesCommand, 0, NO_MAX, True, True], |
| 110 | 'rm': [_command_inst.RemoveGesturesCommand, 1, NO_MAX, True, True], |
| 111 | 'upload': [_command_inst.UploadGestureCommand, 1, 1, True, False], |
| 112 | 'ver': [_command_inst.VersionCommand, 0, 0, False, False]}) |
| 113 | |
| 114 | |
| 115 | #------------------------------------------------------------------------------ |
| 116 | # Main |
| 117 | #------------------------------------------------------------------------------ |
| 118 | def main(): |
| 119 | """Gesture File Command Line Interface main code.""" |
| 120 | try: |
| 121 | signal.signal(signal.SIGINT, cros_gestures_utils.HandleControlC) |
| 122 | command, min_nargs, max_nargs, file_uri, gs_uri = commands[command_string] |
| 123 | # General command validation. |
| 124 | if len(args) < min_nargs or len(args) > max_nargs: |
| 125 | raise CrosGesturesException( |
| 126 | 'Wrong number of arguments for "%s" command.' % command_string) |
| 127 | if not file_uri and cros_gestures_utils.HaveFileUris(args): |
| 128 | raise CrosGesturesException( |
| 129 | '"%s" command does not support "file://" URIs. ' |
| 130 | 'Did you mean to use a gs:// URI?' % command_string) |
| 131 | if not gs_uri and cros_gestures_commands.GestureUri.HasGSUris(args): |
| 132 | raise CrosGesturesException( |
| 133 | '"%s" command does not support gs:// URIs.' % command_string) |
| 134 | # Finally, run the command. |
| 135 | sys.exit(command(cros_gestures_utils.StripFileUris(args), options)) |
| 136 | except CrosGesturesException, e: |
| 137 | cros_gestures_utils.HandleCrosGesturesException(e) |
| 138 | |
| 139 | |
| 140 | if __name__ == '__main__': |
| 141 | main() |