| OLD | NEW |
| 1 # Copyright 2011 Google Inc. All Rights Reserved. | 1 # Copyright 2011 Google Inc. All Rights Reserved. |
| 2 # | 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with 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 | 5 # You may obtain a copy of the License at |
| 6 # | 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 # Write the default API version. | 533 # Write the default API version. |
| 534 config_file.write(""" | 534 config_file.write(""" |
| 535 # 'default_api_version' specifies the default Google Cloud Storage API | 535 # 'default_api_version' specifies the default Google Cloud Storage API |
| 536 # version to use. If not set below gsutil defaults to API version 1. | 536 # version to use. If not set below gsutil defaults to API version 1. |
| 537 """) | 537 """) |
| 538 api_version = 2 | 538 api_version = 2 |
| 539 if not use_oauth2: api_version = 1 | 539 if not use_oauth2: api_version = 1 |
| 540 | 540 |
| 541 config_file.write('default_api_version = %d\n' % api_version) | 541 config_file.write('default_api_version = %d\n' % api_version) |
| 542 | 542 |
| 543 # Write the config file GSUtil section that includes the default | 543 default_project_id = '0' |
| 544 # project ID input from the user. | |
| 545 if launch_browser: | |
| 546 sys.stdout.write( | |
| 547 'Attempting to launch a browser to open the Google API console at ' | |
| 548 'URL: %s\n\n' | |
| 549 '[Note: due to a Python bug, you may see a spurious error message ' | |
| 550 '"object is not\n callable [...] in [...] Popen.__del__" which can ' | |
| 551 'be ignored.]\n\n' % GOOG_API_CONSOLE_URI) | |
| 552 sys.stdout.write( | |
| 553 'In your browser you should see the API Console. Click "Storage" and ' | |
| 554 'look for the value under "Identifying your project\n\n') | |
| 555 if not webbrowser.open(GOOG_API_CONSOLE_URI, new=1, autoraise=True): | |
| 556 sys.stdout.write( | |
| 557 'Launching browser appears to have failed; please navigate a ' | |
| 558 'browser to the following URL:\n%s\n' % GOOG_API_CONSOLE_URI) | |
| 559 # Short delay; webbrowser.open on linux insists on printing out a message | |
| 560 # which we don't want to run into the prompt for the auth code. | |
| 561 time.sleep(2) | |
| 562 else: | |
| 563 sys.stdout.write( | |
| 564 '\nPlease navigate your browser to %s,\nthen click "Services" on the ' | |
| 565 'left side panel and ensure you have Google Cloud\nStorage' | |
| 566 'activated, then click "Google Cloud Storage" on the left side ' | |
| 567 'panel and\nfind the "x-goog-project-id" on that page.\n' % | |
| 568 GOOG_API_CONSOLE_URI) | |
| 569 default_project_id = raw_input('What is your project-id? ') | |
| 570 project_id_section_prelude = """ | 544 project_id_section_prelude = """ |
| 571 # 'default_project_id' specifies the default Google Cloud Storage project ID to | 545 # 'default_project_id' specifies the default Google Cloud Storage project ID to |
| 572 # use with the 'mb' and 'ls' commands. If defined it overrides the default value | 546 # use with the 'mb' and 'ls' commands. If defined it overrides the default value |
| 573 # you set in the API Console. Either of these defaults can be overridden | 547 # you set in the API Console. Either of these defaults can be overridden |
| 574 # by specifying the -p option to the 'mb' and 'ls' commands. | 548 # by specifying the -p option to the 'mb' and 'ls' commands. |
| 575 """ | 549 """ |
| 576 if default_project_id: | 550 if default_project_id: |
| 577 config_file.write('%sdefault_project_id = %s\n\n\n' % | 551 config_file.write('%sdefault_project_id = %s\n\n\n' % |
| 578 (project_id_section_prelude, default_project_id)) | 552 (project_id_section_prelude, default_project_id)) |
| 579 else: | 553 else: |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 except e: | 623 except e: |
| 650 raise CommandException('Failed to back up existing config ' | 624 raise CommandException('Failed to back up existing config ' |
| 651 'file ("%s" -> "%s"): %s.' | 625 'file ("%s" -> "%s"): %s.' |
| 652 % (default_config_path, default_config_path_bak, e)) | 626 % (default_config_path, default_config_path_bak, e)) |
| 653 output_file_name = default_config_path | 627 output_file_name = default_config_path |
| 654 | 628 |
| 655 if output_file_name == '-': | 629 if output_file_name == '-': |
| 656 output_file = sys.stdout | 630 output_file = sys.stdout |
| 657 else: | 631 else: |
| 658 output_file = self._OpenConfigFile(output_file_name) | 632 output_file = self._OpenConfigFile(output_file_name) |
| 659 sys.stderr.write( | |
| 660 'This script will create a boto config file at\n%s\ncontaining your ' | |
| 661 'credentials, based on your responses to the following questions.\n\n' | |
| 662 % output_file_name) | |
| 663 | 633 |
| 664 # Catch ^C so we can restore the backup. | 634 # Catch ^C so we can restore the backup. |
| 665 signal.signal(signal.SIGINT, cleanup_handler) | 635 signal.signal(signal.SIGINT, cleanup_handler) |
| 666 try: | 636 try: |
| 667 self._WriteBotoConfigFile(output_file, use_oauth2=use_oauth2, | 637 self._WriteBotoConfigFile(output_file, use_oauth2=use_oauth2, |
| 668 launch_browser=launch_browser, oauth2_scopes=scopes) | 638 launch_browser=launch_browser, oauth2_scopes=scopes) |
| 669 except Exception, e: | 639 except Exception, e: |
| 670 user_aborted = isinstance(e, AbortException) | 640 user_aborted = isinstance(e, AbortException) |
| 671 if user_aborted: | 641 if user_aborted: |
| 672 sys.stderr.write('\nCaught ^C; cleaning up\n') | 642 sys.stderr.write('\nCaught ^C; cleaning up\n') |
| 673 # If an error occurred during config file creation, remove the invalid | 643 # If an error occurred during config file creation, remove the invalid |
| 674 # config file and restore the backup file. | 644 # config file and restore the backup file. |
| 675 if output_file_name != '-': | 645 if output_file_name != '-': |
| 676 output_file.close() | 646 output_file.close() |
| 677 os.unlink(output_file_name) | 647 os.unlink(output_file_name) |
| 678 if default_config_path_bak: | 648 if default_config_path_bak: |
| 679 sys.stderr.write('Restoring previous backed up file (%s)\n' % | 649 sys.stderr.write('Restoring previous backed up file (%s)\n' % |
| 680 default_config_path_bak) | 650 default_config_path_bak) |
| 681 os.rename(default_config_path_bak, output_file_name) | 651 os.rename(default_config_path_bak, output_file_name) |
| 682 raise | 652 raise |
| 683 | 653 |
| 684 if output_file_name != '-': | 654 if output_file_name != '-': |
| 685 output_file.close() | 655 output_file.close() |
| 686 sys.stderr.write( | 656 sys.stderr.write( |
| 687 '\nBoto config file "%s" created.\nIf you need to use a proxy to ' | 657 '\nBoto config file "%s" created.\n' % output_file_name) |
| 688 'use a proxy to access the Internet please see the instructions in ' | |
| 689 'that file.\n' % output_file_name) | |
| 690 | 658 |
| 691 return 0 | 659 return 0 |
| 692 | 660 |
| 693 def cleanup_handler(signalnum, handler): | 661 def cleanup_handler(signalnum, handler): |
| 694 raise AbortException('User interrupted config command') | 662 raise AbortException('User interrupted config command') |
| OLD | NEW |