OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os | 5 import os |
6 import environment | 6 import environment |
| 7 import logging |
7 | 8 |
8 from caching_file_system import CachingFileSystem | 9 from caching_file_system import CachingFileSystem |
9 from empty_dir_file_system import EmptyDirFileSystem | 10 from empty_dir_file_system import EmptyDirFileSystem |
| 11 from environment import IsTest |
10 from extensions_paths import LOCAL_GCS_DIR, LOCAL_GCS_DEBUG_CONF | 12 from extensions_paths import LOCAL_GCS_DIR, LOCAL_GCS_DEBUG_CONF |
| 13 from gcs_file_system import CloudStorageFileSystem |
11 from local_file_system import LocalFileSystem | 14 from local_file_system import LocalFileSystem |
12 from path_util import IsDirectory, ToDirectory | 15 from path_util import ToDirectory |
| 16 |
13 | 17 |
14 class CloudStorageFileSystemProvider(object): | 18 class CloudStorageFileSystemProvider(object): |
15 '''Provides CloudStorageFileSystem bound to a GCS bucket. | 19 '''Provides CloudStorageFileSystem bound to a GCS bucket. |
16 ''' | 20 ''' |
17 def __init__(self, object_store_creator): | 21 def __init__(self, object_store_creator): |
18 self._object_store_creator = object_store_creator | 22 self._object_store_creator = object_store_creator |
19 | 23 |
20 def Create(self, bucket): | 24 def Create(self, bucket): |
21 '''Creates a CloudStorageFileSystemProvider. | 25 '''Creates a CloudStorageFileSystemProvider. |
22 | 26 |
23 |bucket| is the name of GCS bucket, eg devtools-docs. It is expected | 27 |bucket| is the name of GCS bucket, eg devtools-docs. It is expected |
24 that this bucket has Read permission for this app in its ACLs. | 28 that this bucket has Read permission for this app in its ACLs. |
25 | 29 |
26 Optional configuration can be set in a local_debug/gcs_debug.conf file: | 30 Optional configuration can be set in a local_debug/gcs_debug.conf file: |
27 use_local_fs=True|False | 31 use_local_fs=True|False |
28 access_token=<token> | |
29 remote_bucket_prefix=<prefix> | 32 remote_bucket_prefix=<prefix> |
30 | 33 |
31 If running in Preview mode or in Development mode with use_local_fs set to | 34 If running in Preview mode or in Development mode with use_local_fs set to |
32 True, buckets and files are looked inside the local_debug folder instead | 35 True, buckets and files are looked for inside the local_debug folder instead |
33 of in the real GCS server. Preview server does not support direct GCS | 36 of in the real GCS server. |
34 access, so it is always forced to use a LocalFileSystem. | 37 ''' |
| 38 if IsTest(): |
| 39 return EmptyDirFileSystem() |
35 | 40 |
36 For real GCS access in the Development mode (dev_appserver.py), | |
37 access_token and remote_bucket_prefix options can be | |
38 used to change the way GCS files are accessed. Both are ignored in a real | |
39 appengine instance. | |
40 | |
41 "access_token" is always REQUIRED on dev_appengine, otherwise you will | |
42 get 404 (auth) errors. You can get one access_token valid for a few minutes | |
43 by typing: | |
44 gsutil -d ls 2>&1 | grep "Bearer" | | |
45 sed "s/.*Bearer \(.*\).r.nUser-Agent.*/access_token=\1/" )" | |
46 | |
47 A sample output would be: | |
48 access_token=ya29.1.AADtN_VW5ibbfLHV5cMIK5ss4bHtVzBXpa4byjd | |
49 | |
50 Now add this line to the local_debug/gcs_debug.conf file and restart the | |
51 appengine development server. | |
52 | |
53 Remember that you will need a new access_token every ten minutes or | |
54 so. If you get 404 errors on log, update it. Access token is not | |
55 used for a deployed appengine app, only if you use dev_appengine.py. | |
56 | |
57 remote_bucket_prefix is useful if you want to test on your own GCS buckets | |
58 before using the real GCS buckets. | |
59 | |
60 ''' | |
61 if not environment.IsReleaseServer() and not environment.IsDevServer(): | |
62 bucket_local_path = os.path.join(LOCAL_GCS_DIR, bucket) | |
63 if IsDirectory(bucket_local_path): | |
64 return LocalFileSystem(bucket_local_path) | |
65 else: | |
66 return EmptyDirFileSystem() | |
67 | |
68 debug_access_token = None | |
69 debug_bucket_prefix = None | 41 debug_bucket_prefix = None |
70 use_local_fs = False | 42 use_local_fs = False |
71 | 43 if os.path.exists(LOCAL_GCS_DEBUG_CONF): |
72 if environment.IsDevServer() and os.path.exists(LOCAL_GCS_DEBUG_CONF): | |
73 with open(LOCAL_GCS_DEBUG_CONF, "r") as token_file: | 44 with open(LOCAL_GCS_DEBUG_CONF, "r") as token_file: |
74 properties = dict(line.strip().split('=', 1) for line in token_file) | 45 properties = dict(line.strip().split('=', 1) for line in token_file) |
75 use_local_fs = properties.get('use_local_fs', 'False')=='True' | 46 use_local_fs = properties.get('use_local_fs', 'False')=='True' |
76 debug_access_token = properties.get('access_token', None) | |
77 debug_bucket_prefix = properties.get('remote_bucket_prefix', None) | 47 debug_bucket_prefix = properties.get('remote_bucket_prefix', None) |
| 48 logging.debug('gcs: prefixing all bucket names with %s' % |
| 49 debug_bucket_prefix) |
78 | 50 |
79 if environment.IsDevServer() and use_local_fs: | 51 if use_local_fs: |
80 return LocalFileSystem(ToDirectory(os.path.join(LOCAL_GCS_DIR, bucket))) | 52 return LocalFileSystem(ToDirectory(os.path.join(LOCAL_GCS_DIR, bucket))) |
81 | 53 |
82 # gcs_file_system has strong dependencies on runtime appengine APIs, | 54 if debug_bucket_prefix: |
83 # so we only import it when we are sure we are not on preview.py or tests. | 55 bucket = debug_bucket_prefix + bucket |
84 from gcs_file_system import CloudStorageFileSystem | 56 |
85 return CachingFileSystem(CloudStorageFileSystem(bucket, | 57 return CachingFileSystem(CloudStorageFileSystem(bucket), |
86 debug_access_token, debug_bucket_prefix), | 58 self._object_store_creator) |
87 self._object_store_creator) | |
88 | 59 |
89 @staticmethod | 60 @staticmethod |
90 def ForEmpty(): | 61 def ForEmpty(): |
91 class EmptyImpl(object): | 62 class EmptyImpl(object): |
92 def Create(self, bucket): | 63 def Create(self, bucket): |
93 return EmptyDirFileSystem() | 64 return EmptyDirFileSystem() |
94 return EmptyImpl() | 65 return EmptyImpl() |
OLD | NEW |