Skip to content
Snippets Groups Projects
Commit e69ab0e9 authored by Hugo Hörnquist's avatar Hugo Hörnquist
Browse files

Forward puppet strings messages to logger.

parent c4aa3661
No related branches found
No related tags found
No related merge requests found
...@@ -34,6 +34,7 @@ from typing import ( ...@@ -34,6 +34,7 @@ from typing import (
from dataclasses import dataclass, field from dataclasses import dataclass, field
import logging import logging
from .internal import Deserializable from .internal import Deserializable
import re
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -370,14 +371,42 @@ def puppet_strings(path: str) -> bytes: ...@@ -370,14 +371,42 @@ def puppet_strings(path: str) -> bytes:
>>> PuppetStrings.from_json(puppet_strings("/etc/puppetlabs/code/modules/stdlib")) >>> PuppetStrings.from_json(puppet_strings("/etc/puppetlabs/code/modules/stdlib"))
""" """
# TODO adding an --out flag (to not stdout) causes warnings to be # All this extra weird stuff with tempfiles and pipes since puppet
# printed to stdout. Warnings # strings output errors on stdout, and only if the --out flag
# isn't used.
import tempfile
cmd = subprocess.run('puppet strings generate --format json'.split(' '), tmpfile = tempfile.NamedTemporaryFile()
cmd = subprocess.Popen(
['puppet', 'strings', 'generate',
'--format', 'json',
'--out', tmpfile.name],
cwd=path, cwd=path,
check=True, stdout=subprocess.PIPE,
stdout=subprocess.PIPE) text=True,
return cmd.stdout )
if not cmd.stdout:
# TODO better error?
raise Exception(cmd.returncode)
for line in cmd.stdout:
line = line.strip()
# These debug levels are by best effort, and based on the
# enum found here:
# https://github.com/puppetlabs/puppet-strings/blob/afe75151f8b47ce33433c488e22ca508aa48ac7c/spec/unit/puppet-strings/yard/handlers/ruby/rsapi_handler_spec.rb#L105
# Expected formatting from observing the output.
if m := re.match(r'^\[(\w+)]: (.*)', line):
match m[1]:
case "debug": logger.debug(m[2])
case "warn": logger.warning(m[2])
case "error": logger.error(m[2])
case _: logger.warning(line)
else:
logger.info(line)
return tmpfile.read()
class HasDocstring(Protocol): class HasDocstring(Protocol):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment