Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: third_party/gsutil/gslib/commands/config.py

Issue 10199002: Upgrade gsutil to 3.4 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed comments Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/gsutil/gslib/commands/cat.py ('k') | third_party/gsutil/gslib/commands/cp.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2011 Google Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import boto
16 import datetime
17 import multiprocessing
18 import platform
19 import os
20 import sys
21 import time
22 import webbrowser
23
24 from boto.provider import Provider
25 from gslib.command import Command
26 from gslib.command import COMMAND_NAME
27 from gslib.command import COMMAND_NAME_ALIASES
28 from gslib.command import CONFIG_REQUIRED
29 from gslib.command import FILE_URIS_OK
30 from gslib.command import MAX_ARGS
31 from gslib.command import MIN_ARGS
32 from gslib.command import PROVIDER_URIS_OK
33 from gslib.command import SUPPORTED_SUB_ARGS
34 from gslib.command import URIS_START_ARG
35 from gslib.exception import CommandException
36 from gslib.help_provider import HELP_NAME
37 from gslib.help_provider import HELP_NAME_ALIASES
38 from gslib.help_provider import HELP_ONE_LINE_SUMMARY
39 from gslib.help_provider import HELP_TEXT
40 from gslib.help_provider import HelpType
41 from gslib.help_provider import HELP_TYPE
42 from gslib.util import HAVE_OAUTH2
43 from gslib.util import ONE_MB
44
45 _detailed_help_text = ("""
46 <B>SYNOPSIS</B>
47 gsutil [-D] config [-a] [-b] [-f] [-o <file>] [-r] [-s <scope>] [-w]
48
49
50 <B>DESCRIPTION</B>
51 The gsutil config command obtains access credentials for Google Cloud
52 Storage and writes a boto/gsutil configuration file containing the obtained
53 credentials along with a number of other configuration-controllable values.
54
55 Unless specified otherwise (see OPTIONS), the configuration file is written
56 to ~/.boto (i.e., the file .boto under the user's home directory). If the
57 default file already exists, an attempt is made to rename the existing file
58 to ~/.boto.bak; if that attempt fails the command will exit. A different
59 destination file can be specified with the -o option (see OPTIONS).
60
61 Because the boto configuration file contains your credentials you should
62 keep its file permissions set so no one but you has read access. (The file
63 is created read-only when you run gsutil config.)
64
65
66 <B>CREDENTIALS</B>
67 By default gsutil config obtains OAuth2 credentials, and writes them
68 to the [Credentials] section of the configuration file. The -r, -w,
69 -f options (see OPTIONS below) cause gsutil config to request a token
70 with restricted scope; the resulting token will be restricted to read-only
71 operations, read-write operation, or all operations (including getacl/setacl/
72 getdefacl/setdefacl/disablelogging/enablelogging/getlogging operations). In
73 addition, -s <scope> can be used to request additional (non-Google-Storage)
74 scopes.
75
76 If you want to use credentials based on access key and secret (the older
77 authentication method before OAuth2 was supported) instead of OAuth2,
78 see help about the -a option in the OPTIONS section.
79
80 If you wish to use gsutil with other providers (or to copy data back and
81 forth between multiple providers) you can edit their credentials into the
82 [Credentials] section after creating the initial configuration file.
83
84
85 <B>CONFIGURATION FILE SELECTION PROCEDURE</B>
86 By default, gsutil will look for the configuration file in /etc/boto.cfg and
87 ~/.boto. You can override this choice by setting the BOTO_CONFIG environment
88 variable. This is also useful if you have several different identities or
89 cloud storage environments: By setting up the credentials and any additional
90 configuration in separate files for each, you can switch environments by
91 changing environment variables.
92
93 You can also set up a path of configuration files, by setting the BOTO_PATH
94 environment variable to contain a ":" delimited path. For example setting
95 the BOTO_PATH environment variable to:
96
97 /etc/projects/my_group_project.boto.cfg:/home/mylogin/.boto
98
99 will cause gsutil to load each configuration file found in the path in
100 order. This is useful if you want to set up some shared configuration
101 state among many users: The shared state can go in the central shared file
102 ( /etc/projects/my_group_project.boto.cfg) and each user's individual
103 credentials can be placed in the configuration file in each of their home
104 directories. (For security reasons users should never share credentials
105 via a shared configuration file.)
106
107
108 <B>CONFIGURATION FILE STRUCTURE</B>
109 The configuration file contains a number of sections: [Credentials],
110 [Boto], [GSUtil], and [OAuth2]. If you edit the file make sure to edit the
111 appropriate section (discussed below), and to be careful not to mis-edit
112 any of the setting names (like "gs_access_key_id") and not to remove the
113 section delimiters (like "[Credentials]").
114
115
116 <B>ADDITIONAL CONFIGURATION-CONTROLLABLE FEATURES</B>
117 With the exception of setting up gsutil to work through a proxy (see
118 below), most users won't need to edit values in the boto configuration file;
119 values found in there tend to be of more specialized use than command line
120 option-controllable features.
121
122 The following are the currently defined configuration settings, broken
123 down by section. Their use is documented in comments preceding each, in
124 the configuration file. If you see a setting you want to change that's not
125 listed in your current file, see the section below on Updating to the Latest
126 Configuration File.
127
128 The currently supported settings, are, by section:
129 [Boto]
130 proxy
131 proxy_port
132 proxy_user
133 proxy_pass
134 is_secure
135 https_validate_certificates
136 debug
137 num_retries
138
139 [GSUtil]
140 resumable_threshold
141 resumable_tracker_dir
142 parallel_process_count
143 parallel_thread_count
144 default_api_version
145 default_project_id
146 use_magicfile
147
148 [OAuth2]
149 token_cache
150 token_cache
151 client_id
152 client_secret
153 provider_label
154 provider_authorization_uri
155 provider_token_uri
156
157
158 <B>UPDATING TO THE LATEST CONFIGURATION FILE</B>
159 We add new configuration controllable features to the boto configuration file
160 over time, but most gsutil users create a configuration file once and then
161 keep it for a long time, so new features aren't apparent when you update
162 to a newer version of gsutil. If you want to get the latest configuration
163 file (which includes all the latest settings and documentation about each)
164 you can rename your current file (e.g., to '.boto_old'), run gsutil config,
165 and then edit any configuration settings you wanted from your old file
166 into the newly created file. Note, however, that if you're using OAuth2
167 credentials and you go back through the OAuth2 configuration dialog it will
168 invalidate your previous OAuth2 credentials.
169
170 If no explicit scope option is given, -f (full control) is assumed by default.
171
172
173 <B>OPTIONS</B>
174 -a Prompt for Google Cloud Storage access key and secret (the older
175 authentication method before OAuth2 was supported) instead of
176 obtaining an OAuth2 token.
177
178 -b Causes gsutil config to launch a browser to obtain OAuth2 approval
179 and the project ID instead of showing the URL for each and asking
180 the user to open the browser. This will probably not work as
181 expected if you are running gsutil from an ssh window, or using
182 gsutil on Windows.
183
184 -f Request token with full-control access (default).
185
186 -o <file> Write the configuration to <file> instead of ~/.boto.
187 Use '-' for stdout.
188
189 -r Request token restricted to read-only access.
190
191 -s <scope> Request additional OAuth2 <scope>.
192
193 -w Request token restricted to read-write access.
194 """)
195
196
197 try:
198 from oauth2_plugin import oauth2_helper
199 except ImportError:
200 pass
201
202 GOOG_API_CONSOLE_URI = 'http://code.google.com/apis/console'
203
204 SCOPE_FULL_CONTROL = 'https://www.googleapis.com/auth/devstorage.full_control'
205 SCOPE_READ_WRITE = 'https://www.googleapis.com/auth/devstorage.read_write'
206 SCOPE_READ_ONLY = 'https://www.googleapis.com/auth/devstorage.read_only'
207
208 CONFIG_PRELUDE_CONTENT = """
209 # This file contains credentials and other configuration information needed
210 # by the boto library, used by gsutil. You can edit this file (e.g., to add
211 # credentials) but be careful not to mis-edit any of the variable names (like
212 # "gs_access_key_id") or remove important markers (like the "[Credentials]" and
213 # "[Boto]" section delimiters).
214 #
215 """
216
217 # Default number of OS processes and Python threads for parallel operations.
218 # On Linux systems we automatically scale the number of processes to match
219 # the underlying CPU/core count. Given we'll be running multiple concurrent
220 # processes on a typical multi-core Linux computer, to avoid being too
221 # aggressive with resources, the default number of threads is reduced from
222 # the previous value of 24 to 10.
223 # On Windows and Mac systems parallel multi-processing and multi-threading
224 # in Python presents various challenges so we retain compatibility with
225 # the established parallel mode operation, i.e. one process and 24 threads.
226 if platform.system() == 'Linux':
227 DEFAULT_PARALLEL_PROCESS_COUNT = multiprocessing.cpu_count()
228 DEFAULT_PARALLEL_THREAD_COUNT = 10
229 else:
230 DEFAULT_PARALLEL_PROCESS_COUNT = 1
231 DEFAULT_PARALLEL_THREAD_COUNT = 24
232
233 CONFIG_BOTO_SECTION_CONTENT = """
234 [Boto]
235
236 # To use a proxy, edit and uncomment the proxy and proxy_port lines. If you
237 # need a user/password with this proxy, edit and uncomment those lines as well.
238 #proxy = <proxy host>
239 #proxy_port = <proxy port>
240 #proxy_user = <your proxy user name>
241 #proxy_pass = <your proxy password>
242
243 # The following two options control the use of a secure transport for requests
244 # to S3 and Google Cloud Storage. It is highly recommended to set both options
245 # to True in production environments, especially when using OAuth2 bearer token
246 # authentication with Google Cloud Storage.
247
248 # Set 'is_secure' to False to cause boto to connect using HTTP instead of the
249 # default HTTPS. This is useful if you want to capture/analyze traffic
250 # (e.g., with tcpdump). This option should always be set to True in production
251 # environments.
252 #is_secure = False
253
254 # Set 'https_validate_certificates' to False to disable server certificate
255 # checking. This is useful if you want to capture/analyze traffic using an
256 # intercepting proxy. This option should always be set to True in production
257 # environments.
258 # In gsutil, the default for this option is True. *However*, the default for
259 # this option in the boto library itself is currently 'False'; it is therefore
260 # recommended to always set this option explicitly to True in configuration
261 # files.
262 https_validate_certificates = True
263
264 # 'debug' controls the level of debug messages printed: 0 for none, 1
265 # for basic boto debug, 2 for all boto debug plus HTTP requests/responses.
266 # Note: 'gsutil -d' sets debug to 2 for that one command run.
267 #debug = <0, 1, or 2>
268
269 # 'num_retries' controls the number of retry attempts made when errors occur.
270 # The default is 6. Note: don't set this value to 0, as it will cause boto to
271 # fail when reusing HTTP connections.
272 #num_retries = <integer value>
273 """
274
275 CONFIG_INPUTLESS_GSUTIL_SECTION_CONTENT = """
276 [GSUtil]
277
278 # 'resumable_threshold' specifies the smallest file size [bytes] for which
279 # resumable Google Cloud Storage transfers are attempted. The default is 1048576
280 # (1MB).
281 #resumable_threshold = %(resumable_threshold)d
282
283 # 'resumable_tracker_dir' specifies the base location where resumable
284 # transfer tracker files are saved. By default they're in ~/.gsutil
285 #resumable_tracker_dir = <file path>
286
287 # 'parallel_process_count' and 'parallel_thread_count' specify the number
288 # of OS processes and Python threads, respectively, to use when executing
289 # operations in parallel. The default settings should work well as configured,
290 # however, to enhance performance for transfers involving large numbers of
291 # files, you may experiment with hand tuning these values to optimize
292 # performance for your particular system configuration.
293 # MacOS and Windows users should see
294 # http://code.google.com/p/gsutil/issues/detail?id=78 before attempting
295 # to experiment with these values.
296 #parallel_process_count = %(parallel_process_count)d
297 #parallel_thread_count = %(parallel_thread_count)d
298
299 # 'use_magicfile' specifies if the 'file --mime-type <filename>' command should
300 # be used to guess MIME types instead of the default filename extension-based
301 # mechanism. Available on UNIX and MacOS (and possibly on Windows, if you're
302 # running Cygwin or some other package that provides implementations of
303 # UNIX-like commands). When available and enabled use_magicfile should be more
304 # robust because it analyzes file contents in addition to extensions.
305 #use_magicfile = False
306
307 """ % {'resumable_threshold': ONE_MB,
308 'parallel_process_count': DEFAULT_PARALLEL_PROCESS_COUNT,
309 'parallel_thread_count': DEFAULT_PARALLEL_THREAD_COUNT}
310
311 CONFIG_OAUTH2_CONFIG_CONTENT = """
312 [OAuth2]
313 # This section specifies options used with OAuth2 authentication.
314
315 # 'token_cache' specifies how the OAuth2 client should cache access tokens.
316 # Valid values are:
317 # 'in_memory': an in-memory cache is used. This is only useful if the boto
318 # client instance (and with it the OAuth2 plugin instance) persists
319 # across multiple requests.
320 # 'file_system' : access tokens will be cached in the file system, in files
321 # whose names include a key derived from the refresh token the access token
322 # based on.
323 # The default is 'file_system'.
324 #token_cache = file_system
325 #token_cache = in_memory
326
327 # 'token_cache_path_pattern' specifies a path pattern for token cache files.
328 # This option is only relevant if token_cache = file_system.
329 # The value of this option should be a path, with place-holders '%(key)s' (which
330 # will be replaced with a key derived from the refresh token the cached access
331 # token was based on), and (optionally), %(uid)s (which will be replaced with
332 # the UID of the current user, if available via os.getuid()).
333 # Note that the config parser itself interpolates '%' placeholders, and hence
334 # the above placeholders need to be escaped as '%%(key)s'.
335 # The default value of this option is
336 # token_cache_path_pattern = <tmpdir>/oauth2client-tokencache.%%(uid)s.%%(key)s
337 # where <tmpdir> is the system-dependent default temp directory.
338
339 # The following options specify the OAuth2 client identity and secret that is
340 # used when requesting and using OAuth2 tokens. If not specified, a default
341 # OAuth2 client for the gsutil tool is used; for uses of the boto library (with
342 # OAuth2 authentication plugin) in other client software, it is recommended to
343 # use a tool/client-specific OAuth2 client. For more information on OAuth2, see
344 # http://code.google.com/apis/accounts/docs/OAuth2.html
345 #client_id = <OAuth2 client id>
346 #client_secret = <OAuth2 client secret>
347
348 # The following options specify the label and endpoint URIs for the OAUth2
349 # authorization provider being used. Primarily useful for tool developers.
350 #provider_label = Google
351 #provider_authorization_uri = https://accounts.google.com/o/oauth2/auth
352 #provider_token_uri = https://accounts.google.com/o/oauth2/token
353 """
354
355 class ConfigCommand(Command):
356 """Implementation of gsutil config command."""
357
358 # Command specification (processed by parent class).
359 command_spec = {
360 # Name of command.
361 COMMAND_NAME : 'config',
362 # List of command name aliases.
363 COMMAND_NAME_ALIASES : ['cfg', 'conf', 'configure'],
364 # Min number of args required by this command.
365 MIN_ARGS : 0,
366 # Max number of args required by this command, or NO_MAX.
367 MAX_ARGS : 0,
368 # Getopt-style string specifying acceptable sub args.
369 SUPPORTED_SUB_ARGS : 'habfwrs:o:',
370 # True if file URIs acceptable for this command.
371 FILE_URIS_OK : False,
372 # True if provider-only URIs acceptable for this command.
373 PROVIDER_URIS_OK : False,
374 # Index in args of first URI arg.
375 URIS_START_ARG : 0,
376 # True if must configure gsutil before running command.
377 CONFIG_REQUIRED : False,
378 }
379 help_spec = {
380 # Name of command or auxiliary help info for which this help applies.
381 HELP_NAME : 'config',
382 # List of help name aliases.
383 HELP_NAME_ALIASES : ['cfg', 'conf', 'configure', 'proxy', 'aws', 's3'],
384 # Type of help:
385 HELP_TYPE : HelpType.COMMAND_HELP,
386 # One line summary of this help.
387 HELP_ONE_LINE_SUMMARY : 'Obtain credentials and create configuration file',
388 # The full help text.
389 HELP_TEXT : _detailed_help_text,
390 }
391
392 def _OpenConfigFile(self, file_path):
393 """Creates and opens a configuration file for writing.
394
395 The file is created with mode 0600, and attempts to open existing files will
396 fail (the latter is important to prevent symlink attacks).
397
398 It is the caller's responsibility to close the file.
399
400 Args:
401 file_path: Path of the file to be created.
402
403 Returns:
404 A writable file object for the opened file.
405
406 Raises:
407 CommandException: if an error occurred when opening the file (including
408 when the file already exists).
409 """
410 flags = os.O_RDWR | os.O_CREAT | os.O_EXCL
411 # Accommodate Windows; stolen from python2.6/tempfile.py.
412 if hasattr(os, 'O_NOINHERIT'):
413 flags |= os.O_NOINHERIT
414 try:
415 fd = os.open(file_path, flags, 0600)
416 except (OSError, IOError), e:
417 raise CommandException('Failed to open %s for writing: %s' %
418 (file_path, e))
419 return os.fdopen(fd, 'w')
420
421 def _WriteBotoConfigFile(self, config_file, use_oauth2=True,
422 launch_browser=True, oauth2_scopes=[SCOPE_FULL_CONTROL]):
423 """Creates a boto config file interactively.
424
425 Needed credentials are obtained interactively, either by asking the user for
426 access key and secret, or by walking the user through the OAuth2 approval
427 flow.
428
429 Args:
430 config_file: File object to which the resulting config file will be
431 written.
432 use_oauth2: If True, walk user through OAuth2 approval flow and produce a
433 config with an oauth2_refresh_token credential. If false, ask the
434 user for access key and secret.
435 launch_browser: In the OAuth2 approval flow, attempt to open a browser
436 window and navigate to the approval URL.
437 oauth2_scopes: A list of OAuth2 scopes to request authorization for, when
438 using OAuth2.
439 """
440
441 # Collect credentials
442 provider_map = {'aws': 'aws', 'google': 'gs'}
443 uri_map = {'aws': 's3', 'google': 'gs'}
444 key_ids = {}
445 sec_keys = {}
446 if use_oauth2:
447 oauth2_refresh_token = oauth2_helper.OAuth2ApprovalFlow(
448 oauth2_helper.OAuth2ClientFromBotoConfig(boto.config),
449 oauth2_scopes, launch_browser)
450 else:
451 got_creds = False
452 for provider in provider_map:
453 if provider == 'google':
454 key_ids[provider] = raw_input('What is your %s access key ID? ' %
455 provider)
456 sec_keys[provider] = raw_input('What is your %s secret access key? ' %
457 provider)
458 got_creds = True
459 if not key_ids[provider] or not sec_keys[provider]:
460 raise CommandException(
461 'Incomplete credentials provided. Please try again.')
462 if not got_creds:
463 raise CommandException('No credentials provided. Please try again.')
464
465 # Write the config file prelude.
466 config_file.write(CONFIG_PRELUDE_CONTENT.lstrip())
467 config_file.write(
468 '# This file was created by gsutil version %s at %s.\n'
469 % (self.gsutil_ver,
470 datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
471 config_file.write('#\n# You can create additional configuration files by '
472 'running\n# gsutil config [options] [-o <config-file>]\n\n\n')
473
474 # Write the config file Credentials section.
475 config_file.write('[Credentials]\n\n')
476 if use_oauth2:
477 config_file.write('# Google OAuth2 credentials (for "gs://" URIs):\n')
478 config_file.write('# The following OAuth2 token is authorized for '
479 'scope(s):\n')
480 for scope in oauth2_scopes:
481 config_file.write('# %s\n' % scope)
482 config_file.write('gs_oauth2_refresh_token = %s\n\n' %
483 oauth2_refresh_token.refresh_token)
484 else:
485 config_file.write('# To add Google OAuth2 credentials ("gs://" URIs), '
486 'edit and uncomment the\n# following line:\n'
487 '#gs_oauth2_refresh_token = <your OAuth2 refresh token>\n\n')
488
489 for provider in provider_map:
490 key_prefix = provider_map[provider]
491 uri_scheme = uri_map[provider]
492 if provider in key_ids and provider in sec_keys:
493 config_file.write('# %s credentials ("%s://" URIs):\n' %
494 (provider, uri_scheme))
495 config_file.write('%s_access_key_id = %s\n' %
496 (key_prefix, key_ids[provider]))
497 config_file.write('%s_secret_access_key = %s\n' %
498 (key_prefix, sec_keys[provider]))
499 else:
500 config_file.write('# To add %s credentials ("%s://" URIs), edit and '
501 'uncomment the\n# following two lines:\n'
502 '#%s_access_key_id = <your %s access key ID>\n'
503 '#%s_secret_access_key = <your %s secret access key>\n' %
504 (provider, uri_scheme, key_prefix, provider, key_prefix,
505 provider))
506 host_key = Provider.HostKeyMap[provider]
507 config_file.write('# The ability to specify an alternate storage host '
508 'is primarily for cloud\n# storage service developers.\n'
509 '#%s_host = <alternate storage host address>\n\n' % host_key)
510
511 # Write the config file Boto section.
512 config_file.write('%s\n' % CONFIG_BOTO_SECTION_CONTENT)
513
514 # Write the config file GSUtil section that doesn't depend on user input.
515 config_file.write(CONFIG_INPUTLESS_GSUTIL_SECTION_CONTENT)
516
517 # Write the default API version.
518 config_file.write("""
519 # 'default_api_version' specifies the default Google Cloud Storage API
520 # version to use. If not set below gsutil defaults to API version 1.
521 """)
522 api_version = 2
523 if not use_oauth2: api_version = 1
524
525 config_file.write('default_api_version = %d\n' % api_version)
526
527 # Write the config file GSUtil section that includes the default
528 # project ID input from the user.
529 if launch_browser:
530 sys.stdout.write(
531 'Attempting to launch a browser to open the Google API console at '
532 'URL: %s\n\n'
533 '[Note: due to a Python bug, you may see a spurious error message '
534 '"object is not\n callable [...] in [...] Popen.__del__" which can '
535 'be ignored.]\n\n' % GOOG_API_CONSOLE_URI)
536 sys.stdout.write(
537 'In your browser you should see the API Console. Click "Storage" and '
538 'look for the value under "Identifying your project\n\n')
539 if not webbrowser.open(GOOG_API_CONSOLE_URI, new=1, autoraise=True):
540 sys.stdout.write(
541 'Launching browser appears to have failed; please navigate a '
542 'browser to the following URL:\n%s\n' % GOOG_API_CONSOLE_URI)
543 # Short delay; webbrowser.open on linux insists on printing out a message
544 # which we don't want to run into the prompt for the auth code.
545 time.sleep(2)
546 else:
547 sys.stdout.write(
548 '\nPlease navigate your browser to %s,\nthen click "Services" on the '
549 'left side panel and ensure you have Google Cloud\nStorage'
550 'activated, then click "Google Cloud Storage" on the left side '
551 'panel and\nfind the "x-goog-project-id" on that page.\n' %
552 GOOG_API_CONSOLE_URI)
553 default_project_id = raw_input('What is your project-id? ')
554 project_id_section_prelude = """
555 # 'default_project_id' specifies the default Google Cloud Storage project ID to
556 # use with the 'mb' and 'ls' commands. If defined it overrides the default value
557 # you set in the API Console. Either of these defaults can be overridden
558 # by specifying the -p option to the 'mb' and 'ls' commands.
559 """
560 if default_project_id:
561 config_file.write('%sdefault_project_id = %s\n\n\n' %
562 (project_id_section_prelude, default_project_id))
563 else:
564 sys.stderr.write('No default project ID entered. You will need to edit '
565 'the default_project_id value\nin your boto config file '
566 'before using "gsutil ls gs://" or "mb" commands'
567 'with the default API version 2.\n')
568 config_file.write('%s#default_project_id = <value>\n\n\n' %
569 project_id_section_prelude)
570
571 # Write the config file OAuth2 section.
572 config_file.write(CONFIG_OAUTH2_CONFIG_CONTENT)
573
574 # Command entry point.
575 def RunCommand(self):
576 scopes = []
577 use_oauth2 = True
578 launch_browser = False
579 output_file_name = None
580 for opt, opt_arg in self.sub_opts:
581 if opt == '-a':
582 use_oauth2 = False
583 elif opt == '-b':
584 launch_browser = True
585 elif opt == '-f':
586 scopes.append(SCOPE_FULL_CONTROL)
587 elif opt == '-o':
588 output_file_name = opt_arg
589 elif opt == '-r':
590 scopes.append(SCOPE_READ_ONLY)
591 elif opt == '-s':
592 scopes.append(opt_arg)
593 elif opt == '-w':
594 scopes.append(SCOPE_READ_WRITE)
595
596 if use_oauth2 and not HAVE_OAUTH2:
597 raise CommandException(
598 'OAuth2 is only supported when running under Python 2.6 or later\n'
599 '(unless additional dependencies are installed, '
600 'see README for details);\n'
601 'you are running Python %s.\nUse "gsutil config -a" to create a '
602 'config with Developer Key authentication credentials.' % sys.version)
603
604 if not scopes:
605 scopes.append(SCOPE_FULL_CONTROL)
606
607 if output_file_name is None:
608 # Use the default config file name, if it doesn't exist or can be moved
609 # out of the way without clobbering an existing backup file.
610 default_config_path = os.path.expanduser(os.path.join('~', '.boto'))
611 if not os.path.exists(default_config_path):
612 output_file_name = default_config_path
613 else:
614 default_config_path_bak = default_config_path + '.bak'
615 if os.path.exists(default_config_path_bak):
616 raise CommandException('Cannot back up existing config '
617 'file "%s": backup file exists ("%s").'
618 % (default_config_path, default_config_path_bak))
619 else:
620 try:
621 sys.stderr.write(
622 'Backing up existing config file "%s" to "%s"...\n'
623 % (default_config_path, default_config_path_bak))
624 os.rename(default_config_path, default_config_path_bak)
625 except e:
626 raise CommandException('Failed to back up existing config '
627 'file ("%s" -> "%s"): %s.'
628 % (default_config_path, default_config_path_bak, e))
629 output_file_name = default_config_path
630
631 if output_file_name == '-':
632 output_file = sys.stdout
633 else:
634 output_file = self._OpenConfigFile(output_file_name)
635 sys.stderr.write(
636 'This script will create a boto config file at\n%s\ncontaining your '
637 'credentials, based on your responses to the following questions.\n\n'
638 % output_file_name)
639
640 try:
641 self._WriteBotoConfigFile(output_file, use_oauth2=use_oauth2,
642 launch_browser=launch_browser, oauth2_scopes=scopes)
643 except Exception, e:
644 # If an error occurred during config file creation, remove the invalid
645 # config file.
646 if output_file_name != '-':
647 output_file.close()
648 os.unlink(output_file_name)
649 raise
650
651 if output_file_name != '-':
652 output_file.close()
653 sys.stderr.write(
654 '\nBoto config file "%s" created. If you need to use\na proxy to '
655 'access the Internet please see the instructions in that file.\n'
656 % output_file_name)
OLDNEW
« no previous file with comments | « third_party/gsutil/gslib/commands/cat.py ('k') | third_party/gsutil/gslib/commands/cp.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698