Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: cms/bin/test_server.py

Issue 29334114: issue 3546 - Add port and hostname options to CMS testing server (Closed)
Patch Set: Removed listen argument in favor of address and port, updated help text, and updated formatted stri… Created Jan. 21, 2016, 2:40 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
30 address = None
31 port = None
29 32
30 UNICODE_ENCODING = "utf-8" 33 UNICODE_ENCODING = "utf-8"
31 34
32 ERROR_TEMPLATE = """ 35 ERROR_TEMPLATE = """
33 <html> 36 <html>
34 <head> 37 <head>
35 <title>{{status}}</title> 38 <title>{{status}}</title>
36 </head> 39 </head>
37 <body> 40 <body>
38 <h1>{{status}}</h1> 41 <h1>{{status}}</h1>
(...skipping 19 matching lines...) Expand all
58 locale, page = path.split("/", 1) 61 locale, page = path.split("/", 1)
59 else: 62 else:
60 locale, page = path, "" 63 locale, page = path, ""
61 64
62 default_page = source.read_config().get("general", "defaultpage") 65 default_page = source.read_config().get("general", "defaultpage")
63 alternative_page = "/".join([page, default_page]).lstrip("/") 66 alternative_page = "/".join([page, default_page]).lstrip("/")
64 67
65 for format in converters.iterkeys(): 68 for format in converters.iterkeys():
66 for p in (page, alternative_page): 69 for p in (page, alternative_page):
67 if source.has_page(p, format): 70 if source.has_page(p, format):
68 return (p, process_page(source, locale, p, format, "http://127.0.0.1:500 0")) 71 return (p, process_page(source, locale, p, format, "http://%s:%d" % (add ress, port)))
69 if source.has_localizable_file(locale, page): 72 if source.has_localizable_file(locale, page):
70 return (page, source.read_localizable_file(locale, page)) 73 return (page, source.read_localizable_file(locale, page))
71 74
72 return (None, None) 75 return (None, None)
73 76
74 def has_conflicting_pages(page): 77 def has_conflicting_pages(page):
75 pages = [p for p, _ in source.list_pages()] 78 pages = [p for p, _ in source.list_pages()]
76 pages.extend(source.list_localizable_files()) 79 pages.extend(source.list_localizable_files())
77 80
78 if pages.count(page) > 1: 81 if pages.count(page) > 1:
(...skipping 13 matching lines...) Expand all
92 95
93 def show_error(start_response, status, **kwargs): 96 def show_error(start_response, status, **kwargs):
94 env = jinja2.Environment(autoescape=True) 97 env = jinja2.Environment(autoescape=True)
95 template = env.from_string(ERROR_TEMPLATE) 98 template = env.from_string(ERROR_TEMPLATE)
96 mime = "text/html; encoding=%s" % UNICODE_ENCODING 99 mime = "text/html; encoding=%s" % UNICODE_ENCODING
97 start_response(status, [("Content-Type", mime)]) 100 start_response(status, [("Content-Type", mime)])
98 for fragment in template.stream(status=status, **kwargs): 101 for fragment in template.stream(status=status, **kwargs):
99 yield fragment.encode(UNICODE_ENCODING) 102 yield fragment.encode(UNICODE_ENCODING)
100 103
101 def handler(environ, start_response): 104 def handler(environ, start_response):
105
102 path = environ.get("PATH_INFO") 106 path = environ.get("PATH_INFO")
103 107
104 data = get_data(path) 108 data = get_data(path)
105 if data is None: 109 if data is None:
106 return show_error(start_response, "404 Not Found", uri=path) 110 return show_error(start_response, "404 Not Found", uri=path)
107 111
108 mime = mimetypes.guess_type(path)[0] or "text/html" 112 mime = mimetypes.guess_type(path)[0] or "text/html"
109 113
110 if isinstance(data, unicode): 114 if isinstance(data, unicode):
111 data = data.encode(UNICODE_ENCODING) 115 data = data.encode(UNICODE_ENCODING)
112 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) 116 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING)
113 117
114 start_response("200 OK", [("Content-Type", mime)]) 118 start_response("200 OK", [("Content-Type", mime)])
115 return [data] 119 return [data]
116 120
117 if __name__ == "__main__": 121 if __name__ == "__main__":
118 if len(sys.argv) < 2: 122
119 source = FileSource(os.curdir) 123 parser = argparse.ArgumentParser(description='CMS testing server.')
Sebastian Noack 2016/01/21 15:12:32 Nit: As below, no full stop if it isn't a sentence
juliandoucette 2016/01/21 16:06:25 Done.
120 elif os.path.isdir(sys.argv[1]): 124 parser.add_argument('path', nargs='?', default=os.curdir)
121 source = FileSource(sys.argv[1]) 125 parser.add_argument('-a', '--address', default='localhost', help='Address of t he interface the server will listen on')
Sebastian Noack 2016/01/21 15:12:32 Nit: Mind making the help messages consistent? Eit
juliandoucette 2016/01/21 16:06:26 Done.
juliandoucette 2016/01/21 16:06:26 Done.
122 else: 126 parser.add_argument('-p', '--port', type=int, default=5000, help='The TCP port the server will listen on')
123 sys.exit("Usage: %s [source_dir]" % sys.argv[0]) 127 args = parser.parse_args()
128
129 source = FileSource(args.path)
130 address = args.address
131 port = args.port
124 132
125 try: 133 try:
126 from werkzeug.serving import ThreadedWSGIServer, run_simple 134 from werkzeug.serving import ThreadedWSGIServer, run_simple
127 135
128 # see https://github.com/mitsuhiko/werkzeug/pull/770 136 # see https://github.com/mitsuhiko/werkzeug/pull/770
129 ThreadedWSGIServer.daemon_threads = True 137 ThreadedWSGIServer.daemon_threads = True
130 138
131 def run(*args, **kwargs): 139 def run(*args, **kwargs):
132 # The werkzeug logger must be configured before the 140 # The werkzeug logger must be configured before the
133 # root logger. Also we must prevent it from propagating 141 # root logger. Also we must prevent it from propagating
(...skipping 17 matching lines...) Expand all
151 try: 159 try:
152 return app(environ, start_response) 160 return app(environ, start_response)
153 except Exception, e: 161 except Exception, e:
154 return show_error(start_response, "500 Internal Server Error", 162 return show_error(start_response, "500 Internal Server Error",
155 uri=environ.get("PATH_INFO"), error=e) 163 uri=environ.get("PATH_INFO"), error=e)
156 164
157 server = make_server(host, port, wrapper, ThreadedWSGIServer) 165 server = make_server(host, port, wrapper, ThreadedWSGIServer)
158 print " * Running on http://%s:%i/" % server.server_address 166 print " * Running on http://%s:%i/" % server.server_address
159 server.serve_forever() 167 server.serve_forever()
160 168
161 run("localhost", 5000, handler, use_reloader=True, use_debugger=True) 169 run(address, port, handler, use_reloader=True, use_debugger=True)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld