Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 logging | 5 import logging |
| 6 import os | |
| 6 import traceback | 7 import traceback |
| 7 | 8 |
| 8 from chroot_file_system import ChrootFileSystem | 9 from chroot_file_system import ChrootFileSystem |
| 9 from content_provider import ContentProvider | 10 from content_provider import ContentProvider |
| 10 from extensions_paths import CONTENT_PROVIDERS | 11 import environment |
| 12 from extensions_paths import CONTENT_PROVIDERS, LOCAL_DEBUG_DIR | |
| 11 from future import Gettable, Future | 13 from future import Gettable, Future |
| 14 from local_file_system import LocalFileSystem | |
| 12 from third_party.json_schema_compiler.memoize import memoize | 15 from third_party.json_schema_compiler.memoize import memoize |
| 13 | 16 |
| 14 | 17 |
| 15 class ContentProviders(object): | 18 class ContentProviders(object): |
| 16 '''Implements the content_providers.json configuration; see | 19 '''Implements the content_providers.json configuration; see |
| 17 chrome/common/extensions/docs/templates/json/content_providers.json for its | 20 chrome/common/extensions/docs/templates/json/content_providers.json for its |
| 18 current state and a description of the format. | 21 current state and a description of the format. |
| 19 | 22 |
| 20 Returns ContentProvider instances based on how they're configured there. | 23 Returns ContentProvider instances based on how they're configured there. |
| 21 ''' | 24 ''' |
| 22 | 25 |
| 23 def __init__(self, | 26 def __init__(self, |
| 24 compiled_fs_factory, | 27 compiled_fs_factory, |
| 25 host_file_system, | 28 host_file_system, |
| 26 github_file_system_provider): | 29 github_file_system_provider, |
| 30 gcs_file_system_provider): | |
| 27 self._compiled_fs_factory = compiled_fs_factory | 31 self._compiled_fs_factory = compiled_fs_factory |
| 28 self._host_file_system = host_file_system | 32 self._host_file_system = host_file_system |
| 29 self._github_file_system_provider = github_file_system_provider | 33 self._github_file_system_provider = github_file_system_provider |
| 30 self._cache = compiled_fs_factory.ForJson(host_file_system) | 34 self._gcs_file_system_provider = gcs_file_system_provider |
| 35 self._cache = None | |
| 36 | |
| 37 # If running the devserver and there is a LOCAL_DEBUG_DIR, we | |
| 38 # will read the content_provider configuration from there instead | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
Instead of what? Also, end comments with punctuati
Renato Mangini (chromium)
2014/01/31 02:29:04
Done.
| |
| 39 if environment.IsDevServer() and os.path.exists(LOCAL_DEBUG_DIR): | |
| 40 local_fs = LocalFileSystem(LOCAL_DEBUG_DIR) | |
| 41 conf_stat = None | |
| 42 try: | |
| 43 conf_stat = local_fs.Stat(CONTENT_PROVIDERS) | |
| 44 except: | |
| 45 pass | |
| 46 | |
| 47 if conf_stat: | |
| 48 logging.warn(("Using local debug folder (%s) for " | |
| 49 "content_provider.json configuration") % LOCAL_DEBUG_DIR) | |
| 50 self._using_debug_conf = True | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
This variable is never used. Do you need it?
Renato Mangini (chromium)
2014/01/31 02:29:04
Done.
| |
| 51 self._cache = compiled_fs_factory.ForJson(local_fs) | |
| 52 | |
| 53 if not self._cache: | |
| 54 self._cache = compiled_fs_factory.ForJson(host_file_system) | |
| 31 | 55 |
| 32 @memoize | 56 @memoize |
| 33 def GetByName(self, name): | 57 def GetByName(self, name): |
| 34 '''Gets the ContentProvider keyed by |name| in content_providers.json, or | 58 '''Gets the ContentProvider keyed by |name| in content_providers.json, or |
| 35 None of there is no such content provider. | 59 None of there is no such content provider. |
| 36 ''' | 60 ''' |
| 37 config = self._GetConfig().get(name) | 61 config = self._GetConfig().get(name) |
| 38 if config is None: | 62 if config is None: |
| 39 logging.error('No content provider found with name "%s"' % name) | 63 logging.error('No content provider found with name "%s"' % name) |
| 40 return None | 64 return None |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 71 supports_templates = config.get('supportsTemplates', False) | 95 supports_templates = config.get('supportsTemplates', False) |
| 72 supports_zip = config.get('supportsZip', False) | 96 supports_zip = config.get('supportsZip', False) |
| 73 | 97 |
| 74 if 'chromium' in config: | 98 if 'chromium' in config: |
| 75 chromium_config = config['chromium'] | 99 chromium_config = config['chromium'] |
| 76 if 'dir' not in chromium_config: | 100 if 'dir' not in chromium_config: |
| 77 logging.error('%s: "chromium" must have a "dir" property' % name) | 101 logging.error('%s: "chromium" must have a "dir" property' % name) |
| 78 return None | 102 return None |
| 79 file_system = ChrootFileSystem(self._host_file_system, | 103 file_system = ChrootFileSystem(self._host_file_system, |
| 80 chromium_config['dir']) | 104 chromium_config['dir']) |
| 105 elif 'gcs' in config: | |
| 106 gcs_config = config['gcs'] | |
| 107 if 'bucket' not in gcs_config: | |
| 108 logging.error('%s: "gcs" must have a "bucket" property' % name) | |
| 109 return None | |
| 110 bucket = gcs_config['bucket'] | |
| 111 if not bucket.startswith('gs://'): | |
| 112 logging.error('%s: bucket %s should start with gs://' % (name, bucket)) | |
| 113 return None | |
| 114 bucket = bucket.lstrip('gs:/') | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
lstrip will drop a 'g' or 's' from the start of th
Renato Mangini (chromium)
2014/01/31 02:29:04
Done.
| |
| 115 file_system = self._gcs_file_system_provider.Create(bucket) | |
| 116 if 'dir' in gcs_config: | |
| 117 file_system = ChrootFileSystem(file_system, gcs_config['dir']) | |
| 118 | |
| 81 elif 'github' in config: | 119 elif 'github' in config: |
| 82 github_config = config['github'] | 120 github_config = config['github'] |
| 83 if 'owner' not in github_config or 'repo' not in github_config: | 121 if 'owner' not in github_config or 'repo' not in github_config: |
| 84 logging.error('%s: "github" must provide an "owner" and "repo"' % name) | 122 logging.error('%s: "github" must provide an "owner" and "repo"' % name) |
| 85 return None | 123 return None |
| 86 file_system = self._github_file_system_provider.Create( | 124 file_system = self._github_file_system_provider.Create( |
| 87 github_config['owner'], github_config['repo']) | 125 github_config['owner'], github_config['repo']) |
| 88 if 'dir' in github_config: | 126 if 'dir' in github_config: |
| 89 file_system = ChrootFileSystem(file_system, github_config['dir']) | 127 file_system = ChrootFileSystem(file_system, github_config['dir']) |
| 128 | |
| 90 else: | 129 else: |
| 91 logging.error( | 130 logging.error('%s: content provider type not supported' % name) |
| 92 '%s: content provider type "%s" not supported' % (name, type_)) | |
| 93 return None | 131 return None |
| 94 | 132 |
| 95 return ContentProvider(name, | 133 return ContentProvider(name, |
| 96 self._compiled_fs_factory, | 134 self._compiled_fs_factory, |
| 97 file_system, | 135 file_system, |
| 98 supports_templates=supports_templates, | 136 supports_templates=supports_templates, |
| 99 supports_zip=supports_zip) | 137 supports_zip=supports_zip) |
| 100 | 138 |
| 101 def Cron(self): | 139 def Cron(self): |
| 102 def safe(name, action, callback): | 140 def safe(name, action, callback): |
| 103 '''Safely runs |callback| for a ContentProvider called |name| by | 141 '''Safely runs |callback| for a ContentProvider called |name| by |
| 104 swallowing exceptions and turning them into a None return value. It's | 142 swallowing exceptions and turning them into a None return value. It's |
| 105 important to run all ContentProvider Crons even if some of them fail. | 143 important to run all ContentProvider Crons even if some of them fail. |
| 106 ''' | 144 ''' |
| 107 try: | 145 try: |
| 108 return callback() | 146 return callback() |
| 109 except: | 147 except: |
| 110 logging.error('Error %s Cron for ContentProvider "%s":\n%s' % | 148 logging.error('Error %s Cron for ContentProvider "%s":\n%s' % |
| 111 (action, name, traceback.format_exc())) | 149 (action, name, traceback.format_exc())) |
| 112 return None | 150 return None |
| 113 | 151 |
| 114 futures = [(name, safe(name, | 152 futures = [(name, safe(name, |
| 115 'initializing', | 153 'initializing', |
| 116 self._CreateContentProvider(name, config).Cron)) | 154 self._CreateContentProvider(name, config).Cron)) |
| 117 for name, config in self._GetConfig().iteritems()] | 155 for name, config in self._GetConfig().iteritems()] |
| 118 return Future(delegate=Gettable( | 156 return Future(delegate=Gettable( |
| 119 lambda: [safe(name, 'resolving', f.Get) for name, f in futures if f])) | 157 lambda: [safe(name, 'resolving', f.Get) for name, f in futures if f])) |
| OLD | NEW |