Beispiel #1
0
def code_analysis_find_untranslated(options):
    log('title', 'Translations')
    files = _find_files(options, '.*\.pt')
    if not files:
        log('ok')
        return True

    # put all files in a single line
    files = ' '.join(files.strip().split('\n'))
    cmd = [
        options['i18ndude-bin'],
        'find-untranslated',
        files
    ]
    try:
        process = subprocess.Popen(
            cmd,
            stderr=subprocess.STDOUT,
            stdout=subprocess.PIPE
        )
    except OSError:
        log('skip')
        return False
    output, err = process.communicate()
    if '-ERROR-' in output:
        log('failure', output)
        return False
    else:
        log('ok')
        return True
def run_cmd(options, jenkins):
    """Run the jshint command using options.

    Run the jshint command using options and return the output.

    :param options: Options received by the code_analysis_jshint funciton.
    :param jenkins: It is true when the jenkins output is turned on.

    """
    # cmd is a sequence of program arguments
    # first argument is child program
    paths = options['directory'].split('\n')
    cmd = [
        options['jshint-bin'],
        '--verbose',
        '--exclude={0}'.format(options['jshint-exclude'] or ' ')] + paths
    try:
        if jenkins:
            cmd.append('--reporter=jslint')
            output_file_name = os.path.join(options['location'], 'jshint.xml')
            output_file = open(output_file_name, 'w+')
        else:
            output_file = TemporaryFile('w+')

        # Wrapper to subprocess.Popen
        try:
            # Return code is not used for jshint.
            output = read_subprocess_output(cmd, output_file)[0]
            return output
        except OSError:
            log('skip')
            message = 'Command: {0}. Outputfile: {1}'.format(cmd, output_file)
            raise CmdError(message)
    finally:
        output_file.close()
def code_analysis_zptlint(options):
    log('title', 'ZPT Lint')

    files = ''
    for suffix in ('pt', 'cpt', 'zpt', ):
        found_files = _find_files(options, '.*\.{0}'.format(suffix))
        if found_files:
            files += found_files

    if len(files) == 0:
        log('ok')
        return True

    # cmd is a sequence of program arguments
    # first argument is child program
    cmd = [options['zptlint-bin']] + files.split()
    try:
        process = subprocess.Popen(
            cmd,
            stderr=subprocess.STDOUT,
            stdout=subprocess.PIPE
        )
    except OSError:
        log('skip')
        return False
    output, err = process.communicate()
    if output != '':
        log('failure', output)
        return False
    else:
        log('ok')
        return True
def code_analysis_utf8_header(options):
    log('title', 'Check utf-8 headers')

    files = find_files(options, '.*\.py')
    if not files:
        log('ok')
        return True

    errors = []
    file_paths = files.strip().split('\n')
    for file_path in file_paths:
        with open(file_path, 'r') as file_handler:

            lines = file_handler.readlines()
            if len(lines) == 0:
                continue
            elif lines[0].find('coding: utf-8') == -1:
                errors.append('{0}: missing utf-8 header'.format(file_path))

    if len(errors) > 0:
        log('failure')
        for err in errors:
            print(err)
        return False
    else:
        log('ok')
        return True
def code_analysis_clean_lines(options):
    log('title', 'Check clean lines')

    files = ''
    for suffix in ('py', 'pt', 'zcml', 'xml',  # standard plone extensions
                   'js', 'css', 'html',  # html stuff
                   'rst', 'txt',  # documentation
                   ):
        found_files = _find_files(options, '.*\.{0}'.format(suffix))
        if found_files:
            files += found_files

    if len(files) == 0:
        log('ok')
        return True

    total_errors = []
    file_paths = files.strip().split('\n')
    for file_path in file_paths:
        with open(file_path, 'r') as file_handler:
            errors = _code_analysis_clean_lines_parser(
                file_handler.readlines(), file_path)

        if len(errors) > 0:
            total_errors += errors

    if len(total_errors) > 0:
        log('failure')
        for err in total_errors:
            print(err)
        return False
    else:
        log('ok')
        return True
def code_analysis_clean_lines(options):
    log('title', 'Check clean lines')

    file_paths = _code_analysis_clean_lines_files_finder(options)
    if len(file_paths) == 0:
        log('ok')
        return True

    total_errors = []
    for file_path in file_paths:
        with open(file_path, 'r') as file_handler:
            errors = _code_analysis_clean_lines_parser(
                file_handler.readlines(), file_path)

        if len(errors) > 0:
            total_errors += errors

    if len(total_errors) > 0:
        log('failure')
        for err in total_errors:
            print(err)
        return False
    else:
        log('ok')
        return True
