From e928edad65a269e4f6fe1df06110113439d2ccc5 Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Fri, 8 Apr 2016 00:26:05 +0530 Subject: [PATCH] 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 Reviewed-By: Fedor Indutny Reviewed-By: James M Snell --- Makefile | 1 + tools/check-imports.py | 42 ++++++++++++++++++++++++++++++++++++++++++ tools/check-imports.sh | 36 ------------------------------------ 3 files changed, 43 insertions(+), 36 deletions(-) create mode 100755 tools/check-imports.py delete mode 100755 tools/check-imports.sh diff --git a/Makefile b/Makefile index b95051f994..92fe690cf0 100644 --- a/Makefile +++ b/Makefile @@ -639,6 +639,7 @@ CPPLINT_FILES = $(filter-out $(CPPLINT_EXCLUDE), $(wildcard \ cpplint: @$(PYTHON) tools/cpplint.py $(CPPLINT_FILES) + @$(PYTHON) tools/check-imports.py ifneq ("","$(wildcard tools/eslint/bin/eslint.js)") lint: jslint cpplint 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) diff --git a/tools/check-imports.sh b/tools/check-imports.sh deleted file mode 100755 index 2fb263f211..0000000000 --- a/tools/check-imports.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/sh - -# Copyright (c) 2013, 2014, Ben Noordhuis -# -# Permission to use, copy, modify, and/or distribute this software for any -# purpose with or without fee is hereby granted, provided that the above -# copyright notice and this permission notice appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -SED=sed -UNAME=`uname` - -if [ "$UNAME" = Darwin ] || [ "$UNAME" = FreeBSD ]; then - SED=gsed -fi - -cd `dirname "$0"`/../ - -for FILE in src/*.cc; do - $SED -rne 's/^using (\w+::\w+);$/\1/p' $FILE | sort -c || echo "in $FILE" -done - -for FILE in src/*.cc; do - for IMPORT in `$SED -rne 's/^using (\w+)::(\w+);$/\2/p' $FILE`; do - if ! $SED -re '/^using (\w+)::(\w+);$/d' $FILE | grep -q "$IMPORT"; then - echo "$IMPORT unused in $FILE" - fi - done -done