PageRenderTime 66ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/deps/v8/tools/presubmit.py

https://github.com/koichik/node
Python | 435 lines | 399 code | 5 blank | 31 comment | 3 complexity | 079f4bfda8213e82886d287fee39f780 MD5 | raw file
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2012 the V8 project authors. All rights reserved.
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following
  12. # disclaimer in the documentation and/or other materials provided
  13. # with the distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived
  16. # from this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. try:
  30. import hashlib
  31. md5er = hashlib.md5
  32. except ImportError, e:
  33. import md5
  34. md5er = md5.new
  35. import optparse
  36. import os
  37. from os.path import abspath, join, dirname, basename, exists
  38. import pickle
  39. import re
  40. import sys
  41. import subprocess
  42. import multiprocessing
  43. from subprocess import PIPE
  44. # Disabled LINT rules and reason.
  45. # build/include_what_you_use: Started giving false positives for variables
  46. # named "string" and "map" assuming that you needed to include STL headers.
  47. ENABLED_LINT_RULES = """
  48. build/class
  49. build/deprecated
  50. build/endif_comment
  51. build/forward_decl
  52. build/include_order
  53. build/printf_format
  54. build/storage_class
  55. legal/copyright
  56. readability/boost
  57. readability/braces
  58. readability/casting
  59. readability/check
  60. readability/constructors
  61. readability/fn_size
  62. readability/function
  63. readability/multiline_comment
  64. readability/multiline_string
  65. readability/streams
  66. readability/todo
  67. readability/utf8
  68. runtime/arrays
  69. runtime/casting
  70. runtime/deprecated_fn
  71. runtime/explicit
  72. runtime/int
  73. runtime/memset
  74. runtime/mutex
  75. runtime/nonconf
  76. runtime/printf
  77. runtime/printf_format
  78. runtime/references
  79. runtime/rtti
  80. runtime/sizeof
  81. runtime/string
  82. runtime/virtual
  83. runtime/vlog
  84. whitespace/blank_line
  85. whitespace/braces
  86. whitespace/comma
  87. whitespace/comments
  88. whitespace/ending_newline
  89. whitespace/indent
  90. whitespace/labels
  91. whitespace/line_length
  92. whitespace/newline
  93. whitespace/operators
  94. whitespace/parens
  95. whitespace/tab
  96. whitespace/todo
  97. """.split()
  98. LINT_OUTPUT_PATTERN = re.compile(r'^.+[:(]\d+[:)]|^Done processing')
  99. def CppLintWorker(command):
  100. try:
  101. process = subprocess.Popen(command, stderr=subprocess.PIPE)
  102. process.wait()
  103. out_lines = ""
  104. error_count = -1
  105. while True:
  106. out_line = process.stderr.readline()
  107. if out_line == '' and process.poll() != None:
  108. if error_count == -1:
  109. print "Failed to process %s" % command.pop()
  110. return 1
  111. break
  112. m = LINT_OUTPUT_PATTERN.match(out_line)
  113. if m:
  114. out_lines += out_line
  115. error_count += 1
  116. sys.stdout.write(out_lines)
  117. return error_count
  118. except KeyboardInterrupt:
  119. process.kill()
  120. except:
  121. print('Error running cpplint.py. Please make sure you have depot_tools' +
  122. ' in your $PATH. Lint check skipped.')
  123. process.kill()
  124. class FileContentsCache(object):
  125. def __init__(self, sums_file_name):
  126. self.sums = {}
  127. self.sums_file_name = sums_file_name
  128. def Load(self):
  129. try:
  130. sums_file = None
  131. try:
  132. sums_file = open(self.sums_file_name, 'r')
  133. self.sums = pickle.load(sums_file)
  134. except IOError:
  135. # File might not exist, this is OK.
  136. pass
  137. finally:
  138. if sums_file:
  139. sums_file.close()
  140. def Save(self):
  141. try:
  142. sums_file = open(self.sums_file_name, 'w')
  143. pickle.dump(self.sums, sums_file)
  144. finally:
  145. sums_file.close()
  146. def FilterUnchangedFiles(self, files):
  147. changed_or_new = []
  148. for file in files:
  149. try:
  150. handle = open(file, "r")
  151. file_sum = md5er(handle.read()).digest()
  152. if not file in self.sums or self.sums[file] != file_sum:
  153. changed_or_new.append(file)
  154. self.sums[file] = file_sum
  155. finally:
  156. handle.close()
  157. return changed_or_new
  158. def RemoveFile(self, file):
  159. if file in self.sums:
  160. self.sums.pop(file)
  161. class SourceFileProcessor(object):
  162. """
  163. Utility class that can run through a directory structure, find all relevant
  164. files and invoke a custom check on the files.
  165. """
  166. def Run(self, path):
  167. all_files = []
  168. for file in self.GetPathsToSearch():
  169. all_files += self.FindFilesIn(join(path, file))
  170. if not self.ProcessFiles(all_files, path):
  171. return False
  172. return True
  173. def IgnoreDir(self, name):
  174. return name.startswith('.') or name == 'data' or name == 'sputniktests'
  175. def IgnoreFile(self, name):
  176. return name.startswith('.')
  177. def FindFilesIn(self, path):
  178. result = []
  179. for (root, dirs, files) in os.walk(path):
  180. for ignored in [x for x in dirs if self.IgnoreDir(x)]:
  181. dirs.remove(ignored)
  182. for file in files:
  183. if not self.IgnoreFile(file) and self.IsRelevant(file):
  184. result.append(join(root, file))
  185. return result
  186. class CppLintProcessor(SourceFileProcessor):
  187. """
  188. Lint files to check that they follow the google code style.
  189. """
  190. def IsRelevant(self, name):
  191. return name.endswith('.cc') or name.endswith('.h')
  192. def IgnoreDir(self, name):
  193. return (super(CppLintProcessor, self).IgnoreDir(name)
  194. or (name == 'third_party'))
  195. IGNORE_LINT = ['flag-definitions.h']
  196. def IgnoreFile(self, name):
  197. return (super(CppLintProcessor, self).IgnoreFile(name)
  198. or (name in CppLintProcessor.IGNORE_LINT))
  199. def GetPathsToSearch(self):
  200. return ['src', 'include', 'samples', join('test', 'cctest')]
  201. def GetCpplintScript(self, prio_path):
  202. for path in [prio_path] + os.environ["PATH"].split(os.pathsep):
  203. path = path.strip('"')
  204. cpplint = os.path.join(path, "cpplint.py")
  205. if os.path.isfile(cpplint):
  206. return cpplint
  207. return None
  208. def ProcessFiles(self, files, path):
  209. good_files_cache = FileContentsCache('.cpplint-cache')
  210. good_files_cache.Load()
  211. files = good_files_cache.FilterUnchangedFiles(files)
  212. if len(files) == 0:
  213. print 'No changes in files detected. Skipping cpplint check.'
  214. return True
  215. filt = '-,' + ",".join(['+' + n for n in ENABLED_LINT_RULES])
  216. command = [sys.executable, 'cpplint.py', '--filter', filt]
  217. cpplint = self.GetCpplintScript(join(path, "tools"))
  218. if cpplint is None:
  219. print('Could not find cpplint.py. Make sure '
  220. 'depot_tools is installed and in the path.')
  221. sys.exit(1)
  222. command = [sys.executable, cpplint, '--filter', filt]
  223. commands = join([command + [file] for file in files])
  224. count = multiprocessing.cpu_count()
  225. pool = multiprocessing.Pool(count)
  226. try:
  227. results = pool.map_async(CppLintWorker, commands).get(999999)
  228. except KeyboardInterrupt:
  229. print "\nCaught KeyboardInterrupt, terminating workers."
  230. sys.exit(1)
  231. for i in range(len(files)):
  232. if results[i] > 0:
  233. good_files_cache.RemoveFile(files[i])
  234. total_errors = sum(results)
  235. print "Total errors found: %d" % total_errors
  236. good_files_cache.Save()
  237. return total_errors == 0
  238. COPYRIGHT_HEADER_PATTERN = re.compile(
  239. r'Copyright [\d-]*20[0-1][0-9] the V8 project authors. All rights reserved.')
  240. class SourceProcessor(SourceFileProcessor):
  241. """
  242. Check that all files include a copyright notice and no trailing whitespaces.
  243. """
  244. RELEVANT_EXTENSIONS = ['.js', '.cc', '.h', '.py', '.c',
  245. '.status', '.gyp', '.gypi']
  246. # Overwriting the one in the parent class.
  247. def FindFilesIn(self, path):
  248. if os.path.exists(path+'/.git'):
  249. output = subprocess.Popen('git ls-files --full-name',
  250. stdout=PIPE, cwd=path, shell=True)
  251. result = []
  252. for file in output.stdout.read().split():
  253. for dir_part in os.path.dirname(file).replace(os.sep, '/').split('/'):
  254. if self.IgnoreDir(dir_part):
  255. break
  256. else:
  257. if self.IsRelevant(file) and not self.IgnoreFile(file):
  258. result.append(join(path, file))
  259. if output.wait() == 0:
  260. return result
  261. return super(SourceProcessor, self).FindFilesIn(path)
  262. def IsRelevant(self, name):
  263. for ext in SourceProcessor.RELEVANT_EXTENSIONS:
  264. if name.endswith(ext):
  265. return True
  266. return False
  267. def GetPathsToSearch(self):
  268. return ['.']
  269. def IgnoreDir(self, name):
  270. return (super(SourceProcessor, self).IgnoreDir(name)
  271. or (name == 'third_party')
  272. or (name == 'gyp')
  273. or (name == 'out')
  274. or (name == 'obj')
  275. or (name == 'DerivedSources'))
  276. IGNORE_COPYRIGHTS = ['cpplint.py',
  277. 'daemon.py',
  278. 'earley-boyer.js',
  279. 'raytrace.js',
  280. 'crypto.js',
  281. 'libraries.cc',
  282. 'libraries-empty.cc',
  283. 'jsmin.py',
  284. 'regexp-pcre.js',
  285. 'gnuplot-4.6.3-emscripten.js']
  286. IGNORE_TABS = IGNORE_COPYRIGHTS + ['unicode-test.js', 'html-comments.js']
  287. def EndOfDeclaration(self, line):
  288. return line == "}" or line == "};"
  289. def StartOfDeclaration(self, line):
  290. return line.find("//") == 0 or \
  291. line.find("/*") == 0 or \
  292. line.find(") {") != -1
  293. def ProcessContents(self, name, contents):
  294. result = True
  295. base = basename(name)
  296. if not base in SourceProcessor.IGNORE_TABS:
  297. if '\t' in contents:
  298. print "%s contains tabs" % name
  299. result = False
  300. if not base in SourceProcessor.IGNORE_COPYRIGHTS:
  301. if not COPYRIGHT_HEADER_PATTERN.search(contents):
  302. print "%s is missing a correct copyright header." % name
  303. result = False
  304. if ' \n' in contents or contents.endswith(' '):
  305. line = 0
  306. lines = []
  307. parts = contents.split(' \n')
  308. if not contents.endswith(' '):
  309. parts.pop()
  310. for part in parts:
  311. line += part.count('\n') + 1
  312. lines.append(str(line))
  313. linenumbers = ', '.join(lines)
  314. if len(lines) > 1:
  315. print "%s has trailing whitespaces in lines %s." % (name, linenumbers)
  316. else:
  317. print "%s has trailing whitespaces in line %s." % (name, linenumbers)
  318. result = False
  319. # Check two empty lines between declarations.
  320. if name.endswith(".cc"):
  321. line = 0
  322. lines = []
  323. parts = contents.split('\n')
  324. while line < len(parts) - 2:
  325. if self.EndOfDeclaration(parts[line]):
  326. if self.StartOfDeclaration(parts[line + 1]):
  327. lines.append(str(line + 1))
  328. line += 1
  329. elif parts[line + 1] == "" and \
  330. self.StartOfDeclaration(parts[line + 2]):
  331. lines.append(str(line + 1))
  332. line += 2
  333. line += 1
  334. if len(lines) >= 1:
  335. linenumbers = ', '.join(lines)
  336. if len(lines) > 1:
  337. print "%s does not have two empty lines between declarations " \
  338. "in lines %s." % (name, linenumbers)
  339. else:
  340. print "%s does not have two empty lines between declarations " \
  341. "in line %s." % (name, linenumbers)
  342. result = False
  343. return result
  344. def ProcessFiles(self, files, path):
  345. success = True
  346. violations = 0
  347. for file in files:
  348. try:
  349. handle = open(file)
  350. contents = handle.read()
  351. if not self.ProcessContents(file, contents):
  352. success = False
  353. violations += 1
  354. finally:
  355. handle.close()
  356. print "Total violating files: %s" % violations
  357. return success
  358. def GetOptions():
  359. result = optparse.OptionParser()
  360. result.add_option('--no-lint', help="Do not run cpplint", default=False,
  361. action="store_true")
  362. return result
  363. def Main():
  364. workspace = abspath(join(dirname(sys.argv[0]), '..'))
  365. parser = GetOptions()
  366. (options, args) = parser.parse_args()
  367. success = True
  368. print "Running C++ lint check..."
  369. if not options.no_lint:
  370. success = CppLintProcessor().Run(workspace) and success
  371. print "Running copyright header, trailing whitespaces and " \
  372. "two empty lines between declarations check..."
  373. success = SourceProcessor().Run(workspace) and success
  374. if success:
  375. return 0
  376. else:
  377. return 1
  378. if __name__ == '__main__':
  379. sys.exit(Main())