def code_analysis_deprecated_aliases(options):
    log('title', 'Deprecated aliases')

    # XXX: advice on usage of the right option
    if options.get('deprecated-methods', 'False') != 'False':
        sys.stdout.write('\ndeprecated-methods option is deprecated; '
                         'use deprecated-aliases instead.')
    if options.get('deprecated-alias', 'False') != 'False':
        sys.stdout.write('\ndeprecated-alias option is deprecated; '
                         'use deprecated-aliases instead.')

    files = _find_files(options, '.*\.py')
    if not files:
        log('ok')
        return True

    total_errors = []
    file_paths = files.strip().split('\n')
    for file_path in file_paths:
        with open(file_path, 'r') as file_handler:
            errors = _code_analysis_deprecated_aliases_lines_parser(
                file_handler.readlines(), file_path)

        if len(errors) > 0:
            total_errors += errors

    if len(total_errors) > 0:
        log('failure')
        for err in total_errors:
            print(err)
        return False
    else:
        log('ok')
        return True
def code_analysis_prefer_single_quotes(options):
    log('title', 'Double quotes')

    files = _find_files(options, '.*\.py')
    if not files:
        log('ok')
        return True

    total_errors = []
    file_paths = files.strip().split('\n')
    for file_path in file_paths:
        with open(file_path, 'r') as file_handler:
            errors = _lines_parser(
                file_handler.readlines(), file_path)

        if len(errors) > 0:
            total_errors += errors

    if len(total_errors) > 0:
        log('failure')
        for err in total_errors:
            print(err)
        return False
    else:
        log('ok')
        return True
def run_cmd(options, jenkins):
    """Run the jscs command using options.

    Run the jscs command using options and return the output.

    :param options: Options received by the code_analysis_jscs funciton.
    :param jenkins: It is true when the jenkins output is turned on.

    """
    # cmd is a sequence of program arguments
    cmd = [options['jscs-bin']]     # , '--reporter=inline', '--no-colors']

    all_files = find_files(options, '.*\.js').strip().split('\n')
    exc_files = find_files({
        'directory': options['jscs-exclude'],
    }, '.*\.js').strip().split('\n')

    # Remove excluded files
    files = set(all_files) - set(exc_files)

    if not files:
        log('ok')
        return ''

    # put all files in a single line
    cmd += list(files)

    try:
        if jenkins:
            cmd.append('--reporter=checkstyle')
            output_file_name = os.path.join(options['location'], 'jscs.xml')
            output_file = open(output_file_name, 'w+')
        else:
            output_file = TemporaryFile('w+')

        # Wrapper to subprocess.Popen
        try:
            # Return code is not used for jscs.
            output = read_subprocess_output(cmd, output_file)[0]
            return output
        except OSError:
            log('skip')
            message = 'Command: {0}. Outputfile: {1}'.format(cmd, output_file)
            raise CmdError(message)
    finally:
        output_file.close()
def code_analysis_jshint(options):
    log('title', 'JSHint')
    jenkins = _normalize_boolean(options['jenkins'])

    # cmd is a sequence of program arguments
    # first argument is child program
    paths = options['directory'].split('\n')
    cmd = [
        options['jshint-bin'],
        '--verbose',
        '--exclude={0}'.format(options['jshint-exclude'] or ' ')] + paths
    try:
        if jenkins:
            cmd.append('--reporter=jslint')
            output_file_name = os.path.join(options['location'], 'jshint.xml')
            output_file = open(output_file_name, 'w+')
        else:
            output_file = TemporaryFile('w+')

        # Wrapper to subprocess.Popen
        try:
            # Return code is not used for jshint.
            output = _read_subprocess_output(cmd, output_file)[0]
        except OSError:
            log('skip')
            return False
    finally:
        output_file.close()

    # HACK: workaround for JSHint limitations
    if jshint_errors(output, jenkins):
        if jenkins:
            log('failure', 'Output file written to %s.' % output_file_name)
        else:
            # Name the pattern to use it in the substitution.
            old, new = '\((?P<name>E\d\d\d)\)', u'(\033[00;31m\g<name>\033[0m)'
            log('failure', _process_output(output, old, new))
        return False
    else:
        log('ok')
        if output != '' and not jenkins:
            print(output)  # XXX: there should be warnings on the output
        return True
def code_analysis_jscs(options):
    log('title', 'JSCS')
    jenkins = normalize_boolean(options['jenkins'])

    try:
        output = run_cmd(options, jenkins)
    except CmdError:
        log('skip')
        return False

    if jscs_errors(output, jenkins):
        if jenkins:
            output_file_name = os.path.join(options['location'], 'jscs.xml')
            log('failure', 'Output file written to %s.' % output_file_name)
        else:
            log('failure', output)
        return False
    else:
        log('ok')
        return True
