From 5e70dfb87eca860c378bcf073ceffc65bf1dbed8 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 6 Jun 2013 15:47:53 +0200 Subject: [PATCH] Rewrite javascript style-checker. --include and --exclude are not working yet. refs #4244 --- test/js/checkswag | 117 +++++++++++++++++++++++++++++++++++++++++++ test/js/checkswag.sh | 13 ----- 2 files changed, 117 insertions(+), 13 deletions(-) create mode 100755 test/js/checkswag delete mode 100755 test/js/checkswag.sh diff --git a/test/js/checkswag b/test/js/checkswag new file mode 100755 index 000000000..824a0339b --- /dev/null +++ b/test/js/checkswag @@ -0,0 +1,117 @@ +#!/usr/bin/env python + +import os +import sys +import subprocess +from optparse import OptionParser, BadOptionError, AmbiguousOptionError + + +APPLICATION = 'jshint' +DEFAULT_ARGS = [] + +REPORT_DIRECTORY = '../../build/log' + + +class PassThroughOptionParser(OptionParser): + """ + An unknown option pass-through implementation of OptionParser. + + When unknown arguments are encountered, bundle with largs and try again, + until rargs is depleted. + + sys.exit(status) will still be called if a known argument is passed + incorrectly (e.g. missing arguments or bad argument types, etc.) + + Borrowed from: http://stackoverflow.com/a/9307174 + """ + def _process_args(self, largs, rargs, values): + while rargs: + try: + OptionParser._process_args(self, largs, rargs, values) + except (BadOptionError, AmbiguousOptionError), error: + largs.append(error.opt_str) + + +def execute_command(command, return_output=False, shell=False): + prog = subprocess.Popen(command, shell=shell, + stdout=subprocess.PIPE + if return_output + else None) + return prog.wait() if not return_output else \ + prog.communicate()[0] + + +def get_report_directory(): + path = os.path.abspath(REPORT_DIRECTORY) + + try: + os.makedirs(REPORT_DIRECTORY) + except OSError: + pass + + return path + + +def get_script_directory(): + return os.path.dirname(os.path.abspath(sys.argv[0])) + + +def parse_commandline(): + parser = PassThroughOptionParser(usage='%prog [options] [additional arguments' + ' for {0}]'.format(APPLICATION)) + parser.add_option('-b', '--build', action='store_true', + help='Enable reporting.') + parser.add_option('-v', '--verbose', action='store_true', + help='Be more verbose.') + parser.add_option('-i', '--include', metavar='PATTERN', action='append', + help='Include only specific files/test cases.' + ' (Can be supplied multiple times.)') + parser.add_option('-e', '--exclude', metavar='PATTERN', action='append', + help='Exclude specific files/test cases. ' + '(Can be supplied multiple times.)') + return parser.parse_args() + + +def main(): + options, arguments = parse_commandline() + + # Environment preparation and verification + os.chdir(get_script_directory()) + application_path = execute_command('which {0}'.format(APPLICATION), + True, True).strip() + if not application_path: + print 'ERROR: {0} not found!'.format(APPLICATION) + return 2 + + # Commandline preparation + command_options = [] + if options.build: + command_options.extend(['--reporter', 'jslint']) + else: + command_options.append('--verbose') + if options.verbose: + command_options.append('--show-non-errors') + if options.exclude: + pass # TODO: Preprocess disallowed files/directories + elif options.include: + pass # TODO: Preprocess allowed files/directories + else: + arguments.extend(['../../application', '../../bin', + '../../library/Icinga']) + + # Application invocation.. + if options.build: + result_data = execute_command([application_path] + DEFAULT_ARGS + + command_options + arguments, True) + result_path = os.path.join(get_report_directory(), + 'jshint_results.xml') + with open(result_path, 'w') as result_file: + result_file.write(result_data) + else: + execute_command([application_path] + DEFAULT_ARGS + + command_options + arguments) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/test/js/checkswag.sh b/test/js/checkswag.sh deleted file mode 100755 index df73f5a5a..000000000 --- a/test/js/checkswag.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh - -set -o nounset - -SCRIPTNAME=$(readlink -f $0) -DIR=$(dirname $SCRIPTNAME) - -# Make sure that the destination directory for logs and reports exists -mkdir -p $DIR/../../build/log - -jshint --reporter=jslint "$@" $DIR/../.. > $DIR/../../build/log/jshint_results.xml - -exit 0