blob: 7a2ffc35cb5f435c9f6e0a904ae70fd4e9ab27ef [file] [log] [blame]
Mike Truty140757a2011-09-08 16:01:55 -07001#!/usr/bin/python
Mike Frysingerc92df472019-03-01 15:05:41 -05002# -*- coding: utf-8 -*-
Mike Trutycfe76972012-04-23 20:25:46 -07003# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Mike Truty140757a2011-09-08 16:01:55 -07004# 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
9This is heavily dependent/copied on the gsutil cli interface to Google Storage.
10http://code.google.com/apis/storage/docs/gsutil.html
11Think of it as a version of gsutil with assumptions for more guided use.
12
13Look here for documentation on Google Storage:
14http://code.google.com/apis/storage/docs/gsmanager.html
15"""
16
17import logging
18import pprint
19import signal
20import sys
21
Mike Trutycfe76972012-04-23 20:25:46 -070022import cros_gestures_constants
Mike Truty140757a2011-09-08 16:01:55 -070023import cros_gestures_logging
24import cros_gestures_utils
25from exception import CrosGesturesException
26
27
28color = cros_gestures_utils.Color()
29LOG = logging.getLogger('cros_gestures')
30
31
32#------------------------------------------------------------------------------
33# Setup gslib and boto by identifying gsutil/boto dirs and .boto location.
34#------------------------------------------------------------------------------
35gsutil_bin_dir, boto_config = cros_gestures_utils.FindDependencies()
36
37import boto
38boto.UserAgent += '/cros_gestures'
39from boto.exception import GSResponseError
40from 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
45try:
46 from oauth2_plugin import oauth2_plugin
47 _HAVE_OAUTH2 = True
48except ImportError:
49 pass
50
51import cros_gestures_commands
52import cros_gestures_options
53
54
55#------------------------------------------------------------------------------
56# Declare custom commands.
57#------------------------------------------------------------------------------
58commands = {'cat': None, 'download': None, 'ls': None,
59 'rm': None, 'upload': None, 'ver': None}
60COMMANDS_STRING = ', '.join(sorted(commands.keys()))
61USAGE_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.
86if len(sys.argv) == 1:
87 cros_gestures_utils.OutputAndExit(USAGE_STRING)
88options, args, command_string = cros_gestures_options.ParseArgs(USAGE_STRING,
89 commands)
90cros_gestures_logging.SetupLogging(options)
Mike Trutycfe76972012-04-23 20:25:46 -070091if cros_gestures_constants.debug > 1:
Mike Truty140757a2011-09-08 16:01:55 -070092 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
103NO_MAX = sys.maxint
104# [command_function, min # args, max # args, file_uri_ok, gs_uri_ok]
105commands.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#------------------------------------------------------------------------------
118def 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
140if __name__ == '__main__':
141 main()