def code_analysis_csslint(options):
    log('title', 'CSS Lint')
    jenkins = normalize_boolean(options['jenkins'])

    # cmd is a sequence of program arguments
    # first argument is child program
    paths = options['directory'].split('\n')
    cmd = [options['csslint-bin']] + paths

    try:
        if jenkins:
            cmd.insert(1, '--format=lint-xml')
            # Get only errors, no warnings.
            output_file_name = os.path.join(options['location'], 'csslint.xml')
            output_file = open(output_file_name, 'w+')
        else:
            output_file = TemporaryFile('w+')

        # Wrapper to subprocess.Popen
        try:
            # Return code is not used for csslint.
            output = read_subprocess_output(cmd, output_file)[0]
        except OSError:
            log('skip')
            return False
    finally:
        output_file.close()

    if csslint_errors(output, jenkins):
        if jenkins:
            log('failure', 'Output file written to %s.' % output_file_name)
        else:
            # TODO: pass color to _process_output
            # Name the pattern to use it in the substitution.
            old, new = '(?P<name>Error[^ -]*)', '\033[00;31m\g<name>\033[0m'
            log('failure', process_output(output, old, new))
        return False
    else:
        log('ok')
        return True
def code_analysis_flake8(options):
    log('title', 'Flake8')

    jenkins = normalize_boolean(options['jenkins'])
    if 'flake8-filesystem' in options:
        flake8_filesystem = normalize_boolean(options['flake8-filesystem'])
    else:
        flake8_filesystem = False

    # cmd is a sequence of program arguments
    # first argument is child program
    cmd = [
        os.path.join(options['bin-directory']) + '/flake8',
        '--ignore={0}'.format(options['flake8-ignore']),
        '--exclude={0}'.format(options['flake8-exclude']),
        '--max-complexity={0}'.format(options['flake8-max-complexity']),
        '--max-line-length={0}'.format(options['flake8-max-line-length'])
    ]

    paths_to_check = options['directory'].split('\n')
    cmd.extend(paths_to_check)

    try:
        if jenkins:
            output_file_name = os.path.join(options['location'], 'flake8.log')
            output_file = open(output_file_name, 'w+')
        elif flake8_filesystem:
            output_file_name = os.path.join(options['location'], 'flake8.txt')
            output_file = open(output_file_name, 'w+')
        else:
            output_file = TemporaryFile('w+')

        # Wrapper to subprocess.Popen
        try:
            output, return_code = read_subprocess_output(cmd, output_file)
        except OSError:
            log('skip')
            return False
    finally:
        output_file.close()

    if return_code:
        log('failure', output)
        return False
    else:
        log('ok')
        return True
def code_analysis_imports(options):
    log('title', 'Check imports')
    files = find_files(options, '.*\.py')
    if not files:
        log('ok')
        return True

    total_errors = []
    file_paths = files.strip().split('\n')
    for file_path in file_paths:
        with open(file_path, 'r') as file_handler:
            lines = file_handler.readlines()
            total_errors += _code_analysis_imports_parser(lines, file_path)
            total_errors += _code_analysis_imports_sorting(lines, file_path)

    if len(total_errors) > 0:
        log('failure')
        for err in total_errors:
            print(err)
        return False
    else:
        log('ok')
        return True
def code_analysis_imports(options):
    log("title", "Check imports")
    files = _find_files(options, ".*\.py")
    if not files:
        log("ok")
        return True

    total_errors = []
    file_paths = files.strip().split("\n")
    for file_path in file_paths:
        with open(file_path, "r") as file_handler:
            errors = _code_analysis_imports_parser(file_handler.readlines(), file_path)

        if len(errors) > 0:
            total_errors += errors

    if len(total_errors) > 0:
        log("failure")
        for err in total_errors:
            print(err)
        return False
    else:
        log("ok")
        return True
def code_analysis_jshint(options):
    log('title', 'JSHint')
    jenkins = normalize_boolean(options['jenkins'])

    try:
        output = run_cmd(options, jenkins)
    except CmdError:
        log('skip')
        return False

    # HACK: workaround for JSHint limitations
    if jshint_errors(output, jenkins):
        if jenkins:
            output_file_name = os.path.join(options['location'], 'jshint.xml')
            log('failure', 'Output file written to %s.' % output_file_name)
        else:
            # Name the pattern to use it in the substitution.
            old, new = '\((?P<name>E\d\d\d)\)', u'(\033[00;31m\g<name>\033[0m)'
            log('failure', process_output(output, old, new))
        return False
    else:
        log('ok')
        if output != '' and not jenkins:
            # XXX: there should be warnings on the output
            log('warning', output)
        return True