Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_run_on_string(self):
linter.run('test: document', self.fake_config())
def test_run_on_stream(self):
linter.run(io.StringIO(u'hello'), self.fake_config())
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 supports_color():
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])
else:
conf = YamlLintConfig('extends: default')
except YamlLintConfigError as e:
print(e, file=sys.stderr)
sys.exit(-1)
if conf.locale is not None:
locale.setlocale(locale.LC_ALL, conf.locale)
max_level = 0
for file in find_files_recursively(args.files, conf):
filepath = file[2:] if file.startswith('./') else file
try:
with io.open(file, newline='') 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,
no_warn=args.no_warnings)
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,
no_warn=args.no_warnings)
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('yamllint issues found')
raise SystemExit(1)
:param input_filepaths: List of input files to lint.
:param file: The stream to write errors to.
:returns: List of nits.
"""
# Generic type since the actual type comes from yamllint, which we don't
# control.
nits: List = []
for path in input_filepaths:
# yamllint needs both the file content and the path.
file_content = None
with path.open("r", encoding="utf-8") as fd:
file_content = fd.read()
problems = linter.run(file_content, YamlLintConfig("extends: default"), path)
nits.extend((path, p) for p in problems)
if len(nits):
print("Sorry, Glean found some glinter nits:", file=file)
for (path, p) in nits:
print(f"{path} ({p.line}:{p.column}) - {p.message}")
print("", file=file)
print("Please fix the above nits to continue.", file=file)
return [x[1] for x in nits]
def make_issue(self, problem, filename):
if isinstance(problem, linter.LintProblem):
return YamlLintIssue(
problem.rule or 'syntax',
problem.desc,
filename,
problem.line,
problem.column,
)
if isinstance(problem, EnvironmentError):
return AccessIssue(problem, filename)
return UnknownIssue(problem, filename)
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)