OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 """ | 3 """ |
4 Copyright 2014 Google Inc. | 4 Copyright 2014 Google Inc. |
5 | 5 |
6 Use of this source code is governed by a BSD-style license that can be | 6 Use of this source code is governed by a BSD-style license that can be |
7 found in the LICENSE file. | 7 found in the LICENSE file. |
8 | 8 |
9 Utilities for accessing Google Cloud Storage. | 9 Utilities for accessing Google Cloud Storage. |
10 | |
11 TODO(epoger): move this into tools/utils for broader use? | |
12 """ | 10 """ |
13 | 11 |
14 # System-level imports | 12 # System-level imports |
15 import os | 13 import os |
16 import posixpath | 14 import posixpath |
17 import sys | 15 import sys |
| 16 |
| 17 # Imports from third-party code |
| 18 TRUNK_DIRECTORY = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) |
| 19 for import_subdir in ['google-api-python-client', 'httplib2', 'oauth2client', |
| 20 'uritemplate-py']: |
| 21 import_dirpath = os.path.join( |
| 22 TRUNK_DIRECTORY, 'third_party', 'externals', import_subdir) |
| 23 if import_dirpath not in sys.path: |
| 24 # We need to insert at the beginning of the path, to make sure that our |
| 25 # imported versions are favored over others that might be in the path. |
| 26 # Also, the google-api-python-client checkout contains an empty |
| 27 # oauth2client directory, which will confuse things unless we insert |
| 28 # our checked-out oauth2client in front of it in the path. |
| 29 sys.path.insert(0, import_dirpath) |
18 try: | 30 try: |
19 from apiclient.discovery import build as build_service | 31 from googleapiclient.discovery import build as build_service |
20 except ImportError: | 32 except ImportError: |
21 print ('Missing google-api-python-client. Please install it; directions ' | 33 # TODO(epoger): We are moving toward not needing any dependencies to be |
| 34 # installed at a system level, but in the meanwhile, if developers run into |
| 35 # trouble they can install those system-level dependencies to get unblocked. |
| 36 print ('Missing dependencies of google-api-python-client. Please install ' |
| 37 'google-api-python-client to get those dependencies; directions ' |
22 'can be found at https://developers.google.com/api-client-library/' | 38 'can be found at https://developers.google.com/api-client-library/' |
23 'python/start/installation') | 39 'python/start/installation . More details in http://skbug.com/2641 ') |
24 raise | 40 raise |
25 | 41 |
26 # Local imports | 42 # Local imports |
27 import url_utils | 43 import url_utils |
28 | 44 |
29 | 45 |
30 def download_file(source_bucket, source_path, dest_path, | 46 def download_file(source_bucket, source_path, dest_path, |
31 create_subdirs_if_needed=False): | 47 create_subdirs_if_needed=False): |
32 """ Downloads a single file from Google Cloud Storage to local disk. | 48 """ Downloads a single file from Google Cloud Storage to local disk. |
33 | 49 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 dirs = [] | 88 dirs = [] |
73 for dir_fullpath in results.get('prefixes', []): | 89 for dir_fullpath in results.get('prefixes', []): |
74 dir_basename = dir_fullpath[subdir_length:] | 90 dir_basename = dir_fullpath[subdir_length:] |
75 dirs.append(dir_basename[:-1]) # strip trailing slash | 91 dirs.append(dir_basename[:-1]) # strip trailing slash |
76 files = [] | 92 files = [] |
77 for file_properties in results.get('items', []): | 93 for file_properties in results.get('items', []): |
78 file_fullpath = file_properties['name'] | 94 file_fullpath = file_properties['name'] |
79 file_basename = file_fullpath[subdir_length:] | 95 file_basename = file_fullpath[subdir_length:] |
80 files.append(file_basename) | 96 files.append(file_basename) |
81 return (dirs, files) | 97 return (dirs, files) |
OLD | NEW |