| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 | 2 |
| 3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
| 5 # | 5 # |
| 6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
| 7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
| 8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
| 9 # | 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
| 14 # | 14 # |
| 15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
| 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 17 | 17 |
| 18 import mimetypes | 18 import mimetypes |
| 19 import os | 19 import os |
| 20 import sys | 20 import sys |
| 21 import argparse | |
| 21 | 22 |
| 22 import jinja2 | 23 import jinja2 |
| 23 | 24 |
| 24 from cms.utils import process_page | 25 from cms.utils import process_page |
| 25 from cms.sources import FileSource | 26 from cms.sources import FileSource |
| 26 from cms.converters import converters | 27 from cms.converters import converters |
| 27 | 28 |
| 28 source = None | 29 source = None |
| 29 | 30 |
| 30 UNICODE_ENCODING = "utf-8" | 31 UNICODE_ENCODING = "utf-8" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 | 93 |
| 93 def show_error(start_response, status, **kwargs): | 94 def show_error(start_response, status, **kwargs): |
| 94 env = jinja2.Environment(autoescape=True) | 95 env = jinja2.Environment(autoescape=True) |
| 95 template = env.from_string(ERROR_TEMPLATE) | 96 template = env.from_string(ERROR_TEMPLATE) |
| 96 mime = "text/html; encoding=%s" % UNICODE_ENCODING | 97 mime = "text/html; encoding=%s" % UNICODE_ENCODING |
| 97 start_response(status, [("Content-Type", mime)]) | 98 start_response(status, [("Content-Type", mime)]) |
| 98 for fragment in template.stream(status=status, **kwargs): | 99 for fragment in template.stream(status=status, **kwargs): |
| 99 yield fragment.encode(UNICODE_ENCODING) | 100 yield fragment.encode(UNICODE_ENCODING) |
| 100 | 101 |
| 101 def handler(environ, start_response): | 102 def handler(environ, start_response): |
| 103 | |
| 102 path = environ.get("PATH_INFO") | 104 path = environ.get("PATH_INFO") |
| 103 | 105 |
| 104 data = get_data(path) | 106 data = get_data(path) |
| 105 if data is None: | 107 if data is None: |
| 106 return show_error(start_response, "404 Not Found", uri=path) | 108 return show_error(start_response, "404 Not Found", uri=path) |
| 107 | 109 |
| 108 mime = mimetypes.guess_type(path)[0] or "text/html" | 110 mime = mimetypes.guess_type(path)[0] or "text/html" |
| 109 | 111 |
| 110 if isinstance(data, unicode): | 112 if isinstance(data, unicode): |
| 111 data = data.encode(UNICODE_ENCODING) | 113 data = data.encode(UNICODE_ENCODING) |
| 112 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) | 114 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) |
| 113 | 115 |
| 114 start_response("200 OK", [("Content-Type", mime)]) | 116 start_response("200 OK", [("Content-Type", mime)]) |
| 115 return [data] | 117 return [data] |
| 116 | 118 |
| 117 if __name__ == "__main__": | 119 if __name__ == "__main__": |
| 118 if len(sys.argv) < 2: | 120 |
| 119 source = FileSource(os.curdir) | 121 parser = argparse.ArgumentParser(description='CMS testing server.') |
| 120 elif os.path.isdir(sys.argv[1]): | 122 parser.add_argument('-b', '--basepath', type=str, help='Base directory (EG: /p ath/to/web.eyeo.com)') |
|
Sebastian Noack
2016/01/20 14:35:59
Please keep the path a positional argument rather
juliandoucette
2016/01/21 13:46:23
Done.
| |
| 123 parser.add_argument('-n', '--hostname', type=str, help='Server hostname (EG: l ocalhost, eyeo.com, 0.0.0.0)') | |
|
Sebastian Noack
2016/01/20 14:35:59
Specify "localhost" as default.
juliandoucette
2016/01/21 13:46:23
Done.
| |
| 124 parser.add_argument('-p', '--port', type=str, help='Port number (EG: 8080, 500 0)') | |
|
Sebastian Noack
2016/01/20 14:35:59
Specify 5000 as default.
juliandoucette
2016/01/21 13:46:23
Done.
| |
| 125 args = parser.parse_args() | |
| 126 | |
| 127 # for backwards compatibility | |
|
Sebastian Noack
2016/01/20 14:35:59
With the above comments addressed you don't need t
juliandoucette
2016/01/21 13:46:23
Agreed. Thank you for bringing that up.
| |
| 128 if len(sys.argv) > 1 and os.path.isdir(sys.argv[1]): | |
| 121 source = FileSource(sys.argv[1]) | 129 source = FileSource(sys.argv[1]) |
| 130 hostname = 'localhost' | |
| 131 port = 5000 | |
| 122 else: | 132 else: |
| 123 sys.exit("Usage: %s [source_dir]" % sys.argv[0]) | 133 source = FileSource(args.basepath or os.curdir) |
| 134 hostname = args.hostname or 'localhost' | |
| 135 port = int(args.port or '5000') | |
| 124 | 136 |
| 125 try: | 137 try: |
| 126 from werkzeug.serving import ThreadedWSGIServer, run_simple | 138 from werkzeug.serving import ThreadedWSGIServer, run_simple |
| 127 | 139 |
| 128 # see https://github.com/mitsuhiko/werkzeug/pull/770 | 140 # see https://github.com/mitsuhiko/werkzeug/pull/770 |
| 129 ThreadedWSGIServer.daemon_threads = True | 141 ThreadedWSGIServer.daemon_threads = True |
| 130 | 142 |
| 131 def run(*args, **kwargs): | 143 def run(*args, **kwargs): |
| 132 # The werkzeug logger must be configured before the | 144 # The werkzeug logger must be configured before the |
| 133 # root logger. Also we must prevent it from propagating | 145 # root logger. Also we must prevent it from propagating |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 151 try: | 163 try: |
| 152 return app(environ, start_response) | 164 return app(environ, start_response) |
| 153 except Exception, e: | 165 except Exception, e: |
| 154 return show_error(start_response, "500 Internal Server Error", | 166 return show_error(start_response, "500 Internal Server Error", |
| 155 uri=environ.get("PATH_INFO"), error=e) | 167 uri=environ.get("PATH_INFO"), error=e) |
| 156 | 168 |
| 157 server = make_server(host, port, wrapper, ThreadedWSGIServer) | 169 server = make_server(host, port, wrapper, ThreadedWSGIServer) |
| 158 print " * Running on http://%s:%i/" % server.server_address | 170 print " * Running on http://%s:%i/" % server.server_address |
| 159 server.serve_forever() | 171 server.serve_forever() |
| 160 | 172 |
| 161 run("localhost", 5000, handler, use_reloader=True, use_debugger=True) | 173 run(hostname, port, handler, use_reloader=True, use_debugger=True) |
| OLD | NEW |