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 8 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.
Jump to
Jump to file
Failed to load files.
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"]
yaml = ["pyyaml"]

[tool.poetry.group.lint.dependencies]
pylama = { extras = [
Expand Down
55 changes: 49 additions & 6 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), default_flow_style=False)


# =============================================================================
Expand Down Expand Up @@ -676,12 +689,22 @@ def cli_parser() -> argparse.ArgumentParser:
default=False,
help="Uses a persistent cache when querying pypi.",
)
parser.add_argument(
"--yaml",
action="store_true",
help="Write YAML instead of the default JSON. Needs the 'pyyaml' package.",
)

parser.add_argument(
"--outfile",
"-o",
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,13 +729,29 @@ 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
if hasattr(options.outfile, "write"):
output_stream = options.outfile
if pathlib.Path(output_stream.name).suffix.casefold() in (".yaml", ".yml"):
options.yaml = True
else:
output_stream = sys.stdout

if options.yaml:
try:
# optional dependency, not imported at top
import yaml
except ImportError:
parser.error(
"Outputing YAML requires 'pyyaml' package: try 'pip install pyyaml'"
)

# print platform info if requested, and exit
if options.platform_info:
json.dump(
asdict(PlatformFactory.from_current_interpreter()), output_stream, indent=4
)
info = asdict(PlatformFactory.from_current_interpreter())
if options.yaml:
yaml.dump(info, output_stream, default_flow_style=False)
else:
json.dump(info, output_stream, indent=4)
parser.exit()

# print installed packages if requested, and exit
Expand Down Expand Up @@ -771,6 +810,10 @@ def main():
# generate flatpak-builder build module
build_module = FlatpakGenerator.build_module(requirements, downloads)

if options.yaml:
yaml.dump(build_module, output_stream, default_flow_style=False)
cbm755 marked this conversation as resolved.
Show resolved Hide resolved
parser.exit()

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

Expand Down
11 changes: 11 additions & 0 deletions tests/test_req2flatpak.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from typing import List
from unittest.mock import patch

import yaml

from req2flatpak import (
DownloadChooser,
FlatpakGenerator,
Expand Down Expand Up @@ -95,6 +97,15 @@ def test_cli_with_reqs_as_args(self):
build_module = json.loads(result.stdout)
self.validate_build_module(build_module)

def test_cli_with_reqs_as_args_yaml(self):
"""Runs req2flatpak in yaml mode by passing requirements as cmdline arg."""
args = ["--requirements"] + self.requirements
args += ["--target-platforms"] + self.target_platforms
args += ["--yaml"]
result = self._run_r2f(args)
build_module = yaml.load(result.stdout, yaml.Loader)
cbm755 marked this conversation as resolved.
Show resolved Hide resolved
self.validate_build_module(build_module)

def test_cli_with_reqs_as_file(self):
"""Runs req2flatpak by passing requirements as requirements.txt file."""
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8") as req_file:
Expand Down