OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
Evan Martin
2011/02/09 00:57:23
Can you add a file-level comment describing what t
| |
5 import os | |
6 import socket | |
7 import subprocess | |
8 import time | |
9 | |
10 | |
11 class Failure(Exception): | |
12 pass | |
13 | |
14 | |
15 def test_port(port): | |
Evan Martin
2011/02/09 00:57:23
comments
I guess this should be TestPort() for Go
| |
16 s = socket.socket() | |
17 try: | |
18 return s.connect_ex(('127.0.0.1', port)) == 0 | |
19 finally: | |
20 s.close() | |
21 | |
22 | |
23 def find_free_port(): | |
24 # Test to find an available port starting at 8080. | |
25 port = 8080 | |
26 max_val = (2<<16) | |
27 while test_port(port) and port < max_val: | |
28 port += 1 | |
29 if port == max_val: | |
30 raise Failure('Having issues finding an available port') | |
31 return port | |
32 | |
33 | |
34 class RietveldMock(object): | |
Evan Martin
2011/02/09 00:57:23
"mock" is a weird name for this because it's kind
| |
35 """Downloads everything needed to run a local instance of Rietveld.""" | |
36 | |
37 def __init__(self, base_dir=None): | |
38 # Paths | |
39 self.base_dir = base_dir | |
40 if not self.base_dir: | |
41 self.base_dir = os.path.dirname(os.path.abspath(__file__)) | |
42 self.base_dir = os.path.realpath(os.path.join(self.base_dir, '..')) | |
43 self.sdk_path = os.path.abspath( | |
44 os.path.join(self.base_dir, '..', '..', 'google_appengine')) | |
45 self.dev_app = os.path.join(self.sdk_path, 'dev_appserver.py') | |
46 self.rietveld = os.path.join(self.base_dir, 'test', 'rietveld') | |
47 self.django_path = os.path.join(self.rietveld, 'django') | |
48 self.test_server = None | |
49 self.port = None | |
50 # Generate a friendly environment. | |
51 self.env = os.environ.copy() | |
52 self.env['LANGUAGE'] = 'en' | |
53 if self.env.get('PYTHONPATH'): | |
54 self.env['PYTHONPATH'] = ( | |
55 self.env['PYTHONPATH'].rstrip(os.pathsep) + os.pathsep + | |
56 self.django_path) | |
57 else: | |
58 self.env['PYTHONPATH'] = self.django_path | |
59 | |
60 def install_prerequisites(self): | |
61 # First, verify the Google AppEngine SDK is available. | |
62 if not os.path.isfile(self.dev_app): | |
63 raise Failure('Install google_appengine sdk in %s' % self.sdk_path) | |
64 | |
65 def call(*args, **kwargs): | |
66 kwargs['env'] = self.env | |
67 x = subprocess.Popen(*args, **kwargs) | |
68 x.communicate() | |
69 return x.returncode == 0 | |
70 | |
71 # Second, checkout rietveld and django if not available. | |
72 if not os.path.isdir(self.rietveld): | |
73 print('Checking out rietveld...') | |
74 if not call(['svn', 'co', '-q', | |
75 'http://rietveld.googlecode.com/svn/trunk@563', | |
76 self.rietveld]): | |
77 raise Failure('Failed to checkout rietveld') | |
78 if not os.path.isdir(self.django_path): | |
79 print('Checking out django...') | |
80 if not call( | |
81 ['svn', 'co', '-q', | |
82 'http://code.djangoproject.com/' | |
83 'svn/django/branches/releases/1.0.X/django@13637', | |
84 self.django_path]): | |
85 raise Failure('Failed to checkout django') | |
86 | |
87 def start_server(self, verbose=False): | |
88 self.install_prerequisites() | |
89 self.port = find_free_port() | |
90 if verbose: | |
91 stdout = None | |
92 stderr = None | |
93 else: | |
94 stdout = subprocess.PIPE | |
95 stderr = subprocess.PIPE | |
96 output = [] | |
97 self.test_server = subprocess.Popen( | |
98 [self.dev_app, self.rietveld, '--port=%d' % self.port, | |
99 '--datastore_path=' + os.path.join(self.rietveld, 'tmp.db'), '-c'], | |
100 stdout=stdout, stderr=stderr, env=self.env) | |
101 # Loop until port 127.0.0.1:port opens or the process dies. | |
102 while not test_port(self.port): | |
103 self.test_server.poll() | |
104 if self.test_server.returncode is not None: | |
105 raise Failure( | |
106 'Test rietveld instance failed early on port %s' % self.port) | |
107 time.sleep(0.001) | |
108 | |
109 def stop_server(self): | |
110 if self.test_server: | |
111 self.test_server.kill() | |
112 self.test_server = None | |
113 self.port = None | |
114 | |
115 | |
116 def main(): | |
117 print RietveldMock().start_server() | |
118 | |
119 | |
120 if __name__ == '__main__': | |
121 main() | |
OLD | NEW |