summaryrefslogtreecommitdiff
path: root/tools/check-imports.py
diff options
context:
space:
mode:
authorSakthipriyan Vairamani <thechargingvolcano@gmail.com>2016-04-08 00:26:05 +0530
committerSakthipriyan Vairamani <thechargingvolcano@gmail.com>2016-04-25 19:22:50 +0530
commit6781d917f49adfd5a00a7c251277ed9f971f3aee (patch)
treee3f60c8ae89420dd03a2432e659b322189fda552 /tools/check-imports.py
parent75e073f2b2a0f5a5e002b485afd7288c9adecac6 (diff)
downloadnode-new-6781d917f49adfd5a00a7c251277ed9f971f3aee.tar.gz
tools: rewrite check-install.sh in python
As it is, check-install.sh does not show more helpful error messages, and supporting various shells could be a problem. This patch rewrites the same in Python. This patch also enables check-imports.py in the linting process PR-URL: https://github.com/nodejs/node/pull/6105 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'tools/check-imports.py')
-rwxr-xr-xtools/check-imports.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/tools/check-imports.py b/tools/check-imports.py
new file mode 100755
index 0000000000..b71b4a9271
--- /dev/null
+++ b/tools/check-imports.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+from __future__ import print_function
+import glob
+import re
+import sys
+
+
+def do_exist(file_name, lines, imported):
+ if not any(not re.match('using \w+::{0};'.format(imported), line) and
+ re.search(imported, line) for line in lines):
+ print('File "{0}" does not use "{1}"'.format(file_name, imported))
+ return False
+ return True
+
+
+def is_valid(file_name):
+ with open(file_name) as source_file:
+ lines = [line.strip() for line in source_file]
+
+ usings, importeds, line_numbers, valid = [], [], [], True
+ for idx, line in enumerate(lines, 1):
+ matches = re.search(r'^using (\w+::(\w+));$', line)
+ if matches:
+ line_numbers.append(idx)
+ usings.append(matches.group(1))
+ importeds.append(matches.group(2))
+
+ valid = all([do_exist(file_name, lines, imported) for imported in importeds])
+
+ sorted_usings = sorted(usings, key=lambda x: x.lower())
+ if sorted_usings != usings:
+ print("using statements aren't sorted in '{0}'.".format(file_name))
+ for num, actual, expected in zip(line_numbers, usings, sorted_usings):
+ if actual != expected:
+ print('\tLine {0}: Actual: {1}, Expected: {2}'
+ .format(num, actual, expected))
+ return False
+ else:
+ return valid
+
+sys.exit(0 if all(map(is_valid, glob.iglob('src/*.cc'))) else 1)