Exemplo n.º 1
0
def check_file(path):
    from pylama.main import parse_options, process_paths
    from pylama.config import CURDIR

    options = parse_options()
    path = op.relpath(str(path), CURDIR)
    return process_paths(options, candidates=[path], error=False)
Exemplo n.º 2
0
def check_file(path):
    from pylama.main import parse_options, check_files
    from pylama.config import CURDIR

    options = parse_options()
    path = op.relpath(str(path), CURDIR)
    return check_files([path], options, error=False)
Exemplo n.º 3
0
def check_file(path):
    from pylama.main import parse_options, process_paths
    from pylama.config import CURDIR

    options = parse_options()
    path = op.relpath(str(path), CURDIR)
    return process_paths(options, candidates=[path], error=False)
Exemplo n.º 4
0
def code_check():
    """ Run pylama and check current file.

    :return bool:

    """

    with silence_stderr():

        from pylama.main import parse_options
        from pylama.tasks import check_path

        if not env.curbuf.name:
            return env.stop()

        options = parse_options(
            ignore=env.var('g:pymode_lint_ignore'),
            select=env.var('g:pymode_lint_select'),
            linters=env.var('g:pymode_lint_checkers'),
        )

        path = os.path.relpath(env.curbuf.name, env.curdir)
        env.debug("Start code check: ", path)

        if getattr(options, 'skip', None) and any(
                p.match(path) for p in options.skip):  # noqa
            env.message('Skip code checking.')
            env.debug("Skipped")
            return env.stop()

        if env.options.get('debug'):
            from pylama.core import LOGGER, logging
            LOGGER.setLevel(logging.DEBUG)

        errors = check_path(path,
                            options=options,
                            code='\n'.join(env.curbuf) + '\n')

    env.debug("Find errors: ", len(errors))
    sort_rules = env.var('g:pymode_lint_sort')

    def __sort(e):
        try:
            return sort_rules.index(e.get('type'))
        except ValueError:
            return 999

    if sort_rules:
        env.debug("Find sorting: ", sort_rules)
        errors = sorted(errors, key=__sort)

    for e in errors:
        e['bufnr'] = env.curbuf.number

    env.run('g:PymodeLocList.current().extend', errors)
Exemplo n.º 5
0
def code_check():
    """ Run pylama and check current file.

    :return bool:

    """

    with silence_stderr():

        from pylama.main import parse_options
        from pylama.tasks import check_path

        if not env.curbuf.name:
            return env.stop()

        options = parse_options(
            ignore=env.var('g:pymode_lint_ignore'),
            select=env.var('g:pymode_lint_select'),
            linters=env.var('g:pymode_lint_checkers'),
        )
        env.debug(options)

        path = os.path.relpath(env.curbuf.name, env.curdir)
        env.debug("Start code check: ", path)

        if getattr(options, 'skip', None) and any(p.match(path) for p in options.skip): # noqa
            env.message('Skip code checking.')
            env.debug("Skipped")
            return env.stop()

        if env.options.get('debug'):
            from pylama.core import LOGGER, logging
            LOGGER.setLevel(logging.DEBUG)

        errors = check_path(
            path, options=options, code='\n'.join(env.curbuf) + '\n')

    env.debug("Find errors: ", len(errors))
    sort_rules = env.var('g:pymode_lint_sort')

    def __sort(e):
        try:
            return sort_rules.index(e.get('type'))
        except ValueError:
            return 999

    if sort_rules:
        env.debug("Find sorting: ", sort_rules)
        errors = sorted(errors, key=__sort)

    for e in errors:
        e['bufnr'] = env.curbuf.number

    env.run('g:PymodeLocList.current().extend', errors)
Exemplo n.º 6
0
 def test_pylama(self):
     from pylama.main import parse_options, process_paths
     from pylama.config import LOGGER
     LOGGER.setLevel(logging.WARN)
     options = parse_options(['pybsm'], verbose=False)
     errors = process_paths(options, error=False)
     if len(errors):
         if os.environ.get('DEV', None) == '1':
             pytest.skip(msg="Should not have pylama errors but has")
         else:
             pytest.fail(msg="Should not have pylama errors", pytrace=False)
     else:
         self.assertTrue(True, 'pylama with no errors')
