OLD | NEW |
1 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Top-level presubmit script for depot tools. | 5 """Top-level presubmit script for depot tools. |
6 | 6 |
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for | 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for |
8 details on the presubmit API built into gcl. | 8 details on the presubmit API built into gcl. |
9 """ | 9 """ |
10 | 10 |
11 | 11 |
12 def CheckChangeOnUpload(input_api, output_api): | 12 def CheckChangeOnUpload(input_api, output_api): |
13 return RunTests(input_api, output_api) | 13 return RunTests(input_api, output_api) |
14 | 14 |
15 | 15 |
16 def CheckChangeOnCommit(input_api, output_api): | 16 def CheckChangeOnCommit(input_api, output_api): |
17 return RunTests(input_api, output_api) | 17 return RunTests(input_api, output_api) |
18 | 18 |
19 | 19 |
20 def RunTests(input_api, output_api): | 20 def RunTests(input_api, output_api): |
21 """Run all the shells scripts in the directory test. | 21 """Run all the shells scripts in the directory test. |
| 22 """ |
| 23 # Not exposed from InputApi. |
| 24 from os import listdir |
22 | 25 |
23 Also verify the GAE python SDK is available, fetches Rietveld if necessary and | 26 # First loads a local Rietveld instance. |
24 start a test instance to test against. | 27 import sys |
25 """ | 28 old_sys_path = sys.path |
26 # They are not exposed from InputApi. | 29 try: |
27 from os import listdir, pathsep | 30 sys.path = [input_api.PresubmitLocalPath()] + sys.path |
28 import socket | 31 from test import local_rietveld |
29 import time | 32 server = local_rietveld.LocalRietveld() |
| 33 finally: |
| 34 sys.path = old_sys_path |
30 | 35 |
31 # Shortcuts | 36 # Set to True for testing. |
32 join = input_api.os_path.join | |
33 error = output_api.PresubmitError | |
34 | |
35 # Paths | |
36 sdk_path = input_api.os_path.abspath(join('..', '..', 'google_appengine')) | |
37 dev_app = join(sdk_path, 'dev_appserver.py') | |
38 rietveld = join('test', 'rietveld') | |
39 django_path = join(rietveld, 'django') | |
40 | |
41 # Generate a friendly environment. | |
42 env = input_api.environ.copy() | |
43 env['LANGUAGE'] = 'en' | |
44 if env.get('PYTHONPATH'): | |
45 env['PYTHONPATH'] = (env['PYTHONPATH'].rstrip(pathsep) + pathsep + | |
46 django_path) | |
47 else: | |
48 env['PYTHONPATH'] = django_path | |
49 | |
50 def call(*args, **kwargs): | |
51 kwargs['env'] = env | |
52 x = input_api.subprocess.Popen(*args, **kwargs) | |
53 x.communicate() | |
54 return x.returncode == 0 | |
55 | |
56 def test_port(port): | |
57 s = socket.socket() | |
58 try: | |
59 return s.connect_ex(('127.0.0.1', port)) == 0 | |
60 finally: | |
61 s.close() | |
62 | |
63 # First, verify the Google AppEngine SDK is available. | |
64 if not input_api.os_path.isfile(dev_app): | |
65 return [error('Install google_appengine sdk in %s' % sdk_path)] | |
66 | |
67 # Second, checkout rietveld and django if not available. | |
68 if not input_api.os_path.isdir(rietveld): | |
69 print('Checking out rietveld...') | |
70 if not call(['svn', 'co', '-q', | |
71 'http://rietveld.googlecode.com/svn/trunk@563', | |
72 rietveld]): | |
73 return [error('Failed to checkout rietveld')] | |
74 if not input_api.os_path.isdir(django_path): | |
75 print('Checking out django...') | |
76 if not call( | |
77 ['svn', 'co', '-q', | |
78 'http://code.djangoproject.com/' | |
79 'svn/django/branches/releases/1.0.X/django@13637', | |
80 django_path]): | |
81 return [error('Failed to checkout django')] | |
82 | |
83 | |
84 # Test to find an available port starting at 8080. | |
85 port = 8080 | |
86 while test_port(port) and port < 65000: | |
87 port += 1 | |
88 if port == 65000: | |
89 return [error('Having issues finding an available port')] | |
90 | |
91 verbose = False | 37 verbose = False |
92 if verbose: | 38 if verbose: |
93 stdout = None | 39 stdout = None |
94 stderr = None | 40 stderr = None |
95 else: | 41 else: |
96 stdout = input_api.subprocess.PIPE | 42 stdout = input_api.subprocess.PIPE |
97 stderr = input_api.subprocess.PIPE | 43 stderr = input_api.subprocess.STDOUT |
98 output = [] | 44 output = [] |
99 test_server = input_api.subprocess.Popen( | |
100 [dev_app, rietveld, '--port=%d' % port, | |
101 '--datastore_path=' + join(rietveld, 'tmp.db'), '-c'], | |
102 stdout=stdout, stderr=stderr, env=env) | |
103 try: | 45 try: |
104 # Loop until port 127.0.0.1:port opens or the process dies. | 46 # Start a local rietveld instance to test against. |
105 while not test_port(port): | 47 server.start_server() |
106 test_server.poll() | 48 test_path = input_api.os_path.abspath( |
107 if test_server.returncode is not None: | 49 input_api.os_path.join(input_api.PresubmitLocalPath(), 'test')) |
108 output.append(error('Test rietveld instance failed early')) | |
109 break | |
110 time.sleep(0.001) | |
111 | |
112 test_path = input_api.os_path.abspath('test') | |
113 for test in listdir(test_path): | 50 for test in listdir(test_path): |
114 # push-from-logs and rename fails for now. Remove from this list once they | 51 # push-from-logs and rename fails for now. Remove from this list once |
115 # work. | 52 # they work. |
116 if (test in ('push-from-logs.sh', 'rename.sh', 'test-lib.sh') or | 53 if (test in ('push-from-logs.sh', 'rename.sh', 'test-lib.sh') or |
117 not test.endswith('.sh')): | 54 not test.endswith('.sh')): |
118 continue | 55 continue |
| 56 |
119 print('Running %s' % test) | 57 print('Running %s' % test) |
120 if not call([join(test_path, test)], cwd=test_path, stdout=stdout): | 58 proc = input_api.subprocess.Popen( |
121 output.append(error('%s failed' % test)) | 59 [input_api.os_path.join(test_path, test)], |
| 60 cwd=test_path, |
| 61 stdout=stdout, |
| 62 stderr=stderr) |
| 63 proc.communicate() |
| 64 if proc.returncode != 0: |
| 65 output.append(output_api.PresubmitError('%s failed' % test)) |
| 66 except setup_mock.Failure, e: |
| 67 output.append(output_api.PresubmitError('\n'.join(str(i) for i in e.args))) |
122 finally: | 68 finally: |
123 test_server.kill() | 69 server.stop_server() |
124 return output | 70 return output |
OLD | NEW |