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 IsAppEngine, IsTest | |
12 from environment_wrappers import CreateUrlFetcher | |
Ken Rockot(use gerrit already)
2015/05/26 00:26:24
These are now unused. Removed in PS#2
| |
10 from extensions_paths import LOCAL_GCS_DIR, LOCAL_GCS_DEBUG_CONF | 13 from extensions_paths import LOCAL_GCS_DIR, LOCAL_GCS_DEBUG_CONF |
14 from gcs_file_system import CloudStorageFileSystem | |
11 from local_file_system import LocalFileSystem | 15 from local_file_system import LocalFileSystem |
12 from path_util import IsDirectory, ToDirectory | 16 from path_util import IsDirectory, ToDirectory |
13 | 17 |
18 | |
14 class CloudStorageFileSystemProvider(object): | 19 class CloudStorageFileSystemProvider(object): |
15 '''Provides CloudStorageFileSystem bound to a GCS bucket. | 20 '''Provides CloudStorageFileSystem bound to a GCS bucket. |
16 ''' | 21 ''' |
17 def __init__(self, object_store_creator): | 22 def __init__(self, object_store_creator): |
18 self._object_store_creator = object_store_creator | 23 self._object_store_creator = object_store_creator |
19 | 24 |
20 def Create(self, bucket): | 25 def Create(self, bucket): |
21 '''Creates a CloudStorageFileSystemProvider. | 26 '''Creates a CloudStorageFileSystemProvider. |
22 | 27 |
23 |bucket| is the name of GCS bucket, eg devtools-docs. It is expected | 28 |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. | 29 that this bucket has Read permission for this app in its ACLs. |
25 | 30 |
26 Optional configuration can be set in a local_debug/gcs_debug.conf file: | 31 Optional configuration can be set in a local_debug/gcs_debug.conf file: |
27 use_local_fs=True|False | 32 use_local_fs=True|False |
28 access_token=<token> | |
29 remote_bucket_prefix=<prefix> | 33 remote_bucket_prefix=<prefix> |
30 | 34 |
31 If running in Preview mode or in Development mode with use_local_fs set to | 35 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 | 36 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 | 37 of in the real GCS server. |
34 access, so it is always forced to use a LocalFileSystem. | 38 ''' |
39 if IsTest(): | |
40 return EmptyDirFileSystem() | |
35 | 41 |
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 | 42 debug_bucket_prefix = None |
70 use_local_fs = False | 43 use_local_fs = False |
71 | 44 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: | 45 with open(LOCAL_GCS_DEBUG_CONF, "r") as token_file: |
74 properties = dict(line.strip().split('=', 1) for line in token_file) | 46 properties = dict(line.strip().split('=', 1) for line in token_file) |
75 use_local_fs = properties.get('use_local_fs', 'False')=='True' | 47 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) | 48 debug_bucket_prefix = properties.get('remote_bucket_prefix', None) |
49 logging.debug('gcs: prefixing all bucket names with %s' % | |
50 debug_bucket_prefix) | |
78 | 51 |
79 if environment.IsDevServer() and use_local_fs: | 52 if use_local_fs: |
80 return LocalFileSystem(ToDirectory(os.path.join(LOCAL_GCS_DIR, bucket))) | 53 return LocalFileSystem(ToDirectory(os.path.join(LOCAL_GCS_DIR, bucket))) |
81 | 54 |
82 # gcs_file_system has strong dependencies on runtime appengine APIs, | 55 if debug_bucket_prefix: |
83 # so we only import it when we are sure we are not on preview.py or tests. | 56 bucket = debug_bucket_prefix + bucket |
84 from gcs_file_system import CloudStorageFileSystem | 57 |
85 return CachingFileSystem(CloudStorageFileSystem(bucket, | 58 return CachingFileSystem(CloudStorageFileSystem(bucket), |
86 debug_access_token, debug_bucket_prefix), | 59 self._object_store_creator) |
87 self._object_store_creator) | |
88 | 60 |
89 @staticmethod | 61 @staticmethod |
90 def ForEmpty(): | 62 def ForEmpty(): |
91 class EmptyImpl(object): | 63 class EmptyImpl(object): |
92 def Create(self, bucket): | 64 def Create(self, bucket): |
93 return EmptyDirFileSystem() | 65 return EmptyDirFileSystem() |
94 return EmptyImpl() | 66 return EmptyImpl() |
OLD | NEW |