Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add yaml output #41

Merged
merged 20 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ packaging = { version = "^21.3", optional = true }

[tool.poetry.extras]
packaging = ["packaging"]
# TODO: something about pyyaml here
cbm755 marked this conversation as resolved.
Show resolved Hide resolved

[tool.poetry.group.lint.dependencies]
pylama = { extras = [
Expand Down
40 changes: 37 additions & 3 deletions req2flatpak.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,12 +634,25 @@ def sources(downloads: Iterable[Download]) -> List[dict]:
@classmethod
def build_module_as_str(cls, *args, **kwargs) -> str:
"""
Generates a build module for inclusion in a flatpak-builder build manifest.
Generate JSON build module for inclusion in a flatpak-builder build manifest.

The args and kwargs are the same as in
:py:meth:`~req2flatpak.FlatpakGenerator.build_module`
"""
return json.dumps(cls.build_module(*args, **kwargs), indent=2)
return json.dumps(cls.build_module(*args, **kwargs), indent=4)

@classmethod
def build_module_as_yaml_str(cls, *args, **kwargs) -> str:
"""
Generate YAML build module for inclusion in a flatpak-builder build manifest.

The args and kwargs are the same as in
:py:meth:`~req2flatpak.FlatpakGenerator.build_module`
"""
# optional dependency, not imported at top
import yaml

return yaml.dump(cls.build_module(*args, **kwargs), indent=2)
cbm755 marked this conversation as resolved.
Show resolved Hide resolved


# =============================================================================
Expand Down Expand Up @@ -682,6 +695,10 @@ def cli_parser() -> argparse.ArgumentParser:
nargs="?",
type=argparse.FileType("w"),
default=sys.stdout,
help="""
By default, writes JSON but specify a '.yaml' extension and YAML
will be written instead, provided you have the 'pyyaml' package.
""",
cbm755 marked this conversation as resolved.
Show resolved Hide resolved
)
parser.add_argument(
"--platform-info",
Expand All @@ -706,7 +723,13 @@ def main():
options = parser.parse_args()

# stream output to a file or to stdout
output_stream = options.outfile if hasattr(options.outfile, "write") else sys.stdout
want_yaml = False
if hasattr(options.outfile, "write"):
output_stream = options.outfile
if pathlib.Path(output_stream.name).suffix.casefold() in (".yaml", ".yml"):
want_yaml = True
else:
output_stream = sys.stdout

# print platform info if requested, and exit
if options.platform_info:
Expand Down Expand Up @@ -771,6 +794,17 @@ def main():
# generate flatpak-builder build module
build_module = FlatpakGenerator.build_module(requirements, downloads)

if want_yaml:
try:
# optional dependency, not imported at top
import yaml
except ImportError:
parser.error(
"Writing yaml files requires pyyaml package: try 'pip install pyyaml'"
)
yaml.dump(build_module, output_stream, indent=2)
parser.exit()

# write output
json.dump(build_module, output_stream, indent=4)
cbm755 marked this conversation as resolved.
Show resolved Hide resolved

Expand Down