OLD | NEW |
1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 # for details. All rights reserved. Use of this source code is governed by a | 2 # for details. All rights reserved. Use of this source code is governed by a |
3 # BSD-style license that can be found in the LICENSE file. | 3 # BSD-style license that can be found in the LICENSE file. |
4 | 4 |
| 5 import os |
5 import cpplint | 6 import cpplint |
6 import os | 7 |
| 8 |
| 9 class PathHackException(Exception): |
| 10 def __init__(self, error_msg): |
| 11 self.error_msg = error_msg |
| 12 |
| 13 def __str__(self): |
| 14 return repr(self.error_msg) |
| 15 |
| 16 |
| 17 def TrySvnPathHack(parent_path): |
| 18 orig_path = os.path.join(parent_path, '.svn') |
| 19 renamed_path = os.path.join(parent_path, '.svn_orig') |
| 20 if os.path.exists(renamed_path): |
| 21 error_msg = '".svn_orig" exists in presubmit parent directory(' |
| 22 error_msg += parent_path |
| 23 error_msg += '). Consider renaming it manually to ".svn".' |
| 24 raise PathHackException(error_msg) |
| 25 if os.path.exists(orig_path): |
| 26 # Make the parent SVN directory non-discoverable by cpplint to get |
| 27 # the correct header guard checks. This is needed if using all Dart |
| 28 # checkout. |
| 29 os.rename(orig_path, renamed_path) |
| 30 return lambda: os.rename(renamed_path, orig_path) |
| 31 |
| 32 |
| 33 def TryGitPathHack(filename, parent_path): |
| 34 def CommonSubdirectory(parent, child): |
| 35 while len(child) > len(parent): |
| 36 child, tail = os.path.split(child) |
| 37 if child == parent: |
| 38 return os.path.join(parent, tail) |
| 39 if os.path.exists(os.path.join(parent_path, '.git')): |
| 40 runtime_path = CommonSubdirectory(parent_path, filename) |
| 41 if runtime_path is not None: |
| 42 fake_svn_path = os.path.join(runtime_path, '.svn') |
| 43 if os.path.exists(fake_svn_path): |
| 44 error_msg = '".svn" exists in presubmit parent subdirectory(' |
| 45 error_msg += fake_svn_path |
| 46 error_msg += '). Consider removing it manually.' |
| 47 raise PathHackException(error_msg) |
| 48 # Deposit a file named ".svn" in the runtime directory to fool |
| 49 # cpplint into thinking it is the source root. |
| 50 open(fake_svn_path, 'w').close() |
| 51 return lambda: os.remove(fake_svn_path) |
| 52 |
7 | 53 |
8 def RunLint(input_api, output_api): | 54 def RunLint(input_api, output_api): |
9 result = [] | 55 result = [] |
10 cpplint._cpplint_state.ResetErrorCounts() | 56 cpplint._cpplint_state.ResetErrorCounts() |
11 # Find all .cc and .h files in the change list. | 57 # Find all .cc and .h files in the change list. |
12 for svn_file in input_api.AffectedTextFiles(): | 58 for svn_file in input_api.AffectedTextFiles(): |
13 filename = svn_file.AbsoluteLocalPath() | 59 filename = svn_file.AbsoluteLocalPath() |
14 if filename.endswith('.cc') or filename.endswith('.h'): | 60 if filename.endswith('.cc') or filename.endswith('.h'): |
15 hacked_parent_svn = 0 | 61 cleanup = None |
16 if filename.endswith('.h'): | 62 parent_path = os.path.dirname(input_api.PresubmitLocalPath()) |
17 parent_path = os.path.dirname(input_api.PresubmitLocalPath()) | 63 try: |
18 orig_path = os.path.join(parent_path, '.svn') | 64 if filename.endswith('.h'): |
19 renamed_path = os.path.join(parent_path, '.svn_orig') | 65 cleanup = TrySvnPathHack(parent_path) |
20 if (os.path.exists(renamed_path)): | 66 if cleanup is None: |
21 error_msg = '".svn_orig" exists in presubmit parent directory(' | 67 cleanup = TryGitPathHack(filename, parent_path) |
22 error_msg += parent_path | 68 except PathHackException, exception: |
23 error_msg += '). Consider renaming it manually to ".svn".' | 69 return [output_api.PresubmitError(str(exception))] |
24 result = [output_api.PresubmitError(error_msg)] | |
25 return result | |
26 if (os.path.exists(orig_path)): | |
27 # Make the parent SVN directory non-discoverable by cpplint to get | |
28 # the correct header guard checks. This is needed if using all Dart | |
29 # checkout. | |
30 os.rename(orig_path, renamed_path) | |
31 hacked_parent_svn = 1 | |
32 # Run cpplint on the file. | 70 # Run cpplint on the file. |
33 cpplint.ProcessFile(filename, 1) | 71 cpplint.ProcessFile(filename, 1) |
34 if hacked_parent_svn != 0: | 72 if cleanup is not None: |
35 # Undo hacks from above: Restore the original name. | 73 cleanup() |
36 os.rename(renamed_path, orig_path) | |
37 # Report a presubmit error if any of the files had an error. | 74 # Report a presubmit error if any of the files had an error. |
38 if cpplint._cpplint_state.error_count > 0: | 75 if cpplint._cpplint_state.error_count > 0: |
39 result = [output_api.PresubmitError('Failed cpplint check.')] | 76 result = [output_api.PresubmitError('Failed cpplint check.')] |
40 return result | 77 return result |
41 | 78 |
| 79 |
42 def CheckChangeOnUpload(input_api, output_api): | 80 def CheckChangeOnUpload(input_api, output_api): |
43 return RunLint(input_api, output_api) | 81 return RunLint(input_api, output_api) |
44 | 82 |
| 83 |
45 def CheckChangeOnCommit(input_api, output_api): | 84 def CheckChangeOnCommit(input_api, output_api): |
46 return RunLint(input_api, output_api) | 85 return RunLint(input_api, output_api) |
OLD | NEW |