Example #1
0
    def test_run_on_non_ascii_chars(self):
        s = (u'- hétérogénéité\n'
             u'# 19.99 €\n')
        linter.run(s, self.fake_config())
        linter.run(s.encode('utf-8'), self.fake_config())
        linter.run(s.encode('iso-8859-15'), self.fake_config())

        s = (u'- お早う御座います。\n'
             u'# الأَبْجَدِيَّة العَرَبِيَّة\n')
        linter.run(s, self.fake_config())
        linter.run(s.encode('utf-8'), self.fake_config())
Example #2
0
def validate_yaml(yaml_in, yaml_fn):
    """Check with yamllint the yaml syntaxes
    Looking for duplicate keys."""
    try:
        import yamllint.linter as linter
        from yamllint.config import YamlLintConfig
    except ImportError:
        return
    conf = """{"extends": "relaxed",
               "rules": {"trailing-spaces": {"level": "warning"},
                         "new-lines": {"level": "warning"},
                         "new-line-at-end-of-file": {"level": "warning"}}}"""
    if utils.file_exists(yaml_in):
        with open(yaml_in) as in_handle:
            yaml_in = in_handle.read()
    out = linter.run(yaml_in, YamlLintConfig(conf))

    for problem in out:
        msg = '%(fn)s:%(line)s:%(col)s: [%(level)s] %(msg)s' % {'fn': yaml_fn,
                                                                'line': problem.line,
                                                                'col': problem.column,
                                                                'level': problem.level,
                                                                'msg': problem.message}
        if problem.level == "error":
            raise ValueError(msg)
Example #3
0
 def check_yaml(self, conf, path, contents):
     """
     :type conf: YamlLintConfig
     :type path: str
     :type contents: str
     """
     self.messages += [self.result_to_message(r, path) for r in linter.run(contents, conf, path)]
Example #4
0
    def run(self):
        ''' run command '''
        if self.excludes is not None:
            print("Excludes:\n{0}".format(yaml.dump(self.excludes, default_flow_style=False)))

        config = YamlLintConfig(file=self.config_file)

        has_errors = False
        has_warnings = False

        if self.format == 'parsable':
            format_method = Format.parsable
        else:
            format_method = Format.standard_color

        for yaml_file in find_files(os.getcwd(), self.excludes, None, r'\.ya?ml$'):
            first = True
            with open(yaml_file, 'r') as contents:
                for problem in linter.run(contents, config):
                    if first and self.format != 'parsable':
                        print('\n{0}:'.format(os.path.relpath(yaml_file)))
                        first = False

                    print(format_method(problem, yaml_file))
                    if problem.level == linter.PROBLEM_LEVELS[2]:
                        has_errors = True
                    elif problem.level == linter.PROBLEM_LEVELS[1]:
                        has_warnings = True

        if has_errors or has_warnings:
            print('yammlint issues found')
            raise SystemExit(1)
Example #5
0
    def check(self, source, conf, **kwargs):
        expected_problems = []
        for key in kwargs:
            assert key.startswith('problem')
            if len(kwargs[key]) > 2:
                if kwargs[key][2] == 'syntax':
                    rule_id = None
                else:
                    rule_id = kwargs[key][2]
            else:
                rule_id = self.rule_id
            expected_problems.append(linter.LintProblem(
                kwargs[key][0], kwargs[key][1], rule=rule_id))
        expected_problems.sort()

        real_problems = list(linter.run(source, self.build_fake_config(conf)))
        self.assertEqual(real_problems, expected_problems)
Example #6
0
def _check_yaml_file(yaml_fn):
    """Check with yamllint the yaml syntaxes
    Looking for duplicate keys."""
    try:
        import yamllint.linter as linter
        from yamllint.config import YamlLintConfig
        out = linter.run(open(yaml_fn), YamlLintConfig('extends: relaxed'))
        for problem in out:
            msg = '%(fn)s:%(line)s:%(col)s: [%(level)s] %(msg)s' % {'fn': yaml_fn,
                                                                    'line': problem.line,
                                                                    'col': problem.column,
                                                                    'level': problem.level,
                                                                    'msg': problem.message}
            if problem.level == "error":
                raise ValueError(msg)
    except ImportError:
        pass
