blob: 2548572c6c9b2ccf5ab59b8ec43b3507f9b8ca22 [file] [log] [blame]
Mandeep Singh Baines116ad102011-04-27 15:16:37 -07001# Copyright (c) 2011 The Chromium OS 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
Ryan Cui9b651632011-05-11 11:38:58 -07005import ConfigParser
Dale Curtis2975c432011-05-03 17:25:20 -07006import json
Mandeep Singh Baines116ad102011-04-27 15:16:37 -07007import os
Ryan Cuiec4d6332011-05-02 14:15:25 -07008import re
Mandeep Singh Bainesa7ffa4b2011-05-03 11:37:02 -07009import sys
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070010import subprocess
11
Ryan Cui1562fb82011-05-09 11:01:31 -070012from errors import (VerifyException, HookFailure, PrintErrorForProject,
13 PrintErrorsForCommit)
Ryan Cuiec4d6332011-05-02 14:15:25 -070014
Ryan Cuiec4d6332011-05-02 14:15:25 -070015
16COMMON_INCLUDED_PATHS = [
17 # C++ and friends
18 r".*\.c$", r".*\.cc$", r".*\.cpp$", r".*\.h$", r".*\.m$", r".*\.mm$",
19 r".*\.inl$", r".*\.asm$", r".*\.hxx$", r".*\.hpp$", r".*\.s$", r".*\.S$",
20 # Scripts
21 r".*\.js$", r".*\.py$", r".*\.sh$", r".*\.rb$", r".*\.pl$", r".*\.pm$",
22 # No extension at all, note that ALL CAPS files are black listed in
23 # COMMON_EXCLUDED_LIST below.
24 r"(^|.*?[\\\/])[^.]+$",
25 # Other
26 r".*\.java$", r".*\.mk$", r".*\.am$",
27]
28
Ryan Cui1562fb82011-05-09 11:01:31 -070029
Ryan Cuiec4d6332011-05-02 14:15:25 -070030COMMON_EXCLUDED_PATHS = [
Ryan Cui31e0c172011-05-04 21:00:45 -070031 # avoid doing source file checks for kernel
32 r"/src/third_party/kernel/",
33 r"/src/third_party/kernel-next/",
Paul Taysomf8b6e012011-05-09 14:32:42 -070034 r"/src/third_party/ktop/",
35 r"/src/third_party/punybench/",
Ryan Cuiec4d6332011-05-02 14:15:25 -070036 r".*\bexperimental[\\\/].*",
37 r".*\b[A-Z0-9_]{2,}$",
38 r".*[\\\/]debian[\\\/]rules$",
Brian Harringd780d602011-10-18 16:48:08 -070039 # for ebuild trees, ignore any caches and manifest data
40 r".*/Manifest$",
41 r".*/metadata/[^/]*cache[^/]*/[^/]+/[^/]+$",
Doug Anderson5bfb6792011-10-25 16:45:41 -070042
43 # ignore profiles data (like overlay-tegra2/profiles)
44 r".*/overlay-.*/profiles/.*",
Ryan Cuiec4d6332011-05-02 14:15:25 -070045]
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070046
Ryan Cui1562fb82011-05-09 11:01:31 -070047
Ryan Cui9b651632011-05-11 11:38:58 -070048_CONFIG_FILE = 'PRESUBMIT.cfg'
49
50
Ryan Cui1562fb82011-05-09 11:01:31 -070051# General Helpers
52
Sean Paulba01d402011-05-05 11:36:23 -040053
Ryan Cui72834d12011-05-05 14:51:33 -070054def _run_command(cmd):
55 """Executes the passed in command and returns raw stdout output."""
56 return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
57
Ryan Cui1562fb82011-05-09 11:01:31 -070058
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070059def _get_hooks_dir():
Ryan Cuiec4d6332011-05-02 14:15:25 -070060 """Returns the absolute path to the repohooks directory."""
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070061 cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd']
Ryan Cui72834d12011-05-05 14:51:33 -070062 return _run_command(cmd).strip()
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070063
Ryan Cui1562fb82011-05-09 11:01:31 -070064
Ryan Cuiec4d6332011-05-02 14:15:25 -070065def _match_regex_list(subject, expressions):
66 """Try to match a list of regular expressions to a string.
67
68 Args:
69 subject: The string to match regexes on
70 expressions: A list of regular expressions to check for matches with.
71
72 Returns:
73 Whether the passed in subject matches any of the passed in regexes.
74 """
75 for expr in expressions:
76 if (re.search(expr, subject)):
77 return True
78 return False
79
Ryan Cui1562fb82011-05-09 11:01:31 -070080
Ryan Cuiec4d6332011-05-02 14:15:25 -070081def _filter_files(files, include_list, exclude_list=[]):
82 """Filter out files based on the conditions passed in.
83
84 Args:
85 files: list of filepaths to filter
86 include_list: list of regex that when matched with a file path will cause it
87 to be added to the output list unless the file is also matched with a
88 regex in the exclude_list.
89 exclude_list: list of regex that when matched with a file will prevent it
90 from being added to the output list, even if it is also matched with a
91 regex in the include_list.
92
93 Returns:
94 A list of filepaths that contain files matched in the include_list and not
95 in the exclude_list.
96 """
97 filtered = []
98 for f in files:
99 if (_match_regex_list(f, include_list) and
100 not _match_regex_list(f, exclude_list)):
101 filtered.append(f)
102 return filtered
103
Ryan Cuiec4d6332011-05-02 14:15:25 -0700104
105# Git Helpers
Ryan Cui1562fb82011-05-09 11:01:31 -0700106
107
Ryan Cui4725d952011-05-05 15:41:19 -0700108def _get_upstream_branch():
109 """Returns the upstream tracking branch of the current branch.
110
111 Raises:
112 Error if there is no tracking branch
113 """
114 current_branch = _run_command(['git', 'symbolic-ref', 'HEAD']).strip()
115 current_branch = current_branch.replace('refs/heads/', '')
116 if not current_branch:
Ryan Cui1562fb82011-05-09 11:01:31 -0700117 raise VerifyException('Need to be on a tracking branch')
Ryan Cui4725d952011-05-05 15:41:19 -0700118
119 cfg_option = 'branch.' + current_branch + '.%s'
120 full_upstream = _run_command(['git', 'config', cfg_option % 'merge']).strip()
121 remote = _run_command(['git', 'config', cfg_option % 'remote']).strip()
122 if not remote or not full_upstream:
Ryan Cui1562fb82011-05-09 11:01:31 -0700123 raise VerifyException('Need to be on a tracking branch')
Ryan Cui4725d952011-05-05 15:41:19 -0700124
125 return full_upstream.replace('heads', 'remotes/' + remote)
126
Ryan Cui1562fb82011-05-09 11:01:31 -0700127
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -0700128def _get_diff(commit):
Ryan Cuiec4d6332011-05-02 14:15:25 -0700129 """Returns the diff for this commit."""
Ryan Cui72834d12011-05-05 14:51:33 -0700130 return _run_command(['git', 'show', commit])
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700131
Ryan Cui1562fb82011-05-09 11:01:31 -0700132
Ryan Cuiec4d6332011-05-02 14:15:25 -0700133def _get_file_diff(file, commit):
134 """Returns a list of (linenum, lines) tuples that the commit touched."""
Ryan Cui72834d12011-05-05 14:51:33 -0700135 output = _run_command(['git', 'show', '-p', '--no-ext-diff', commit, file])
Ryan Cuiec4d6332011-05-02 14:15:25 -0700136
137 new_lines = []
138 line_num = 0
139 for line in output.splitlines():
140 m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line)
141 if m:
142 line_num = int(m.groups(1)[0])
143 continue
144 if line.startswith('+') and not line.startswith('++'):
145 new_lines.append((line_num, line[1:]))
146 if not line.startswith('-'):
147 line_num += 1
148 return new_lines
149
Ryan Cui1562fb82011-05-09 11:01:31 -0700150
Ryan Cuiec4d6332011-05-02 14:15:25 -0700151def _get_affected_files(commit):
152 """Returns list of absolute filepaths that were modified/added."""
Ryan Cui72834d12011-05-05 14:51:33 -0700153 output = _run_command(['git', 'diff', '--name-status', commit + '^!'])
Ryan Cuiec4d6332011-05-02 14:15:25 -0700154 files = []
155 for statusline in output.splitlines():
156 m = re.match('^(\w)+\t(.+)$', statusline.rstrip())
157 # Ignore deleted files, and return absolute paths of files
158 if (m.group(1)[0] != 'D'):
159 pwd = os.getcwd()
160 files.append(os.path.join(pwd, m.group(2)))
161 return files
162
Ryan Cui1562fb82011-05-09 11:01:31 -0700163
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -0700164def _get_commits():
Ryan Cuiec4d6332011-05-02 14:15:25 -0700165 """Returns a list of commits for this review."""
Ryan Cui4725d952011-05-05 15:41:19 -0700166 cmd = ['git', 'log', '%s..' % _get_upstream_branch(), '--format=%H']
Ryan Cui72834d12011-05-05 14:51:33 -0700167 return _run_command(cmd).split()
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -0700168
Ryan Cui1562fb82011-05-09 11:01:31 -0700169
Ryan Cuiec4d6332011-05-02 14:15:25 -0700170def _get_commit_desc(commit):
171 """Returns the full commit message of a commit."""
Sean Paul23a2c582011-05-06 13:10:44 -0400172 return _run_command(['git', 'log', '--format=%s%n%n%b', commit + '^!'])
Ryan Cuiec4d6332011-05-02 14:15:25 -0700173
174
175# Common Hooks
176
Ryan Cui1562fb82011-05-09 11:01:31 -0700177
Ryan Cuiec4d6332011-05-02 14:15:25 -0700178def _check_no_long_lines(project, commit):
179 """Checks that there aren't any lines longer than maxlen characters in any of
180 the text files to be submitted.
181 """
182 MAX_LEN = 80
183
184 errors = []
185 files = _filter_files(_get_affected_files(commit),
186 COMMON_INCLUDED_PATHS,
187 COMMON_EXCLUDED_PATHS)
188
189 for afile in files:
190 for line_num, line in _get_file_diff(afile, commit):
191 # Allow certain lines to exceed the maxlen rule.
192 if (len(line) > MAX_LEN and
193 not 'http://' in line and
194 not 'https://' in line and
195 not line.startswith('#define') and
196 not line.startswith('#include') and
197 not line.startswith('#import') and
198 not line.startswith('#pragma') and
199 not line.startswith('#if') and
200 not line.startswith('#endif')):
201 errors.append('%s, line %s, %s chars' % (afile, line_num, len(line)))
202 if len(errors) == 5: # Just show the first 5 errors.
203 break
204
205 if errors:
206 msg = 'Found lines longer than %s characters (first 5 shown):' % MAX_LEN
Ryan Cui1562fb82011-05-09 11:01:31 -0700207 return HookFailure(msg, errors)
208
Ryan Cuiec4d6332011-05-02 14:15:25 -0700209
210def _check_no_stray_whitespace(project, commit):
211 """Checks that there is no stray whitespace at source lines end."""
212 errors = []
213 files = _filter_files(_get_affected_files(commit),
214 COMMON_INCLUDED_PATHS,
215 COMMON_EXCLUDED_PATHS)
216
217 for afile in files:
218 for line_num, line in _get_file_diff(afile, commit):
219 if line.rstrip() != line:
220 errors.append('%s, line %s' % (afile, line_num))
221 if errors:
Ryan Cui1562fb82011-05-09 11:01:31 -0700222 return HookFailure('Found line ending with white space in:', errors)
223
Ryan Cuiec4d6332011-05-02 14:15:25 -0700224
225def _check_no_tabs(project, commit):
226 """Checks there are no unexpanded tabs."""
227 TAB_OK_PATHS = [
Ryan Cui31e0c172011-05-04 21:00:45 -0700228 r"/src/third_party/u-boot/",
Ryan Cuiec4d6332011-05-02 14:15:25 -0700229 r".*\.ebuild$",
230 r".*\.eclass$",
Elly Jones5ab34192011-11-15 14:57:06 -0500231 r".*/[M|m]akefile$",
232 r".*\.mk$"
Ryan Cuiec4d6332011-05-02 14:15:25 -0700233 ]
234
235 errors = []
236 files = _filter_files(_get_affected_files(commit),
237 COMMON_INCLUDED_PATHS,
238 COMMON_EXCLUDED_PATHS + TAB_OK_PATHS)
239
240 for afile in files:
241 for line_num, line in _get_file_diff(afile, commit):
242 if '\t' in line:
243 errors.append('%s, line %s' % (afile, line_num))
244 if errors:
Ryan Cui1562fb82011-05-09 11:01:31 -0700245 return HookFailure('Found a tab character in:', errors)
246
Ryan Cuiec4d6332011-05-02 14:15:25 -0700247
248def _check_change_has_test_field(project, commit):
249 """Check for a non-empty 'TEST=' field in the commit message."""
David McMahon8f6553e2011-06-10 15:46:36 -0700250 TEST_RE = r'\nTEST=\S+'
Ryan Cuiec4d6332011-05-02 14:15:25 -0700251
Mandeep Singh Baines96a53be2011-05-03 11:10:25 -0700252 if not re.search(TEST_RE, _get_commit_desc(commit)):
Ryan Cui1562fb82011-05-09 11:01:31 -0700253 msg = 'Changelist description needs TEST field (after first line)'
254 return HookFailure(msg)
255
Ryan Cuiec4d6332011-05-02 14:15:25 -0700256
257def _check_change_has_bug_field(project, commit):
David McMahon8f6553e2011-06-10 15:46:36 -0700258 """Check for a correctly formatted 'BUG=' field in the commit message."""
259 BUG_RE = r'\nBUG=([Nn]one|(chrome-os-partner|chromium-os):\d+)'
Ryan Cuiec4d6332011-05-02 14:15:25 -0700260
Mandeep Singh Baines96a53be2011-05-03 11:10:25 -0700261 if not re.search(BUG_RE, _get_commit_desc(commit)):
David McMahon8f6553e2011-06-10 15:46:36 -0700262 msg = ('Changelist description needs BUG field (after first line):\n'
263 'BUG=chromium-os:99999 (for public tracker)\n'
264 'BUG=chrome-os-partner:9999 (for partner tracker)\n'
265 'BUG=None')
Ryan Cui1562fb82011-05-09 11:01:31 -0700266 return HookFailure(msg)
267
Ryan Cuiec4d6332011-05-02 14:15:25 -0700268
Mandeep Singh Bainesa23eb5f2011-05-04 13:43:25 -0700269def _check_change_has_proper_changeid(project, commit):
270 """Verify that Change-ID is present in last paragraph of commit message."""
271 desc = _get_commit_desc(commit)
272 loc = desc.rfind('\nChange-Id:')
273 if loc == -1 or re.search('\n\s*\n\s*\S+', desc[loc:]):
Ryan Cui1562fb82011-05-09 11:01:31 -0700274 return HookFailure('Change-Id must be in last paragraph of description.')
275
Mandeep Singh Bainesa23eb5f2011-05-04 13:43:25 -0700276
Ryan Cuiec4d6332011-05-02 14:15:25 -0700277def _check_license(project, commit):
278 """Verifies the license header."""
279 LICENSE_HEADER = (
280 r".*? Copyright \(c\) 20[-0-9]{2,7} The Chromium OS Authors\. All rights "
281 r"reserved\." "\n"
282 r".*? Use of this source code is governed by a BSD-style license that can "
283 "be\n"
284 r".*? found in the LICENSE file\."
285 "\n"
286 )
287
288 license_re = re.compile(LICENSE_HEADER, re.MULTILINE)
289 bad_files = []
290 files = _filter_files(_get_affected_files(commit),
291 COMMON_INCLUDED_PATHS,
292 COMMON_EXCLUDED_PATHS)
293
294 for f in files:
295 contents = open(f).read()
296 if len(contents) == 0: continue # Ignore empty files
297 if not license_re.search(contents):
298 bad_files.append(f)
299 if bad_files:
Ryan Cui1562fb82011-05-09 11:01:31 -0700300 return HookFailure('License must match:\n%s\n' % license_re.pattern +
301 'Found a bad license header in these files:',
302 bad_files)
Ryan Cuiec4d6332011-05-02 14:15:25 -0700303
304
305# Project-specific hooks
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700306
Ryan Cui1562fb82011-05-09 11:01:31 -0700307
Anton Staaf815d6852011-08-22 10:08:45 -0700308def _run_checkpatch(project, commit, options=[]):
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700309 """Runs checkpatch.pl on the given project"""
310 hooks_dir = _get_hooks_dir()
Anton Staaf815d6852011-08-22 10:08:45 -0700311 cmd = ['%s/checkpatch.pl' % hooks_dir] + options + ['-']
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700312 p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -0700313 output = p.communicate(_get_diff(commit))[0]
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700314 if p.returncode:
Ryan Cui1562fb82011-05-09 11:01:31 -0700315 return HookFailure('checkpatch.pl errors/warnings\n\n' + output)
Ryan Cuiec4d6332011-05-02 14:15:25 -0700316
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700317
Anton Staaf815d6852011-08-22 10:08:45 -0700318def _run_checkpatch_no_tree(project, commit):
319 return _run_checkpatch(project, commit, ['--no-tree'])
320
321
Dale Curtis2975c432011-05-03 17:25:20 -0700322def _run_json_check(project, commit):
323 """Checks that all JSON files are syntactically valid."""
Dale Curtisa039cfd2011-05-04 12:01:05 -0700324 for f in _filter_files(_get_affected_files(commit), [r'.*\.json']):
Dale Curtis2975c432011-05-03 17:25:20 -0700325 try:
326 json.load(open(f))
327 except Exception, e:
Ryan Cui1562fb82011-05-09 11:01:31 -0700328 return HookFailure('Invalid JSON in %s: %s' % (f, e))
Dale Curtis2975c432011-05-03 17:25:20 -0700329
330
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700331# Base
332
Ryan Cui1562fb82011-05-09 11:01:31 -0700333
Ryan Cui9b651632011-05-11 11:38:58 -0700334# A list of hooks that are not project-specific
335_COMMON_HOOKS = [
336 _check_change_has_bug_field,
337 _check_change_has_test_field,
338 _check_change_has_proper_changeid,
339 _check_no_stray_whitespace,
340 _check_no_long_lines,
341 _check_license,
342 _check_no_tabs,
343]
Ryan Cuiec4d6332011-05-02 14:15:25 -0700344
Ryan Cui1562fb82011-05-09 11:01:31 -0700345
Ryan Cui9b651632011-05-11 11:38:58 -0700346# A dictionary of project-specific hooks(callbacks), indexed by project name.
347# dict[project] = [callback1, callback2]
348_PROJECT_SPECIFIC_HOOKS = {
Doug Anderson830216f2011-05-02 10:08:37 -0700349 "chromiumos/third_party/kernel": [_run_checkpatch],
350 "chromiumos/third_party/kernel-next": [_run_checkpatch],
Anton Staaf815d6852011-08-22 10:08:45 -0700351 "chromiumos/third_party/u-boot": [_run_checkpatch_no_tree],
Che-Liang Chiou1eb0ab62011-10-20 16:14:38 +0800352 "chromiumos/platform/u-boot-vboot-integration": [_run_checkpatch_no_tree],
Dale Curtis2975c432011-05-03 17:25:20 -0700353 "chromeos/autotest-tools": [_run_json_check],
Ryan Cui9b651632011-05-11 11:38:58 -0700354}
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700355
Ryan Cui1562fb82011-05-09 11:01:31 -0700356
Ryan Cui9b651632011-05-11 11:38:58 -0700357# A dictionary of flags (keys) that can appear in the config file, and the hook
358# that the flag disables (value)
359_DISABLE_FLAGS = {
360 'stray_whitespace_check': _check_no_stray_whitespace,
361 'long_line_check': _check_no_long_lines,
362 'cros_license_check': _check_license,
363 'tab_check': _check_no_tabs,
364}
365
366
367def _get_disabled_hooks():
368 """Returns a set of hooks disabled by the current project's config file.
369
370 Expects to be called within the project root.
371 """
372 SECTION = 'Hook Overrides'
373 config = ConfigParser.RawConfigParser()
374 try:
375 config.read(_CONFIG_FILE)
376 flags = config.options(SECTION)
377 except ConfigParser.Error:
378 return set([])
379
380 disable_flags = []
381 for flag in flags:
382 try:
383 if not config.getboolean(SECTION, flag): disable_flags.append(flag)
384 except ValueError as e:
385 msg = "Error parsing flag \'%s\' in %s file - " % (flag, _CONFIG_FILE)
386 print msg + str(e)
387
388 disabled_keys = set(_DISABLE_FLAGS.iterkeys()).intersection(disable_flags)
389 return set([_DISABLE_FLAGS[key] for key in disabled_keys])
390
391
392def _get_project_hooks(project):
393 """Returns a list of hooks that need to be run for a project.
394
395 Expects to be called from within the project root.
396 """
397 disabled_hooks = _get_disabled_hooks()
398 hooks = [hook for hook in _COMMON_HOOKS if hook not in disabled_hooks]
399
400 if project in _PROJECT_SPECIFIC_HOOKS:
401 hooks.extend(_PROJECT_SPECIFIC_HOOKS[project])
402
403 return hooks
404
405
406def _run_project_hooks(project):
Ryan Cui1562fb82011-05-09 11:01:31 -0700407 """For each project run its project specific hook from the hooks dictionary.
408
409 Args:
410 project: name of project to run hooks for.
Ryan Cui1562fb82011-05-09 11:01:31 -0700411
412 Returns:
413 Boolean value of whether any errors were ecountered while running the hooks.
414 """
Ryan Cui72834d12011-05-05 14:51:33 -0700415 proj_dir = _run_command(['repo', 'forall', project, '-c', 'pwd']).strip()
Ryan Cuiec4d6332011-05-02 14:15:25 -0700416 pwd = os.getcwd()
417 # hooks assume they are run from the root of the project
418 os.chdir(proj_dir)
419
Ryan Cuifa55df52011-05-06 11:16:55 -0700420 try:
421 commit_list = _get_commits()
Don Garrettdba548a2011-05-05 15:17:14 -0700422 except VerifyException as e:
Ryan Cui1562fb82011-05-09 11:01:31 -0700423 PrintErrorForProject(project, HookFailure(str(e)))
424 os.chdir(pwd)
425 return True
Ryan Cuifa55df52011-05-06 11:16:55 -0700426
Ryan Cui9b651632011-05-11 11:38:58 -0700427 hooks = _get_project_hooks(project)
Ryan Cui1562fb82011-05-09 11:01:31 -0700428 error_found = False
Ryan Cuifa55df52011-05-06 11:16:55 -0700429 for commit in commit_list:
Ryan Cui1562fb82011-05-09 11:01:31 -0700430 error_list = []
Ryan Cui9b651632011-05-11 11:38:58 -0700431 for hook in hooks:
Ryan Cui1562fb82011-05-09 11:01:31 -0700432 hook_error = hook(project, commit)
433 if hook_error:
434 error_list.append(hook_error)
435 error_found = True
436 if error_list:
437 PrintErrorsForCommit(project, commit, _get_commit_desc(commit),
438 error_list)
Don Garrettdba548a2011-05-05 15:17:14 -0700439
Ryan Cuiec4d6332011-05-02 14:15:25 -0700440 os.chdir(pwd)
Ryan Cui1562fb82011-05-09 11:01:31 -0700441 return error_found
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700442
Ryan Cui72834d12011-05-05 14:51:33 -0700443
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700444# Main
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -0700445
Ryan Cui1562fb82011-05-09 11:01:31 -0700446
Anush Elangovan63afad72011-03-23 00:41:27 -0700447def main(project_list, **kwargs):
Ryan Cui1562fb82011-05-09 11:01:31 -0700448 found_error = False
449 for project in project_list:
Ryan Cui9b651632011-05-11 11:38:58 -0700450 if _run_project_hooks(project):
Ryan Cui1562fb82011-05-09 11:01:31 -0700451 found_error = True
452
453 if (found_error):
454 msg = ('Preupload failed due to errors in project(s). HINTS:\n'
Ryan Cui9b651632011-05-11 11:38:58 -0700455 '- To disable some source style checks, and for other hints, see '
456 '<checkout_dir>/src/repohooks/README\n'
457 '- To upload only current project, run \'repo upload .\'')
Ryan Cui1562fb82011-05-09 11:01:31 -0700458 print >> sys.stderr, msg
Don Garrettdba548a2011-05-05 15:17:14 -0700459 sys.exit(1)
Anush Elangovan63afad72011-03-23 00:41:27 -0700460
Ryan Cui1562fb82011-05-09 11:01:31 -0700461
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -0700462if __name__ == '__main__':
463 main()