Exemplo n.º 7
0
 def test_pylama(self):
     from pylama.main import parse_options, process_paths
     from pylama.config import LOGGER
     LOGGER.setLevel(logging.WARN)
     options = parse_options(['pybsm'],
                             verbose=False
                             )
     errors = process_paths(options, error=False)
     if len(errors):
         if os.environ.get('DEV', None) == '1':
             pytest.skip(msg="Should not have pylama errors but has")
         else:
             pytest.fail(msg="Should not have pylama errors", pytrace=False)
     else:
         self.assertTrue(True, 'pylama with no errors')
Exemplo n.º 8
0
def check_file():
    """ Check current buffer. """
    buf = interface.get_current_buffer()

    linters = interface.get_option('lint_checker')
    ignore = interface.get_option('lint_ignore')
    select = interface.get_option('lint_select')
    complexity = interface.get_option('lint_mccabe_complexity') or '0'

    options = parse_options(
        ignore=ignore, select=select, complexity=complexity, linters=linters)

    add_task(
        run_checkers, callback=parse_result, title='Code checking', buf=buf,
        options=options,
    )
Exemplo n.º 9
0
def check_text(text, temp_root, logger=None):
    """
    check code for style requirements using PyLama
    """
    # creates a new temporary directory to extract the submission
    with tempfile.TemporaryDirectory(dir=temp_root) as temp_dir:
        # creates a temporary file to write the code
        code_file = tempfile.NamedTemporaryFile(suffix='.py',
                                                dir=temp_dir,
                                                mode='w',
                                                delete=False)
        # writes the code to the file
        code_file.write(text)
        # closes the file so it can be removed with the directory
        code_file.close()

        # first checks if the file can be compiled
        # i.e., there are no syntax errors in the file
        compiled = True
        try:
            compile(code_file.name, doraise=True)
        except:
            compiled = False

        # configures and runs pylama to analyze the temp file
        pylama_options = {
            'linters':
            ['pep257', 'pydocstyle', 'pycodestyle', 'pyflakes', 'pylint'],
            'ignore':
            list(ignored_errors.keys())
        }
        pylama_path = temp_dir
        options = parse_options([pylama_path], **pylama_options)
        errors = check_path(options, rootdir='.')

        # parses and sorts the errors received
        results = pylama_parser(errors, compiled)
        results.sort(key=lambda x: (int(x['line']), int(x['place'])))

        if logger:
            logger.debug(results)

    return results
Exemplo n.º 10
0
def test_code_quality():
    """Test various code quality metrics."""
    old_cwd = os.getcwd()
    try:
        root_path = os.path.dirname(os.path.dirname(__file__))
        os.chdir(root_path)

        top_level = get_python_source_paths(root_path)

        options = parse_options(top_level, **PYLAMA_OPTION_OVERRIDES)
        errors = check_path(options, rootdir='.')
        if errors:
            print('-' * 80)

            for error in errors:
                print_pylama_error(error, root_path)
                print('-' * 80)

        assert not errors, "%s code quality errors detected." % len(errors)
    finally:
        os.chdir(old_cwd)
Exemplo n.º 11
0
def check_file():
    """ Check current buffer. """
    buf = interface.get_current_buffer()

    linters = interface.get_option('lint_checker')
    ignore = interface.get_option('lint_ignore')
    select = interface.get_option('lint_select')
    complexity = interface.get_option('lint_mccabe_complexity') or '0'

    options = parse_options(ignore=ignore,
                            select=select,
                            complexity=complexity,
                            linters=linters)

    add_task(
        run_checkers,
        callback=parse_result,
        title='Code checking',
        buf=buf,
        options=options,
    )
Exemplo n.º 12
0
def check_file():
    """ Check current buffer. """
    buf = interface.get_current_buffer()
    rootpath = interface.eval_code('getcwd()')

    async = int(interface.get_option('lint_async'))
    linters = interface.get_option('lint_checker')
    ignore = interface.get_option('lint_ignore')
    select = interface.get_option('lint_select')
    complexity = interface.get_option('lint_mccabe_complexity') or '0'

    options = parse_options(
        ignore=ignore, select=select, complexity=complexity, linters=linters)

    if async:
        add_task(
            run_checkers, callback=parse_result, title='Code checking',
            buf=buf, options=options, rootpath=rootpath,
        )

    else:
        result = run_checkers(buf=buf, options=options, rootpath=rootpath)
        parse_result(result, buf=buf)