Example #7
0
    def check_module(self, conf, path, contents):
        """
        :type conf: YamlLintConfig
        :type path: str
        :type contents: str
        """
        docs = self.get_module_docs(path, contents)

        for key, value in docs.items():
            yaml = value['yaml']
            lineno = value['lineno']

            if yaml.startswith('\n'):
                yaml = yaml[1:]
                lineno += 1

            messages = list(linter.run(yaml, conf, path))

            self.messages += [self.result_to_message(r, path, lineno - 1, key) for r in messages]
Example #8
0
    def parse(self, doc, options):
        doc.diagnostics = []

        # load the yamllint config file, if exists
        config = get_yamllint_config(doc.path)

        with open(doc.data_path) as f:
            for problem in linter.run(f, config, doc.data_path):
                loc = types.SourceLocation(line=problem.line, column=problem.column)

                severity = types.Diagnostic.Severity.INFO
                if problem.level == 'warning':
                    severity = types.Diagnostic.Severity.WARNING
                elif problem.level == 'error':
                    severity = types.Diagnostic.Severity.ERROR

                doc.diagnostics.append(types.Diagnostic(severity=severity,
                                                        locations=[loc.to_range()],
                                                        message=problem.message))
Example #9
0
def run(argv=None):
    parser = argparse.ArgumentParser(prog=APP_NAME,
                                     description=APP_DESCRIPTION)
    parser.add_argument('files', metavar='FILE_OR_DIR', nargs='+',
                        help='files to check')
    parser.add_argument('-c', '--config-file', dest='config_file',
                        action='store', help='path to a custom configuration')
    parser.add_argument('-d', '--config-data', dest='config_data',
                        action='store',
                        help='custom configuration (as YAML source)')
    parser.add_argument('-f', '--format',
                        choices=('parsable', 'standard'), default='standard',
                        help='format for parsing output')
    parser.add_argument('-v', '--version', action='version',
                        version='%s %s' % (APP_NAME, APP_VERSION))

    # TODO: read from stdin when no filename?

    args = parser.parse_args(argv)

    if args.config_file is not None and args.config_data is not None:
        print('Options --config-file and --config-data cannot be used '
              'simultaneously.', file=sys.stderr)
        sys.exit(-1)

    # User-global config is supposed to be in ~/.config/yamllint/config
    if 'XDG_CONFIG_HOME' in os.environ:
        user_global_config = os.path.join(
            os.environ['XDG_CONFIG_HOME'], 'yamllint', 'config')
    else:
        user_global_config = os.path.expanduser('~/.config/yamllint/config')

    try:
        if args.config_data is not None:
            if args.config_data != '' and ':' not in args.config_data:
                args.config_data = 'extends: ' + args.config_data
            conf = YamlLintConfig(content=args.config_data)
        elif args.config_file is not None:
            conf = YamlLintConfig(file=args.config_file)
        elif os.path.isfile('.yamllint'):
            conf = YamlLintConfig(file='.yamllint')
        elif os.path.isfile(user_global_config):
            conf = YamlLintConfig(file=user_global_config)
        else:
            conf = YamlLintConfig('extends: default')
    except YamlLintConfigError as e:
        print(e, file=sys.stderr)
        sys.exit(-1)

    return_code = 0

    for file in find_files_recursively(args.files):
        try:
            first = True
            with open(file) as f:
                for problem in linter.run(f, conf):
                    if args.format == 'parsable':
                        print(Format.parsable(problem, file))
                    else:
                        if first:
                            print('\033[4m%s\033[0m' % file)
                            first = False

                        print(Format.standard(problem, file))

                    if return_code == 0 and problem.level == 'error':
                        return_code = 1

            if not first and args.format != 'parsable':
                print('')
        except EnvironmentError as e:
            print(e, file=sys.stderr)
            return_code = -1

    sys.exit(return_code)
