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: changed defaults, variable names, and updated get_page Created Jan. 21, 2016, 1:45 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 = 'localhost'
31 port = '5000'
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://" + address + ":" + str(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.')
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('-n', '--address', default='localhost', help='Address of t he interface the server will listen to.')
122 else: 126 parser.add_argument('-p', '--port', type=int, default=5000, help='Port number. ')
123 sys.exit("Usage: %s [source_dir]" % sys.argv[0]) 127 parser.add_argument('-l', '--listen', help='Address and port number separated by a semicolin. See help for address and port number for more information.')
128 args = parser.parse_args()
129
130 source = FileSource(args.path)
131 address = args.address
132 port = args.port
133
134 if (args.listen):
135 listen = str.split(args.listen, ':')
136 if (len(listen) > 1):
137 port = int(listen[1])
138 address = listen[0]
124 139
125 try: 140 try:
126 from werkzeug.serving import ThreadedWSGIServer, run_simple 141 from werkzeug.serving import ThreadedWSGIServer, run_simple
127 142
128 # see https://github.com/mitsuhiko/werkzeug/pull/770 143 # see https://github.com/mitsuhiko/werkzeug/pull/770
129 ThreadedWSGIServer.daemon_threads = True 144 ThreadedWSGIServer.daemon_threads = True
130 145
131 def run(*args, **kwargs): 146 def run(*args, **kwargs):
132 # The werkzeug logger must be configured before the 147 # The werkzeug logger must be configured before the
133 # root logger. Also we must prevent it from propagating 148 # root logger. Also we must prevent it from propagating
(...skipping 17 matching lines...) Expand all
151 try: 166 try:
152 return app(environ, start_response) 167 return app(environ, start_response)
153 except Exception, e: 168 except Exception, e:
154 return show_error(start_response, "500 Internal Server Error", 169 return show_error(start_response, "500 Internal Server Error",
155 uri=environ.get("PATH_INFO"), error=e) 170 uri=environ.get("PATH_INFO"), error=e)
156 171
157 server = make_server(host, port, wrapper, ThreadedWSGIServer) 172 server = make_server(host, port, wrapper, ThreadedWSGIServer)
158 print " * Running on http://%s:%i/" % server.server_address 173 print " * Running on http://%s:%i/" % server.server_address
159 server.serve_forever() 174 server.serve_forever()
160 175
161 run("localhost", 5000, handler, use_reloader=True, use_debugger=True) 176 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