Пример #1
0
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
Пример #2
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 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
Пример #4
0
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
Пример #5
0
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
Пример #6
0
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 code_analysis_debug_statements(options):
    sys.stdout.write('Debug statements ')

    sys.stdout.flush()

    # Python files
    files = _find_files(options, '.*\.py')
    if files:
        total_errors = []
        file_paths = files.strip().split('\n')
        for file_path in file_paths:
            with open(file_path, 'r') as file_handler:
                errors = _python_lines_parser(
                    file_handler.readlines(), file_path)

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

    # JavaScript files
    files = _find_files(options, '.*\.js')
    if not files:
        print('      [\033[00;32m OK \033[0m]')
        return True

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

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

    if len(total_errors) > 0:
        print('      [\033[00;31m FAILURE \033[0m]')
        for err in total_errors:
            print(err)
        return False
    else:
        print('      [\033[00;32m OK \033[0m]')
        return True
Пример #8
0
 def test_find_files(self):
     test_dir = mkdtemp()
     temp_files = []
     for n in range(1, 5):
         temp_file = NamedTemporaryFile(
             'w+',
             suffix='.py',
             prefix='tmp' + str(n),
             dir=test_dir)
         temp_files.append(temp_file)
     output = _find_files({'directory': test_dir}, '.*py')
     sorted_output = '\n'.join(sorted(output.splitlines()))
     expect = '\n'.join([x.name for x in temp_files])
     self.assertEqual(expect, sorted_output)
Пример #9
0
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