Example #10
0
def run(argv=None):
    parser = argparse.ArgumentParser(prog=APP_NAME,
                                     description=APP_DESCRIPTION)
    parser.add_argument('files', metavar='FILE_OR_DIR', nargs='+',
                        help='files to check')
    config_group = parser.add_mutually_exclusive_group()
    config_group.add_argument('-c', '--config-file', dest='config_file',
                              action='store',
                              help='path to a custom configuration')
    config_group.add_argument('-d', '--config-data', dest='config_data',
                              action='store',
                              help='custom configuration (as YAML source)')
    parser.add_argument('-f', '--format',
                        choices=('parsable', 'standard'), default='standard',
                        help='format for parsing output')
    parser.add_argument('-s', '--strict',
                        action='store_true',
                        help='return non-zero exit code on warnings '
                             'as well as errors')
    parser.add_argument('-v', '--version', action='version',
                        version='%s %s' % (APP_NAME, APP_VERSION))

    # TODO: read from stdin when no filename?

    args = parser.parse_args(argv)

    # User-global config is supposed to be in ~/.config/yamllint/config
    if 'XDG_CONFIG_HOME' in os.environ:
        user_global_config = os.path.join(
            os.environ['XDG_CONFIG_HOME'], 'yamllint', 'config')
    else:
        user_global_config = os.path.expanduser('~/.config/yamllint/config')

    try:
        if args.config_data is not None:
            if args.config_data != '' and ':' not in args.config_data:
                args.config_data = 'extends: ' + args.config_data
            conf = YamlLintConfig(content=args.config_data)
        elif args.config_file is not None:
            conf = YamlLintConfig(file=args.config_file)
        elif os.path.isfile('.yamllint'):
            conf = YamlLintConfig(file='.yamllint')
        elif os.path.isfile(user_global_config):
            conf = YamlLintConfig(file=user_global_config)
        else:
            conf = YamlLintConfig('extends: default')
    except YamlLintConfigError as e:
        print(e, file=sys.stderr)
        sys.exit(-1)

    max_level = 0

    for file in find_files_recursively(args.files):
        filepath = file[2:] if file.startswith('./') else file
        try:
            first = True
            with open(file) as f:
                for problem in linter.run(f, conf, filepath):
                    if args.format == 'parsable':
                        print(Format.parsable(problem, file))
                    elif sys.stdout.isatty():
                        if first:
                            print('\033[4m%s\033[0m' % file)
                            first = False

                        print(Format.standard_color(problem, file))
                    else:
                        if first:
                            print(file)
                            first = False

                        print(Format.standard(problem, file))

                    max_level = max(max_level, PROBLEM_LEVELS[problem.level])

            if not first and args.format != 'parsable':
                print('')
        except EnvironmentError as e:
            print(e, file=sys.stderr)
            sys.exit(-1)

    if max_level == PROBLEM_LEVELS['error']:
        return_code = 1
    elif max_level == PROBLEM_LEVELS['warning']:
        return_code = 2 if args.strict else 0
    else:
        return_code = 0

    sys.exit(return_code)