Exemplo n.º 13
0
def code_check():
    """Run pylama and check current file.

    :return bool:

    """
    with silence_stderr():

        from pylama.core import run
        from pylama.main import parse_options

        if not env.curbuf.name:
            return env.stop()

        linters = env.var('g:pymode_lint_checkers')
        env.debug(linters)

        options = parse_options(
            linters=linters,
            force=1,
            ignore=env.var('g:pymode_lint_ignore'),
            select=env.var('g:pymode_lint_select'),
        )

        for linter in linters:
            opts = env.var('g:pymode_lint_options_%s' % linter, silence=True)
            if opts:
                options.linters_params[linter] = options.linters_params.get(
                    linter, {})
                options.linters_params[linter].update(opts)

        env.debug(options)

        path = os.path.relpath(env.curbuf.name, env.curdir)
        env.debug("Start code check: ", path)

        if getattr(options, 'skip', None) and any(
                p.match(path) for p in options.skip):  # noqa
            env.message('Skip code checking.')
            env.debug("Skipped")
            return env.stop()

        if env.options.get('debug'):
            from pylama.core import LOGGER, logging
            LOGGER.setLevel(logging.DEBUG)

        errors = run(path, code='\n'.join(env.curbuf) + '\n', options=options)

    env.debug("Find errors: ", len(errors))
    sort_rules = env.var('g:pymode_lint_sort')

    def __sort(e):
        try:
            return sort_rules.index(e.get('type'))
        except ValueError:
            return 999

    if sort_rules:
        env.debug("Find sorting: ", sort_rules)
        errors = sorted(errors, key=__sort)

    for e in errors:
        e._info['bufnr'] = env.curbuf.number
        if e._info['col'] is None:
            e._info['col'] = 1

    env.run('g:PymodeLocList.current().extend', [e._info for e in errors])
Exemplo n.º 14
0
def check_file(path):
    options = parse_options()
    path = op.relpath(str(path), CURDIR)
    return check_paths([path], options, rootdir=CURDIR)
Exemplo n.º 15
0
def lint(path):
    opts = {'linters': ['pyflakes'], 'async': True}
    options = parse_options([path], **opts)
    return check_path(options, rootdir=".")
Exemplo n.º 16
0
def code_check():
    """Run pylama and check current file.

    :return bool:

    """
    with silence_stderr():

        from pylama.core import run
        from pylama.main import parse_options

        if not env.curbuf.name:
            return env.stop()

        linters = env.var('g:pymode_lint_checkers')
        env.debug(linters)

        options = parse_options(
            linters=linters, force=1,
            ignore=env.var('g:pymode_lint_ignore'),
            select=env.var('g:pymode_lint_select'),
        )

        for linter in linters:
            opts = env.var('g:pymode_lint_options_%s' % linter, silence=True)
            if opts:
                options.linters_params[linter] = options.linters_params.get(linter, {})
                options.linters_params[linter].update(opts)
        options.linters_params['pylint']['clear_cache'] = True

        env.debug(options)

        path = os.path.relpath(env.curbuf.name, env.curdir)
        env.debug("Start code check: ", path)

        if getattr(options, 'skip', None) and any(p.match(path) for p in options.skip):  # noqa
            env.message('Skip code checking.')
            env.debug("Skipped")
            return env.stop()

        if env.options.get('debug'):
            from pylama.core import LOGGER, logging
            LOGGER.setLevel(logging.DEBUG)

        errors = run(path, code='\n'.join(env.curbuf) + '\n', options=options)

    env.debug("Find errors: ", len(errors))
    sort_rules = env.var('g:pymode_lint_sort')

    def __sort(e):
        try:
            return sort_rules.index(e.get('type'))
        except ValueError:
            return 999

    if sort_rules:
        env.debug("Find sorting: ", sort_rules)
        errors = sorted(errors, key=__sort)

    for e in errors:
        e._info['bufnr'] = env.curbuf.number
        if e._info['col'] is None:
            e._info['col'] = 1

    env.run('g:PymodeLocList.current().extend', [e._info for e in errors])
