yamllint: Ignore exisiting errors


When adding yamllint to an existing project it can be hard to fix all the errors at once.

I wrote a simple script to create a rules block that simply ignores all rules that currently trigger for a file. This works by generating a rules block ignoring paths.

import subprocess
import yaml

YAMLLINT_FILES = "."

rules = {}

cmd = subprocess.run(["yamllint", YAMLLINT_FILES, "-f", "parsable"], capture_output=True, text=True)

for line in cmd.stdout.splitlines():
    # Extract rule
    rule_start = line.find("(") + 1
    rule_end = line.find(")")
    rule_name = line[rule_start:rule_end]

    # Extract file path
    file_path_start = line.find("Line is: ") + 1
    file_path_end = line.find("yaml") + 4
    file_path = line[file_path_start:file_path_end]

    # Construct output
    rule_content = rules.get(rule_name, {"ignore": list()})
    rule_content["ignore"].append(file_path)
    rules[rule_name] = rule_content

print(yaml.dump(rules))

See also