Example #11
0
def run(argv=None):
    parser = argparse.ArgumentParser(prog=APP_NAME,
                                     description=APP_DESCRIPTION)
    parser.add_argument('files',
                        metavar='FILE_OR_DIR',
                        nargs='+',
                        help='files to check')
    parser.add_argument('-c',
                        '--config-file',
                        dest='config_file',
                        action='store',
                        help='path to a custom configuration')
    parser.add_argument('-d',
                        '--config-data',
                        dest='config_data',
                        action='store',
                        help='custom configuration (as YAML source)')
    parser.add_argument('-f',
                        '--format',
                        choices=('parsable', 'standard'),
                        default='standard',
                        help='format for parsing output')
    parser.add_argument('-v',
                        '--version',
                        action='version',
                        version='%s %s' % (APP_NAME, APP_VERSION))

    # TODO: read from stdin when no filename?

    args = parser.parse_args(argv)

    if args.config_file is not None and args.config_data is not None:
        print(
            'Options --config-file and --config-data cannot be used '
            'simultaneously.',
            file=sys.stderr)
        sys.exit(-1)

    # User-global config is supposed to be in ~/.config/yamllint/config
    user_global_config = None
    if 'XDG_CONFIG_HOME' in os.environ:
        user_global_config = os.path.join(os.environ['XDG_CONFIG_HOME'],
                                          'yamllint', 'config')
    elif 'HOME' in os.environ:
        user_global_config = os.path.join(os.environ['HOME'], '.config',
                                          'yamllint', 'config')

    try:
        if args.config_data is not None:
            if args.config_data != '' and ':' not in args.config_data:
                args.config_data = 'extends: ' + args.config_data
            conf = YamlLintConfig(content=args.config_data)
        elif args.config_file is not None:
            conf = YamlLintConfig(file=args.config_file)
        elif os.path.isfile('.yamllint'):
            conf = YamlLintConfig(file='.yamllint')
        elif user_global_config and os.path.isfile(user_global_config):
            conf = YamlLintConfig(file=user_global_config)
        else:
            conf = YamlLintConfig('extends: default')
    except YamlLintConfigError as e:
        print(e, file=sys.stderr)
        sys.exit(-1)

    return_code = 0

    for file in find_files_recursively(args.files):
        try:
            first = True
            with open(file) as f:
                for problem in linter.run(f, conf):
                    if args.format == 'parsable':
                        print(Format.parsable(problem, file))
                    else:
                        if first:
                            print('\033[4m%s\033[0m' % file)
                            first = False

                        print(Format.standard(problem, file))

                    if return_code == 0 and problem.level == 'error':
                        return_code = 1

            if not first and args.format != 'parsable':
                print('')
        except EnvironmentError as e:
            print(e, file=sys.stderr)
            return_code = -1

    sys.exit(return_code)
Example #12
0
def main():
    """
    Mainline script which does the following.
    - Parses arguments
    - Reads the .travis.yml file assuming YAML
    - Passes the file contents to travis-ci.org linter
    - Displays the results.
    """

    # Parse command line arguments.
    parser = argparser()
    args = parser.parse_args()

    # First lint the file as a generic YAML file.
    try:
        log.setLevel(logging.WARNING)
        if args.verbose:
            log.setLevel(logging.INFO)
        if args.debug:
            log.setLevel(logging.DEBUG)
            log.debug('Debugging enabled')

        log.info('Performing generic YAML linting...')
        with open(args.filename, 'r') as yaml_file:
            log.debug('File %s opened successfully', args.filename)
            results = linter.run(yaml_file, YamlLintConfig('{}'))
            invalid = False
            log.debug('Linter returned results')
            for problem in results:
                msg = '%(fn)s:%(line)s:%(col)s: [%(level)s] %(msg)s' % {
                    'fn': args.filename,
                    'line': problem.line,
                    'col': problem.column,
                    'level': problem.level,
                    'msg': problem.message}

                if problem.level == ERROR:
                    log.debug('Gneric YAML error discovered')
                    invalid = True

                logging.warning(msg)

            if invalid:
                raise GenericLintException(
                    'One or more generic lint errors detected.')

            # It's valid generic YAML so now try as Trvis-CI YAML.
            log.info('Performing Travis-CI YAML lint...')

            # Rewind YAML file back to the start and then pass to the Travis-CI
            # lint webpage.
            yaml_file.seek(0, 0)
            log.info('POSTing to Travis-CI linter...')

            # Pass the YAML to the travis-ci.org linter.
            json_result = requests.post(
                URI_LINTER, data={'content': yaml_file.read()})
            log.debug('POST to Travis-CI linter completed')

            # The results are returned as JSON.
            result = json.loads(json_result.text)

            if not result[LINT]:
                raise TravisCiLintException(
                    'Travis-CI did not return a lint result.')

            for msg_type in result[LINT]:
                for report in result[LINT][msg_type]:
                    message = ''
                    if report[KEY]:
                        message = (
                            'in %s \'section\': ' % '.'.join(report[KEY]))
                    message = message + report[MESSAGE]

                    # print("%s" % str(report[MESSAGE]))
                    msg = '%(fn)s:%(line)s:%(col)s: [%(level)s] %(msg)s' % {
                        'fn': args.filename,
                        'line': 0,
                        'col': 0,
                        'level': msg_type,
                        'msg': message}
                    log.warning(msg)

            if WARNINGS in result[LINT] and result[LINT][WARNINGS]:
                raise TravisCiLintException(
                    'One or more Travis-CI lint errors detected.')

            log.info('File is clean!')

    except (GenericLintException, TravisCiLintException) as exc:
        log.exception(exc)

    except Exception as exc:        # pylint: disable=broad-except
        log.exception('Error opening file: %s: %s', args.filename, str(exc))
import os
from yamllint.config import YamlLintConfig
from yamllint import linter

rootdir = '.'
conf = YamlLintConfig('extends: default')
rep = open('./lint/report.txt', "a+")
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        if '.yaml' in file:
            rel_path = os.path.join(subdir, file)
            f = open(rel_path)
            gen = linter.run(f, conf)
            for lint in list(gen):
                rep.write(
                    f'{rel_path}:{lint.line}:{lint.column}: [{lint.level}] {lint.desc} ({lint.rule})\n'
                )
rep.close()
def validate_template(clients):
    ##################################################################
    #                        Variables setting                       #
    ##################################################################
    # It sets the date to the current day, the log will refer to this date when the script will be executed
    today = (str(datetime.datetime.now())).split(" ")[0]
    # It gets the files of the directory "./TemplateLocalStorage"
    onlyyaml = get_yaml()
    app_dir = "{}/htv".format(home)
    # It defines the name of the directories (for valid, warning, error, log files)
    pathvalid = app_dir + "/ValidYamlFiles"
    pathwarn = app_dir + "/WarnYamlFiles"
    patherr = app_dir + "/ErrYamlFiles"
    pathlog = app_dir + "/Log"
    pathfiles = app_dir + "/TemplateLocalStorage"

    ##################################################################
    #                      Validation process                        #
    ##################################################################
    # For every YAML file in onlyyalm list it will examine the file
    for yamlfile in onlyyaml:
        print("\n>>> Analysing of '{}' <<<".format(yamlfile))
        # It saves the name of the file
        filename = yamlfile.split('.')[0]
        # It uses the file into the open function
        with open("{}/{}".format(pathfiles, yamlfile)) as F:
            # It reads the data and saves it into the variable 'data'
            data = F.read()
            print(">> Trying the validity")
            try:
                # It checks if the file is a valid yaml file
                doc = yaml.safe_load(data)
                printout("     Valid Heat template for yaml module\n", GREEN)
                conf = YamlLintConfig('extends: default')
                # It saves warnings if there are any
                gen = linter.run(data, conf)
                warnings = list(gen)
                # If there are warnings they will be saved into the Log file with the file name and the timestamp
                if warnings:
                    with open(
                            "{0}/{1}-{2}-warning.log".format(
                                pathlog, filename, today), 'a+') as output:
                        printout("     This file has one or more warnings\n",
                                 YELLOW)
                        for warning in warnings:
                            output.write("{}\n".format(str(warning)))
                        # Then the file will be moved into a directory containing all the files with warnings
                        os.rename("{}/{}".format(pathfiles, yamlfile),
                                  "{}/{}".format(pathwarn, yamlfile))
                else:
                    # Otherwise the file will be moved into a directory containing all the valid YAML files
                    os.rename("{}/{}".format(pathfiles, yamlfile),
                              "{}/{}".format(pathvalid, yamlfile))
                    printout("     No error for yaml lint too\n", GREEN)
                """Files are valid (even if with some warnings), so it will save items (such as the image, flavor etc) 
                in a specific list and then try to check for their existence into the openstack server"""
                print(
                    ">> Analyzing the template structure and items existence:")
                #################################################
                #    Search for items into openstack server     #
                #################################################

                check_openstack(doc, yamlfile, clients)

            # if the file has errors it is saved in ./ErrorsYamlFiles and a log is created
            except:
                # If there is an exception it will be saved into the Log file
                with open(
                        "{0}/{1}-{2}-error.log".format(pathlog, filename,
                                                       today), 'a+') as output:
                    output.write("{}\n".format(str(traceback.format_exc())))
                printout("     Invalid Heat template for yaml module\n", RED)
                os.rename("{}/{}".format(pathfiles, yamlfile),
                          "{}/{}".format(patherr, yamlfile))
    print("\n>> All files have been analyzed")
