Cfn-python-lint: [feature request] version with callable API

Created on 24 Jun 2018  ยท  12Comments  ยท  Source: aws-cloudformation/cfn-python-lint

As linting should be a part of a CI/CD process, it would be very cool to have an API to use the linter directly from other python based app/scripts.

As a user of the linter, I don't want to call shell commands from my python scripts to call another python based application (the linter).

enhancement

Most helpful comment

Hi @fatbasstard, this was an error with python 3.6 and 0.3.1.
After upgrading to the new version , it works.

thanks fro fixing ;)

All 12 comments

Hi, This is already completely doable, we've got it integrated in our own (Python) Toolkit. What feature(s) are you missing ATM to achieve this? Or do you need help on getting this done?

I could add documentation about how to do this if it helps

Hi @fatbasstard,
thanks for the response and link to the related issue.
As I understand, you encourage me to simply import the module to my python code and call the needed method directly?
I am asking, because I expected this "hint" in the README. That's why I did not even took a look into the code. I will do that ...

Hi @h1f1x,

Yup, just import the module and call the cfnlint.core.run_checks() method with the right parameters... ๐ŸŽ‰

If you need help, let me know. I'll create some documentation to guide the "non-CLI" process.

Hi @h1f1x ,

Just added some documentation and a basic code example on using cfn-lint in your own code: https://github.com/awslabs/cfn-python-lint/blob/master/docs/integration.md

Thanks @fatbasstard. Unfortunately the code doesn't work :(

Two errors, then I stopped ...

    template = cfnlint.cfn_yaml.load(filename)
AttributeError: module 'cfnlint' has no attribute 'cfn_yaml'
    rules = cfnlint.core.get_rules([], [])
AttributeError: module 'cfnlint.core' has no attribute 'get_rules'

I am running Python 3.6.3.

Which version of cfn-lint did you include in your project? Did you include the module in your project?

Here are some facts:

โžœ cat t.py
import cfnlint.core

# The path to the file to check
filename = 'pipeline.yaml'

# Load the YAML file
template = cfnlint.cfn_yaml.load(filename)

# Initialize the ruleset to be applied (no overrules, no excludes)
rules = cfnlint.core.get_rules([], [])

# Use us-east-1 region (spec file) for validation
regions = ['eu-west-1']

# Process all the rules and gather the errors
matches = cfnlint.core.run_checks(
    filename,
    template,
    rules,
    regions)

# Print the output
print(matches)

โžœ pip list | grep cfnlint
cfnlint            0.0.9

โžœ python t.py
Traceback (most recent call last):
  File "t.py", line 7, in <module>
    template = cfnlint.cfn_yaml.load(filename)
AttributeError: module 'cfnlint' has no attribute 'cfn_yaml'

So what I am missing?

I got it, you're using the wrong cfn-lint ๐Ÿ˜‰

You're using cfnlint, https://pypi.org/project/cfnlint/ (which is actually my personal CloudFormation linter I started working on before jumping on the cfn-lint wagon)

You need cfn-lint https://pypi.org/project/cfn-lint/

I'll see if I can remove/redirect the old unmaintained one from pypi.

Can you check if you're including the right one in you project?

Oh. I corrected that. Very strange. I had both installed.

Now I got a new error:

โžœ python t.py
Traceback (most recent call last):
  File "t.py", line 1, in <module>
    import cfnlint.core
  File "/Users/xxx/.venv3/lib/python3.6/site-packages/cfnlint/core.py", line 28, in <module>
    from cfnlint import RulesCollection, Match
ImportError: cannot import name 'RulesCollection'

โžœ pip list | grep cfn
cfn-lint           0.3.1
cfn-sphere         1.0.2

Hmmm.. created the file above and run the same commands:

โžœ  python t.py        
[]
โžœ  pip list | grep cfn
cfn-lint                      0.3.2       /usr/local/lib/python2.7/site-packages

Hi @fatbasstard, this was an error with python 3.6 and 0.3.1.
After upgrading to the new version , it works.

thanks fro fixing ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlessandroLorenzi picture AlessandroLorenzi  ยท  3Comments

LukasMusebrink picture LukasMusebrink  ยท  3Comments

Dunedan picture Dunedan  ยท  3Comments

ow-krioles picture ow-krioles  ยท  5Comments

Sergei-Rudenkov picture Sergei-Rudenkov  ยท  4Comments