Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(535)

Side by Side Diff: testing_support/local_rietveld.py

Issue 1959193002: Fix depot_tools presubmit's local_rietveld script. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: gitignore Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« fetch.py ('K') | « testing_support/get_appengine.py ('k') | 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 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Setups a local Rietveld instance to test against a live server for 6 """Setups a local Rietveld instance to test against a live server for
7 integration tests. 7 integration tests.
8 8
9 It makes sure Google AppEngine SDK is found, download Rietveld and Django code 9 It makes sure Google AppEngine SDK is found, download Rietveld and Django code
10 if necessary and starts the server on a free inbound TCP port. 10 if necessary and starts the server on a free inbound TCP port.
11 """ 11 """
12 12
13 import logging 13 import logging
14 import optparse 14 import optparse
15 import os 15 import os
16 import shutil 16 import shutil
17 import socket 17 import socket
18 import sys 18 import sys
19 import tempfile 19 import tempfile
20 import time 20 import time
21 21
22 try: 22 try:
23 import subprocess2 23 import subprocess2
24 except ImportError: 24 except ImportError:
25 sys.path.append( 25 sys.path.append(
26 os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) 26 os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
27 import subprocess2 27 import subprocess2
28 28
29 DEPOT_TOOLS=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
dsansome 2016/05/10 07:37:45 dirname of dirname? Also nit: probably put spaces
29 30
30 class Failure(Exception): 31 class Failure(Exception):
31 pass 32 pass
32 33
33 34
34 def test_port(port): 35 def test_port(port):
35 s = socket.socket() 36 s = socket.socket()
36 try: 37 try:
37 return s.connect_ex(('127.0.0.1', port)) == 0 38 return s.connect_ex(('127.0.0.1', port)) == 0
38 finally: 39 finally:
39 s.close() 40 s.close()
40 41
41 42
42 def find_free_port(start_port): 43 def find_free_port(start_port):
43 """Search for a free port starting at specified port.""" 44 """Search for a free port starting at specified port."""
44 for port in xrange(start_port, (2<<16)): 45 for port in xrange(start_port, (2<<16)):
45 if not test_port(port): 46 if not test_port(port):
46 return port 47 return port
47 raise Failure('Having issues finding an available port') 48 raise Failure('Having issues finding an available port')
48 49
49 50
50 class LocalRietveld(object): 51 class LocalRietveld(object):
51 """Downloads everything needed to run a local instance of Rietveld.""" 52 """Downloads everything needed to run a local instance of Rietveld."""
52 53
53 def __init__(self, base_dir=None): 54 def __init__(self, base_dir=None):
54 # Paths 55 # Paths
55 self.base_dir = base_dir 56 self.base_dir = base_dir
56 if not self.base_dir: 57 if not self.base_dir:
57 self.base_dir = os.path.dirname(os.path.abspath(__file__)) 58 self.base_dir = os.path.dirname(os.path.abspath(__file__))
58 # TODO(maruel): This should be in /tmp but that would mean having to fetch
59 # everytime. This test is already annoyingly slow.
60 self.rietveld = os.path.join(self.base_dir, '_rietveld') 59 self.rietveld = os.path.join(self.base_dir, '_rietveld')
60 self.infra = os.path.join(self.base_dir, '_infra')
61 self.rietveld_app = os.path.join( 61 self.rietveld_app = os.path.join(
62 self.rietveld, 'appengine', 'chromium_rietveld') 62 self.infra, 'infra', 'appengine', 'chromium_rietveld')
63 self.dev_app = os.path.join(
64 self.infra, 'google_appengine', 'dev_appserver.py')
63 self.test_server = None 65 self.test_server = None
64 self.port = None 66 self.port = None
65 self.tempdir = None 67 self.tempdir = None
66 self.dev_app = None
67 68
68 def install_prerequisites(self): 69 def install_prerequisites(self):
69 # First, install the Google AppEngine SDK. 70 if os.path.exists(self.rietveld):
70 cmd = [os.path.join(self.base_dir, 'get_appengine.py'), 71 print "Removing old rietveld dir"
71 '--dest=%s' % self.base_dir]
72 try:
73 subprocess2.check_call(cmd)
74 except (OSError, subprocess2.CalledProcessError), e:
75 raise Failure('Failed to run %s\n%s' % (cmd, e))
76 sdk_path = os.path.join(self.base_dir, 'google_appengine')
77 self.dev_app = os.path.join(sdk_path, 'dev_appserver.py')
78
79 if os.path.isdir(os.path.join(self.rietveld, '.hg')):
80 # Left over from mercurial. Delete it.
81 print('Deleting deprecated mercurial rietveld files...')
82 shutil.rmtree(self.rietveld) 72 shutil.rmtree(self.rietveld)
83 73
84 # Second, checkout rietveld if not available. 74 sdk_path = os.path.join(self.base_dir, 'google_appengine')
85 if not os.path.isdir(self.rietveld): 75 if os.path.exists(sdk_path):
86 print('Checking out rietveld...') 76 print "Removing old appengine SDK dir"
87 try: 77 shutil.rmtree(sdk_path)
88 subprocess2.check_call(['git', 'init', self.rietveld]) 78
89 subprocess2.check_call( 79 previous = os.environ.get('DEPOT_TOOLS_UPDATE')
90 ['git', 'remote', 'add', '-f', 'origin', 80 os.environ['DEPOT_TOOLS_UPDATE'] = '0'
91 'https://chromium.googlesource.com/infra/infra.git'], 81 try:
92 cwd=self.rietveld) 82 if not os.path.isfile(os.path.join(self.infra, '.gclient')):
93 subprocess2.check_call( 83 print('Checking out infra...')
94 ['git', 'config', 'core.sparseCheckout', 'true'], 84 shutil.rmtree(self.infra, ignore_errors=True)
95 cwd=self.rietveld) 85 try:
96 with file(os.path.join(self.rietveld, '.git/info/sparse-checkout'), 86 os.makedirs(self.infra)
97 'w') as sparse_file: 87 subprocess2.call(
98 sparse_file.write('appengine/chromium_rietveld') 88 [sys.executable, os.path.join(DEPOT_TOOLS, 'fetch.py'),
99 subprocess2.check_call( 89 '--force', 'infra', '--managed=true'],
100 ['git', 'pull', 'origin', 'master'], 90 cwd=self.infra)
101 cwd=self.rietveld) 91 except (OSError, subprocess2.CalledProcessError), e:
102 except (OSError, subprocess2.CalledProcessError), e: 92 raise Failure('Failed to clone infra. \n%s' % e)
103 raise Failure('Failed to clone rietveld. \n%s' % e) 93 else:
104 else: 94 print('Syncing infra...')
105 print('Syncing rietveld...') 95 try:
106 try: 96 subprocess2.call(
107 subprocess2.check_call( 97 [sys.executable, os.path.join(DEPOT_TOOLS, 'gclient.py'),
108 ['git', 'pull', 'origin', 'master'], 98 'sync', '--force'],
109 cwd=self.rietveld) 99 cwd=self.infra)
110 except (OSError, subprocess2.CalledProcessError), e: 100 except (OSError, subprocess2.CalledProcessError), e:
111 raise Failure('Failed to sync rietveld\n%s' % e) 101 raise Failure('Failed to sync infra. \n%s' % e)
102 finally:
103 if previous is None:
104 del os.environ['DEPOT_TOOLS_UPDATE']
105 else:
106 os.environ['DEPOT_TOOLS_UPDATE'] = previous
112 107
113 def start_server(self, verbose=False): 108 def start_server(self, verbose=False):
114 self.install_prerequisites() 109 self.install_prerequisites()
115 assert not self.tempdir 110 assert not self.tempdir
116 self.tempdir = tempfile.mkdtemp(prefix='rietveld_test') 111 self.tempdir = tempfile.mkdtemp(prefix='rietveld_test')
117 self.port = find_free_port(10000) 112 self.port = find_free_port(10000)
118 admin_port = find_free_port(self.port + 1) 113 admin_port = find_free_port(self.port + 1)
119 if verbose: 114 if verbose:
120 stdout = stderr = None 115 stdout = stderr = None
121 else: 116 else:
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 instance.start_server(verbose=options.verbose) 174 instance.start_server(verbose=options.verbose)
180 print 'Local rietveld instance started on port %d' % instance.port 175 print 'Local rietveld instance started on port %d' % instance.port
181 while True: 176 while True:
182 time.sleep(0.1) 177 time.sleep(0.1)
183 finally: 178 finally:
184 instance.stop_server() 179 instance.stop_server()
185 180
186 181
187 if __name__ == '__main__': 182 if __name__ == '__main__':
188 main() 183 main()
OLDNEW
« fetch.py ('K') | « testing_support/get_appengine.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698