Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 | 2 |
| 3 # Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """A CherryPy-based webserver to host images and build packages.""" | 7 """A CherryPy-based webserver to host images and build packages.""" |
| 8 | 8 |
| 9 import cherrypy | 9 import cherrypy |
| 10 import optparse | 10 import optparse |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 'tools.staticdir.on': True, | 49 'tools.staticdir.on': True, |
| 50 'response.timeout': 10000, | 50 'response.timeout': 10000, |
| 51 }, | 51 }, |
| 52 } | 52 } |
| 53 if options.production: | 53 if options.production: |
| 54 base_config['global']['server.environment'] = 'production' | 54 base_config['global']['server.environment'] = 'production' |
| 55 | 55 |
| 56 return base_config | 56 return base_config |
| 57 | 57 |
| 58 | 58 |
| 59 def _PrepareToServeUpdatesOnly(image_dir): | 59 def _PrepareToServeUpdatesOnly(image_dir, static_dir): |
| 60 """Sets up symlink to image_dir for serving purposes.""" | 60 """Sets up symlink to image_dir for serving purposes.""" |
| 61 assert os.path.exists(image_dir), '%s must exist.' % image_dir | 61 assert os.path.exists(image_dir), '%s must exist.' % image_dir |
| 62 # If we're serving out of an archived build dir (e.g. a | 62 # If we're serving out of an archived build dir (e.g. a |
| 63 # buildbot), prepare this webserver's magic 'static/' dir with a | 63 # buildbot), prepare this webserver's magic 'static/' dir with a |
| 64 # link to the build archive. | 64 # link to the build archive. |
| 65 cherrypy.log('Preparing autoupdate for "serve updates only" mode.', | 65 cherrypy.log('Preparing autoupdate for "serve updates only" mode.', |
| 66 'DEVSERVER') | 66 'DEVSERVER') |
| 67 if os.path.lexists('static/archive'): | 67 if os.path.lexists('%s/archive' % static_dir): |
| 68 if image_dir != os.readlink('static/archive'): | 68 if image_dir != os.readlink('%s/archive' % static_dir): |
| 69 cherrypy.log('removing stale symlink to %s' % image_dir, 'DEVSERVER') | 69 cherrypy.log('removing stale symlink to %s' % image_dir, 'DEVSERVER') |
| 70 os.unlink('static/archive') | 70 os.unlink('%s/archive' % static_dir) |
| 71 os.symlink(image_dir, 'static/archive') | 71 os.symlink(image_dir, '%s/archive' % static_dir) |
| 72 else: | 72 else: |
| 73 os.symlink(image_dir, 'static/archive') | 73 os.symlink(image_dir, '%s/archive' % static_dir) |
| 74 cherrypy.log('archive dir: %s ready to be used to serve images.' % image_dir, | 74 cherrypy.log('archive dir: %s ready to be used to serve images.' % image_dir, |
| 75 'DEVSERVER') | 75 'DEVSERVER') |
| 76 | 76 |
| 77 | 77 |
| 78 class DevServerRoot(object): | 78 class DevServerRoot(object): |
| 79 """The Root Class for the Dev Server. | 79 """The Root Class for the Dev Server. |
| 80 | 80 |
| 81 CherryPy works as follows: | 81 CherryPy works as follows: |
| 82 For each method in this class, cherrpy interprets root/path | 82 For each method in this class, cherrpy interprets root/path |
| 83 as a call to an instance of DevServerRoot->method_name. For example, | 83 as a call to an instance of DevServerRoot->method_name. For example, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 parser.add_option('--validate_factory_config', action="store_true", | 153 parser.add_option('--validate_factory_config', action="store_true", |
| 154 dest='validate_factory_config', | 154 dest='validate_factory_config', |
| 155 help='Validate factory config file, then exit.') | 155 help='Validate factory config file, then exit.') |
| 156 parser.set_usage(parser.format_help()) | 156 parser.set_usage(parser.format_help()) |
| 157 (options, _) = parser.parse_args() | 157 (options, _) = parser.parse_args() |
| 158 | 158 |
| 159 devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0])) | 159 devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 160 root_dir = os.path.realpath('%s/../..' % devserver_dir) | 160 root_dir = os.path.realpath('%s/../..' % devserver_dir) |
| 161 serve_only = False | 161 serve_only = False |
| 162 | 162 |
| 163 static_dir = os.path.realpath('%s/static' % options.data_dir) | |
| 164 os.system('mkdir -p %s' % static_dir) | |
| 165 | |
| 163 if options.archive_dir: | 166 if options.archive_dir: |
| 164 static_dir = os.path.realpath(options.archive_dir) | 167 # FIXME(zbehan) Legacy support: |
|
sosa
2011/02/18 22:24:42
TODO(zbehan): Remove legacy support rather than F
| |
| 165 _PrepareToServeUpdatesOnly(static_dir) | 168 # archive_dir is the directory where static/archive will point. |
| 169 # If this is an absolute path, all is fine. If someone calls this | |
| 170 # using a relative path, that is relative to src/platform/dev/. | |
| 171 # That use case is unmaintainable, but since applications use it | |
| 172 # with =./static, instead of a boolean flag, we'll make this relative | |
| 173 # to devserver_dir to keep these unbroken. For now. | |
| 174 prev_cwd = os.getcwd() | |
| 175 os.chdir(devserver_dir) # this surely exists | |
|
sosa
2011/02/18 22:24:42
I'd prefer if you checked if that path was relativ
| |
| 176 archive_dir = os.path.realpath(options.archive_dir) | |
| 177 _PrepareToServeUpdatesOnly(archive_dir, static_dir) | |
| 166 serve_only = True | 178 serve_only = True |
| 167 else: | 179 os.chdir(prev_cwd) |
| 168 static_dir = os.path.realpath('%s/static' % options.data_dir) | |
| 169 os.system('mkdir -p %s' % static_dir) | |
| 170 | 180 |
| 171 cache_dir = os.path.join(static_dir, 'cache') | 181 cache_dir = os.path.join(static_dir, 'cache') |
| 172 cherrypy.log('Using cache directory %s' % cache_dir, 'DEVSERVER') | 182 cherrypy.log('Using cache directory %s' % cache_dir, 'DEVSERVER') |
| 173 | 183 |
| 174 if os.path.exists(cache_dir): | 184 if os.path.exists(cache_dir): |
| 175 if options.clear_cache: | 185 if options.clear_cache: |
| 176 # Clear the cache and exit on error. | 186 # Clear the cache and exit on error. |
| 177 if os.system('rm -rf %s/*' % cache_dir) != 0: | 187 if os.system('rm -rf %s/*' % cache_dir) != 0: |
| 178 cherrypy.log('Failed to clear the cache with %s' % cmd, | 188 cherrypy.log('Failed to clear the cache with %s' % cmd, |
| 179 'DEVSERVER') | 189 'DEVSERVER') |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 # We don't run the dev server with this option. | 233 # We don't run the dev server with this option. |
| 224 if options.validate_factory_config: | 234 if options.validate_factory_config: |
| 225 sys.exit(0) | 235 sys.exit(0) |
| 226 elif options.pregenerate_update: | 236 elif options.pregenerate_update: |
| 227 if not updater.PreGenerateUpdate(): | 237 if not updater.PreGenerateUpdate(): |
| 228 sys.exit(1) | 238 sys.exit(1) |
| 229 | 239 |
| 230 # If the command line requested after setup, it's time to do it. | 240 # If the command line requested after setup, it's time to do it. |
| 231 if not options.exit: | 241 if not options.exit: |
| 232 cherrypy.quickstart(DevServerRoot(), config=_GetConfig(options)) | 242 cherrypy.quickstart(DevServerRoot(), config=_GetConfig(options)) |
| OLD | NEW |