Exemplo n.º 17
0
def python_file_to_json_meta(python_file_path):
    """Take python source code string and extract meta-data as json file."""
    python_file_path = os.path.abspath(python_file_path)
    log.debug("INPUT: Reading Python file {0}.".format(python_file_path))
    with open(python_file_path, encoding="utf-8-sig") as python_file:
        python_code, json_meta = python_file.read(), {}
    json_meta["generator"] = __doc__.splitlines()[0] + " " + __version__
    json_meta["relpath"] = os.path.relpath(python_file_path)  # Paths
    json_meta["basename"] = os.path.basename(python_file_path)
    json_meta["dirname"], all_fades = os.path.dirname(python_file_path), []
    json_meta["fullpath"], json_meta["is_index"] = python_file_path, False
    json_meta["lines_total"] = len(python_code.splitlines())  # Statistics
    json_meta["characters"] = len(python_code.replace("\n", ""))
    json_meta["kilobytes"] = int(os.path.getsize(python_file_path) / 1024)
    json_meta["lines_code"] = len([_ for _ in python_code.splitlines() if len(
        _.strip()) and not _.strip().startswith("#")])
    json_meta["words"] = len([_ for _ in re.sub(
        "[^a-zA-Z0-9 ]", "", python_code).split(" ") if _ != ""])
    json_meta["punctuations"] = len(
        [_ for _ in python_code if _ in punctuation])
    json_meta["permissions"] = int(oct(os.stat(python_file_path).st_mode)[-3:])
    json_meta["writable"] = os.access(python_file_path, os.W_OK)
    json_meta["executable"] = os.access(python_file_path, os.X_OK)
    json_meta["readable"] = os.access(python_file_path, os.R_OK)
    json_meta["symlink"] = os.path.islink(python_file_path)
    json_meta["sha1"] = sha1(python_code.encode("utf-8")).hexdigest()
    json_meta["import_procedural"] = "__import__(" in python_code
    json_meta["has_set_trace"] = ".set_trace()" in python_code
    json_meta["has_print"] = "print(" in python_code
    json_meta["has_tab"] = "\t" in python_code
    json_meta["has_shebang"] = re.findall('^#!/.*python', python_code)
    json_meta["accessed"] = datetime.utcfromtimestamp(os.path.getatime(
        python_file_path)).isoformat(" ").split(".")[0]
    json_meta["modified"] = datetime.utcfromtimestamp(os.path.getmtime(
        python_file_path)).isoformat(" ").split(".")[0]
    old_dir = os.getcwd()  # Workaround for misterious file not found on Pylama
    os.chdir(os.path.dirname(python_file_path))
    json_meta["pylama"] = [  # Bugs
        pylama_error.__dict__["_info"]  # dict with PyLama Errors from linters
        for pylama_error in check_path(parse_options([python_file_path]))
        ] if check_path and parse_options else []  # if no PyLama empty list
    os.chdir(old_dir)  # Workaround for misterious file not found on Pylama
    if len(json_meta["pylama"]) and json_meta["lines_total"]:
        json_meta["lines_per_bug"] = int(
            json_meta["lines_total"] / len(json_meta["pylama"]))
    regex_for_todo, all_todo = r"(  # TODO|  # FIXME|  # OPTIMIZE|  # BUG)", []
    for index, line in enumerate(python_code.splitlines()):
        if re.findall(regex_for_todo, line):
            all_todo.append({  # Using same keywords as PyLama array.
                "lnum": index + 1, "text": line.strip(),
                "type": re.findall(regex_for_todo, line)[0].replace(
                    "#", "").strip().lower()})
    if len(all_todo):
        json_meta["todo"] = all_todo  # this is all todo, fixme,etc on the code
    for index, line in enumerate(python_code.splitlines()):
        if re.findall(r"(  # fades)", line):
            all_fades.append({"lnum": index + 1, "text": line.strip(),
                              "type": line.split("#")[1].strip()})
    if len(all_fades):  # Fades: https://github.com/PyAr/fades
        json_meta["fades"] = all_fades  # this is all todo,fixme,etc on code
    json_meta["links"] = re.findall(r"(?P<url>https?://[^\s]+)", python_code)
    for key, value in PyParse().parse_file(python_file_path).items():
        json_meta[key] = value  # "some_code_entity": "value_of_that_entity",
    return json_meta  # return the Big Ol' JSON