示例#1
0
文件: cli.py 项目: warpnet/salt-lint
def run(args=None):
    """Run the linter and return the exit code."""
    parser = init_argument_parser()
    options = parser.parse_args(args if args is not None else sys.argv[1:])

    stdin_filename = None
    file_names = set(options.files)
    checked_files = set()

    # Read input from STDIN
    if not sys.stdin.isatty():
        with tempfile.NamedTemporaryFile('w', suffix='.sls',
                                         delete=False) as stdin_tmpfile:
            stdin_tmpfile.write(sys.stdin.read())
            stdin_filename = stdin_tmpfile.name
            file_names.add(stdin_filename)

    # Read, parse and validate the configuration
    options_dict = vars(options)
    try:
        config = Configuration(options_dict)
    except SaltLintConfigError as exc:
        print(exc)
        return 2

    # Show a help message on the screen
    if not file_names and not (options.listrules or options.listtags):
        parser.print_help(file=sys.stderr)
        return 1

    # Collect the rules from the configuration
    collection = RulesCollection(config)
    for rulesdir in config.rulesdirs:
        collection.extend(
            RulesCollection.create_from_directory(rulesdir, config))

    # Show the rules listing
    if options.listrules:
        print(collection)
        return 0

    # Show the tags listing
    if options.listtags:
        print(collection.listtags())
        return 0

    formatter = initialize_formatter(config)

    problems = []
    for file_name in file_names:
        runner = Runner(collection, file_name, config, checked_files)
        problems.extend(runner.run())

    # Delete stdin temporary file
    if stdin_filename:
        os.unlink(stdin_filename)

    if problems:
        sorted_problems = sort_problems(problems)
        formatter.process(sorted_problems)
        return 2
    return 0
示例#2
0
def run(args=None):
    """Run the linter and return the exit code."""
    # Wrap `sys.stdout` in an object that automatically encodes an unicode
    # string into utf-8, in Python 2 only. The default encoding for Python 3
    # is already utf-8.
    if sys.version_info[0] < 3:
        sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

    parser = init_argument_parser()
    options = parser.parse_args(args if args is not None else sys.argv[1:])

    stdin_file = None
    file_names = set(options.files)
    checked_files = set()

    # Read input from STDIN
    if not sys.stdin.isatty():
        stdin_file = tempfile.NamedTemporaryFile('w', suffix='.sls', delete=False)
        stdin_file.write(sys.stdin.read())
        stdin_file.flush()
        file_names.add(stdin_file.name)

    # Read, parse and validate the configuration
    options_dict = vars(options)
    try:
        config = Configuration(options_dict)
    except SaltLintConfigError as exc:
        print(exc)
        return 2

    # Show a help message on the screen
    if not file_names and not (options.listrules or options.listtags):
        parser.print_help(file=sys.stderr)
        return 1

    # Collect the rules from the configuration
    collection = RulesCollection(config)
    for rulesdir in config.rulesdirs:
        collection.extend(RulesCollection.create_from_directory(rulesdir, config))

    # Show the rules listing
    if options.listrules:
        print(collection)
        return 0

    # Show the tags listing
    if options.listtags:
        print(collection.listtags())
        return 0

    formatter = initialize_formatter(config)

    problems = []
    for file_name in file_names:
        runner = Runner(collection, file_name, config, checked_files)
        problems.extend(runner.run())

    # Delete stdin temporary file
    if stdin_file:
        os.unlink(stdin_file.name)

    if problems:
        sorted_problems = sort_problems(problems)
        formatter.process(sorted_problems)
        return 2
    return 0