Example #15
0
 def test_run_on_unicode(self):
     linter.run(u'test: document', self.fake_config())
Example #16
0
def merge_files(context):
    """
  Given a context containing path to template, env, and service:
  merge config into template and output the result to stdout
  Args:
    context: a populated context object
  """
    resolver = EFTemplateResolver(profile=context.profile,
                                  region=context.region,
                                  env=context.env,
                                  service=context.service)

    try:
        with open(context.template_path, 'r') as f:
            template_body = f.read()
            f.close()
    except IOError as error:
        raise IOError("Error loading template file: {} {}".format(
            context.template_path, repr(error)))

    if context.no_params is False:
        try:
            with open(context.param_path, 'r') as f:
                param_body = f.read()
                f.close()
        except IOError as error:
            raise IOError("Error loading param file: {} {}".format(
                context.param_path, repr(error)))

        dest = yaml.safe_load(param_body)["dest"]

        # if 'dest' for the current object contains an 'environments' list, check it
        if "environments" in dest:
            if not resolver.resolved["ENV_SHORT"] in dest["environments"]:
                print("Environment: {} not enabled for {}".format(
                    resolver.resolved["ENV_SHORT"], context.template_path))
                return

        # Process the template_body - apply context + parameters
        resolver.load(template_body, param_body)
    else:
        resolver.load(template_body)
    rendered_body = resolver.render()

    if not resolver.resolved_ok():
        raise RuntimeError(
            "Couldn't resolve all symbols; template has leftover {{ or }}: {}".
            format(resolver.unresolved_symbols()))

    if context.lint:
        if context.template_path.endswith(".json"):
            try:
                json.loads(rendered_body, strict=False)
                print("JSON passed linting process.")
            except ValueError as e:
                fail("JSON failed linting process.", e)
        elif context.template_path.endswith((".yml", ".yaml")):
            conf = yamllint_config.YamlLintConfig(content='extends: relaxed')
            lint_output = yamllinter.run(rendered_body, conf)
            lint_level = 'error'
            lint_errors = [
                issue for issue in lint_output if issue.level == lint_level
            ]
            if lint_errors:
                split_body = rendered_body.splitlines()
                for error in lint_errors:
                    print(error)
                    # printing line - 1 because lists start at 0, but files at 1
                    print("\t", split_body[error.line - 1])
                fail("YAML failed linting process.")

    if context.verbose:
        print(context)
        if context.no_params:
            print('no_params flag set to true!')
            print(
                'Inline template resolution based on external symbol lookup only and no destination for file write.\n'
            )
        else:
            dir_path = normpath(dirname(dest["path"]))
            print("make directories: {} {}".format(dir_path, dest["dir_perm"]))
            print("chmod file to: " + dest["file_perm"])
            user, group = dest["user_group"].split(":")
            print("chown last directory in path to user: {}, group: {}".format(
                user, group))
            print("chown file to user: {}, group: {}\n".format(user, group))

        print("template body:\n{}\nrendered body:\n{}\n".format(
            template_body, rendered_body))
    elif context.silent:
        print("Config template rendered successfully.")
    else:
        print(rendered_body)
