tools/lint/python/check_compat.py
author Gregory Szorc <gps@mozilla.com>
Tue, 07 Nov 2017 16:38:39 -0800
changeset 441455 b5f79b0f2a3586faf692c715b443264478e921c3
parent 427539 ad881fc5e6b01f2c74d3a325b6c622a3ccb606c5
child 539607 d72bc20634561de8374822e149fcec3c6e06db5e
permissions -rwxr-xr-x
Bug 1412932 - Switch to PGO build in Makefile.in; r=ted Previously, client.mk made the decision of whether to perform a PGO build. This required passing around MOZ_PGO and invoking a separate make target if this variable was set. In this commit, we move this logic to Makefile.in. We employ a special mechanism in rules.mk to override the default make target so `make` evaluates "profiledbuild" if MOZ_PGO is set. This also required using an explicit target for $(MAKE) invocations inside the "profiledbuild" rule to avoid infinite recursion. MozReview-Commit-ID: 8sHiVspMisM

#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import, print_function

import ast
import json
import sys


def parse_file(f):
    with open(f, 'rb') as fh:
        content = fh.read()
    try:
        return ast.parse(content)
    except SyntaxError as e:
        err = {
            'path': f,
            'message': e.msg,
            'lineno': e.lineno,
            'column': e.offset,
            'source': e.text,
            'rule': 'is-parseable',
        }
        print(json.dumps(err))


def check_compat_py2(f):
    """Check Python 2 and Python 3 compatibility for a file with Python 2"""
    root = parse_file(f)

    # Ignore empty or un-parseable files.
    if not root or not root.body:
        return

    futures = set()
    haveprint = False
    future_lineno = 1
    for node in ast.walk(root):
        if isinstance(node, ast.ImportFrom):
            if node.module == '__future__':
                future_lineno = node.lineno
                futures |= set(n.name for n in node.names)
        elif isinstance(node, ast.Print):
            haveprint = True

    err = {
        'path': f,
        'lineno': future_lineno,
        'column': 1,
    }

    if 'absolute_import' not in futures:
        err['rule'] = 'require absolute_import'
        err['message'] = 'Missing from __future__ import absolute_import'
        print(json.dumps(err))

    if haveprint and 'print_function' not in futures:
        err['rule'] = 'require print_function'
        err['message'] = 'Missing from __future__ import print_function'
        print(json.dumps(err))


def check_compat_py3(f):
    """Check Python 3 compatibility of a file with Python 3."""
    parse_file(f)


if __name__ == '__main__':
    if sys.version_info[0] == 2:
        fn = check_compat_py2
    else:
        fn = check_compat_py3

    manifest = sys.argv[1]
    with open(manifest, 'r') as fh:
        files = fh.read().splitlines()

    for f in files:
        fn(f)

    sys.exit(0)