Ejemplo n.º 1
0
def update_documentation(rules):
    """Generate documentation"""

    # Update the overview of all rules in the linter
    filename = 'docs/rules.md'

    # Sort rules by the Rule ID
    sorted_rules = sorted(rules, key=lambda obj: obj.id)

    data = []

    # Read current file up to the Rules part, everything up to that point is
    # static documentation.
    with open(filename, 'r') as origial_file:

        line = origial_file.readline()
        while line:
            data.append(line)

            if line == '## Rules\n':
                break

            line = origial_file.readline()

    # Rebuild the file content
    with open(filename, 'w') as new_file:

        # Rewrite the static documentation
        for line in data:
            new_file.write(line)

        # Add the rules
        new_file.write('The following **{}** rules are applied by this linter:\n\n'.format(len(sorted_rules)))
        new_file.write('| Rule ID  | Title | Description | Source | Tags |\n')
        new_file.write('| -------- | ----- | ----------- | ------ | ---- |\n')

        rule_output = '| {0} <a name="{0}"></a> | {1} | {2} | [Source]({3}) | {4} |\n'

        # Add system Errors (hardcoded)
        parseerror = cfnlint.ParseError()
        tags = ','.join('`{0}`'.format(tag) for tag in parseerror.tags)
        new_file.write(rule_output.format(
            parseerror.id, parseerror.shortdesc, parseerror.description, '', tags))

        transformerror = cfnlint.TransformError()
        tags = ','.join('`{0}`'.format(tag) for tag in transformerror.tags)
        new_file.write(rule_output.format(
            transformerror.id, transformerror.shortdesc, transformerror.description, '', tags))

        ruleerror = cfnlint.RuleError()
        tags = ','.join('`{0}`'.format(tag) for tag in ruleerror.tags)
        new_file.write(
            rule_output.format(ruleerror.id, ruleerror.shortdesc, ruleerror.description, '', tags))

        for rule in sorted_rules:
            tags = ','.join('`{0}`'.format(tag) for tag in rule.tags)
            new_file.write(rule_output.format(rule.id, rule.shortdesc, rule.description, rule.source_url, tags))
Ejemplo n.º 2
0
    def __init__(self, filename, message, line_number, column_number, key=' '):

        # Call the base class constructor with the parameters it needs
        super(CfnParseError, self).__init__(message)

        # Now for your custom code...
        self.filename = filename
        self.line_number = line_number
        self.column_number = column_number
        self.message = message
        self.match = cfnlint.Match(
            line_number + 1, column_number + 1, line_number + 1,
            column_number + 1 + len(key), filename, cfnlint.ParseError(), message=message)
Ejemplo n.º 3
0
 def __init__(self, msg, doc, pos, key=' '):
     lineno = doc.count('\n', 0, pos) + 1
     colno = pos - doc.rfind('\n', 0, pos)
     errmsg = '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos)
     ValueError.__init__(self, errmsg)
     self.msg = msg
     self.doc = doc
     self.pos = pos
     self.lineno = lineno
     self.colno = colno
     self.match = cfnlint.Match(
         lineno, colno + 1, lineno,
         colno + 1 + len(key), '', cfnlint.ParseError(), message=msg)
Ejemplo n.º 4
0
def update_documentation(rules):
    """Generate documentation"""

    # Update the overview of all rules in the linter
    filename = 'docs/rules.md'

    # Sort rules by the Rule ID
    sorted_rules = sorted(rules, key=lambda obj: obj.id)

    data = []

    # Read current file up to the Rules part, everything up to that point is
    # static documentation.
    with open(filename, 'r') as origial_file:

        line = origial_file.readline()
        while line:
            data.append(line)

            if line == '## Rules\n':
                break

            line = origial_file.readline()

    # Rebuild the file content
    with open(filename, 'w') as new_file:

        # Rewrite the static documentation
        for line in data:
            new_file.write(line)

        # Add the rules
        new_file.write(
            'The following **{}** rules are applied by this linter:\n'.format(
                len(sorted_rules)))
        new_file.write(
            '(_This documentation is generated from the Rules, do not alter this manually_)\n\n'
        )
        new_file.write(
            '| Rule ID  | Title | Description | Config<br />(Name:Type:Default) | Source | Tags |\n'
        )
        new_file.write(
            '| -------- | ----- | ----------- | ---------- | ------ | ---- |\n'
        )

        rule_output = '| {0}<a name="{0}"></a> | {1} | {2} | {3} | [Source]({4}) | {5} |\n'

        # Add system Errors (hardcoded)
        parseerror = cfnlint.ParseError()
        tags = ','.join('`{0}`'.format(tag) for tag in parseerror.tags)
        new_file.write(
            rule_output.format(parseerror.id, parseerror.shortdesc,
                               parseerror.description, '', '', tags))

        transformerror = cfnlint.TransformError()
        tags = ','.join('`{0}`'.format(tag) for tag in transformerror.tags)
        new_file.write(
            rule_output.format(transformerror.id, transformerror.shortdesc,
                               transformerror.description, '', '', tags))

        ruleerror = cfnlint.RuleError()
        tags = ','.join('`{0}`'.format(tag) for tag in ruleerror.tags)
        new_file.write(
            rule_output.format(ruleerror.id, ruleerror.shortdesc,
                               ruleerror.description, '', '', tags))

        # Seprate the experimental rules
        experimental_rules = []

        for rule in sorted_rules:

            if rule.experimental:
                experimental_rules.append(rule)
                continue

            tags = ','.join('`{0}`'.format(tag) for tag in rule.tags)
            config = '<br />'.join(
                '{0}:{1}:{2}'.format(key, values.get('type'),
                                     values.get('default'))
                for key, values in rule.config_definition.items())
            new_file.write(
                rule_output.format(rule.id, rule.shortdesc, rule.description,
                                   config, rule.source_url, tags))

        # Output the experimental rules (if any)
        if experimental_rules:
            new_file.write('### Experimental rules\n')
            new_file.write(
                '| Rule ID  | Title | Description | Source | Tags |\n')
            new_file.write(
                '| -------- | ----- | ----------- | ------ | ---- |\n')

            for rule in experimental_rules:
                tags = ','.join('`{0}`'.format(tag) for tag in rule.tags)
                config = '<br />'.join(
                    '{0}:{1}:{2}'.format(key, values.get('type'),
                                         values.get('default'))
                    for key, values in rule.config_definition.items())
                new_file.write(
                    rule_output.format(rule.id, rule.shortdesc,
                                       rule.description, config,
                                       rule.source_url, tags))