Example #17
0
"""
Script for checking lint checking
"""
from yamllint.config import YamlLintConfig
from yamllint import linter
import sys

requested_file = str(sys.argv[1])

# conf object must be created based on .yamllint rules
conf = YamlLintConfig(file='.yamllint')
f = open(requested_file)
gen = linter.run(f, conf, '.yamllint')
errors = list(gen)
if errors:
    print(errors)
else:
    print("Success")
Example #18
0
def run(argv=None):
    parser = argparse.ArgumentParser(prog=APP_NAME,
                                     description=APP_DESCRIPTION)
    parser.add_argument('files',
                        metavar='FILE_OR_DIR',
                        nargs='+',
                        help='files to check')
    parser.add_argument('-c',
                        '--config',
                        dest='config_file',
                        action='store',
                        help='path to a custom configuration')
    parser.add_argument('-f',
                        '--format',
                        choices=('parsable', 'standard'),
                        default='standard',
                        help='format for parsing output')
    parser.add_argument('-v',
                        '--version',
                        action='version',
                        version='%s %s' % (APP_NAME, APP_VERSION))

    # TODO: read from stdin when no filename?

    args = parser.parse_args(argv)

    try:
        if args.config_file is not None:
            conf = YamlLintConfig(file=args.config_file)
        elif os.path.isfile('.yamllint'):
            conf = YamlLintConfig(file='.yamllint')
        else:
            conf = YamlLintConfig('extends: default')
    except YamlLintConfigError as e:
        print(e, file=sys.stderr)
        sys.exit(-1)

    return_code = 0

    for file in find_files_recursively(args.files):
        try:
            first = True
            with open(file) as f:
                for problem in linter.run(f, conf):
                    if args.format == 'parsable':
                        print(Format.parsable(problem, file))
                    else:
                        if first:
                            print('\033[4m%s\033[0m' % file)
                            first = False

                        print(Format.standard(problem, file))

                    if return_code == 0 and problem.level == 'error':
                        return_code = 1

            if not first and args.format != 'parsable':
                print('')
        except EnvironmentError as e:
            print(e)
            return_code = -1

    sys.exit(return_code)
Example #19
0
 def test_run_on_stream(self):
     linter.run(io.StringIO(u'hello'), self.fake_config())
Example #20
0
 def test_run_on_unicode(self):
     linter.run(u'test: document', self.fake_config())
Example #21
0
 def test_run_on_bytes(self):
     linter.run(b'test: document', self.fake_config())
Example #22
0
 def test_run_on_string(self):
     linter.run('test: document', self.fake_config())
Example #23
0
 def test_run_on_string(self):
     linter.run('test: document', self.fake_config())
Example #24
0
 def test_run_on_bytes(self):
     linter.run(b'test: document', self.fake_config())
Example #25
0
 def check_yaml(self, conf, path, contents):  # type: (YamlLintConfig, str, str) -> None
     """Check the given YAML."""
     self.check_parsable(path, contents)
     self.messages += [self.result_to_message(r, path) for r in linter.run(contents, conf, path)]
Example #26
0
 def test_run_on_stream(self):
     linter.run(io.StringIO(u'hello'), self.fake_config())
Example #27
0
def run(argv=None):
    parser = argparse.ArgumentParser(prog=APP_NAME,
                                     description=APP_DESCRIPTION)
    files_group = parser.add_mutually_exclusive_group(required=True)
    files_group.add_argument('files',
                             metavar='FILE_OR_DIR',
                             nargs='*',
                             default=(),
                             help='files to check')
    files_group.add_argument('-',
                             action='store_true',
                             dest='stdin',
                             help='read from standard input')
    config_group = parser.add_mutually_exclusive_group()
    config_group.add_argument('-c',
                              '--config-file',
                              dest='config_file',
                              action='store',
                              help='path to a custom configuration')
    config_group.add_argument('-d',
                              '--config-data',
                              dest='config_data',
                              action='store',
                              help='custom configuration (as YAML source)')
    parser.add_argument('-f',
                        '--format',
                        choices=('parsable', 'standard', 'colored', 'auto'),
                        default='auto',
                        help='format for parsing output')
    parser.add_argument('-s',
                        '--strict',
                        action='store_true',
                        help='return non-zero exit code on warnings '
                        'as well as errors')
    parser.add_argument('-v',
                        '--version',
                        action='version',
                        version='{} {}'.format(APP_NAME, APP_VERSION))

    args = parser.parse_args(argv)

    # User-global config is supposed to be in ~/.config/yamllint/config
    if 'XDG_CONFIG_HOME' in os.environ:
        user_global_config = os.path.join(os.environ['XDG_CONFIG_HOME'],
                                          'yamllint', 'config')
    else:
        user_global_config = os.path.expanduser('~/.config/yamllint/config')

    try:
        if args.config_data is not None:
            if args.config_data != '' and ':' not in args.config_data:
                args.config_data = 'extends: ' + args.config_data
            conf = YamlLintConfig(content=args.config_data)
        elif args.config_file is not None:
            conf = YamlLintConfig(file=args.config_file)
        elif os.path.isfile('.yamllint'):
            conf = YamlLintConfig(file='.yamllint')
        elif os.path.isfile(user_global_config):
            conf = YamlLintConfig(file=user_global_config)
        else:
            conf = YamlLintConfig('extends: default')
    except YamlLintConfigError as e:
        print(e, file=sys.stderr)
        sys.exit(-1)

    max_level = 0

    for file in find_files_recursively(args.files):
        filepath = file[2:] if file.startswith('./') else file
        try:
            with open(file) as f:
                problems = linter.run(f, conf, filepath)
        except EnvironmentError as e:
            print(e, file=sys.stderr)
            sys.exit(-1)
        prob_level = show_problems(problems, file, args_format=args.format)
        max_level = max(max_level, prob_level)

    # read yaml from stdin
    if args.stdin:
        try:
            problems = linter.run(sys.stdin, conf, '')
        except EnvironmentError as e:
            print(e, file=sys.stderr)
            sys.exit(-1)
        prob_level = show_problems(problems, 'stdin', args_format=args.format)
        max_level = max(max_level, prob_level)

    if max_level == PROBLEM_LEVELS['error']:
        return_code = 1
    elif max_level == PROBLEM_LEVELS['warning']:
        return_code = 2 if args.strict else 0
    else:
        return_code = 0

    sys.exit(return_code)
Example #28
0
import jsonschema
import sys
import yaml
from yamllint import linter as yaml_linter
from yamllint.config import YamlLintConfig

# Get arguments.
schema_filename = sys.argv[1]
yaml_filename = sys.argv[2]

# Parse inputs.
with open(schema_filename, 'r') as schema_file, open(yaml_filename,
                                                     'r') as yaml_file:
    # Lint YAML source.
    yamllint_conf = YamlLintConfig("extends: default")
    errors = list(yaml_linter.run(input=yaml_file, conf=yamllint_conf))
    if errors:
        print(errors)
        print("yamllint failed for:", yaml_filename)
        sys.exit(1)
    yaml_file.seek(0)

    # Parse configuration.
    instance = yaml.safe_load(yaml_file)

    # Parse schema.
    schema = json.load(schema_file)

    # Validate YAML source against schema.
    jsonschema.validate(instance=instance, schema=schema)
Example #29
0
def linter_errors(text: str) -> List[linter.LintProblem]:
    problems = linter.run(text, YAMLLINT_CONFIG)
    return [p for p in problems if p.level == "error"]