예제 #1
0
def code_quality(verbose=True,
                 details=False,
                 fix=False,
                 filename=None,
                 top=10,
                 rev=None):
    """
    Check code quality.

    By default this command will analyse each Python file in the project with a variety of tools and display the count
    or details of the violations discovered, sorted by most violations first.

    Default usage:

    $ fab -H localhost code_quality

    :param rev: If not None, act on files changed since this commit. 'cached/staged' and 'working' have special meaning
    :type rev: str or None, default None
    :param top: Display / fix only the top N violating files, a value of 0 will display / fix all files
    :type top: int, default 10
    :param verbose: Display a header and the counts, without this you just get the filenames in order
    :type verbose: bool, default True
    :param details: Display the violations one per line after the count / file summary
    :type details: bool, default False
    :param fix: Run autopep8 aggressively on the displayed file(s)
    :type fix: bool, default False
    :param filename: Don't test/fix the top N, just the specified file
    :type filename: string, valid path to a file, default all files in the project
    :return: None, exit status equals total number of violations
    :rtype: None

    Intended to be temporary until we have improved code quality and have safeguards to maintain it in place.

    """
    # pylint: disable=too-many-arguments

    verbose = coerce_bool(verbose)
    details = coerce_bool(details)
    fix = coerce_bool(fix)
    top = int(top) or -1

    file_list = generate_file_list(filename) if not rev else filelist_from_git(
        rev)
    results = get_tool_results(file_list)

    if fix:
        for item in sort_and_slice(results, top):
            autopep8(item['path_to_file'])
        # Recalculate results after autopep8 to surprise the user the least
        results = get_tool_results(file_list)

    print_results(results, top, verbose, details)
    sys.exit(sum([item['total_violations'] for item in results]))
예제 #2
0
def build_docs(dep_graph=False, apidoc=True):
    """
    Build the documentation locally.

    :param dep_graph: Build the dependency graphs
    :type dep_graph: Bool, default False
    :param apidoc: Build the automatically generated rst files from the source code
    :type apidoc: Bool, default True

    Default usage:

        $ fab -H localhost build_docs

    Implementation:

    First, a dependency graph is generated and converted into an image that is referenced in the development page.

    Next, the sphinx-apidoc command is (usually) run which searches the code. As part of this it loads the modules and
    if this has side-effects then they will be evident. Any documentation strings that make use of Python documentation
    conventions (like parameter specification) or the Restructured Text (RsT) syntax will be extracted.

    Next, the `make html` command is run to generate HTML output. Other formats (epub, pdf) are available.

    .. todo:: support other languages

    """

    apidoc = coerce_bool(apidoc)

    if coerce_bool(dep_graph):
        create_dependency_graphs()

    with virtualenv(VENV_ROOT):
        with hide('running'):

            apidoc_result = 0
            if apidoc:
                run('mkdir -p {}'.format(
                    os.path.join(PROJECT_ROOT, 'docs', 'autodoc')))
                with cd(os.path.join(PROJECT_ROOT, 'docs', 'autodoc')):
                    with settings(warn_only=True):
                        run('rm *.rst')
                with cd(os.path.join(PROJECT_ROOT, 'docs')):
                    apidoc_result = run(
                        'sphinx-apidoc -o autodoc ..').return_code

            with cd(os.path.join(PROJECT_ROOT, 'docs')):
                make_result = run('make html').return_code
                return_code = apidoc_result + make_result

    sys.exit(return_code)