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 15 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
35 changes: 34 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,41 @@ jobs:
- name: Run linters
run: make lint

generate_test_matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0

- name: setup python
uses: actions/setup-python@d27e3f3d7c64b4bbf8e4abfb9b63b83e846e0435 # v4.5.0
with:
python-version: "3.11"

- name: Extract extras from `pyproject.toml`
id: set-matrix
shell: python
run: |
import tomllib
import os
import json
with open('pyproject.toml', 'rb') as f:
manifest = tomllib.load(f)
yaml = { 'include' : [{ 'extras' : extra} for extra in [''] + list(manifest['tool']['poetry']['extras'])]}
out = json.dumps(yaml)
print(out)
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
f.write('matrix=' + out)

test:
name: test ${{ matrix.extras && 'with' || '' }} ${{ matrix.extras }}
runs-on: ubuntu-latest
needs: generate_test_matrix
strategy:
matrix: ${{ fromJson(needs.generate_test_matrix.outputs.matrix) }}
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0
Expand All @@ -33,7 +66,7 @@ jobs:
id: setup
uses: ./.github/actions/setup
with:
install-options: --without lint
install-options: --without lint ${{ matrix.extras && format('--extras "{0}"', matrix.extras) || '' }}

- name: Run Tests
run: make test
Expand Down
17 changes: 15 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 20 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
[build-system]
requires = ["poetry-core"]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "req2flatpak"
version = "0.1.0"
name = "req2flatpak"
version = "0.1.0"
description = "Generates a flatpak-builder build module for installing python packages defined in requirements.txt files."
authors = ["johannesjh <[email protected]>"]
license = "MIT"
readme = "README.rst"
authors = ["johannesjh <[email protected]>"]
license = "MIT"
readme = "README.rst"

[tool.poetry.scripts]
req2flatpak = 'req2flatpak:main'

[tool.poetry.dependencies]
python = "^3.7.2"
python = "^3.7.2"
packaging = { version = "^21.3", optional = true }
pyyaml = { version = "^6.0", optional = true }

[tool.poetry.extras]
packaging = ["packaging"]
yaml = ["pyyaml"]

[tool.poetry.group.lint.dependencies]
pylama = { extras = [
Expand All @@ -28,7 +30,10 @@ pylama = { extras = [
"toml",
], version = "^8.4.1" }
bandit = { extras = ["toml"], version = "^1.7.4" }
types-setuptools = "^65.5.0.2" # type stubs for mypy linting

# type stubs for mypy linting
types-setuptools = "^65.5.0.2"
types-pyyaml = "^6.0.12.8"

# [tool.poetry.group.tests.dependencies]
pydocstyle = "<6.2"
Expand All @@ -37,18 +42,18 @@ pydocstyle = "<6.2"
optional = true

[tool.poetry.group.docs.dependencies]
sphinx = "^5.3.0"
sphinx-argparse = "^0.3.2"
sphinx = "^5.3.0"
sphinx-argparse = "^0.3.2"
sphinx-rtd-theme-github-versions = "^1.1"

[tool.isort]
profile = "black"
profile = "black"
skip_gitignore = true

[tool.pylama]
max_line_length = 88
concurrent = true
linters = "pycodestyle,pydocstyle,pyflakes,pylint,eradicate,mypy"
concurrent = true
linters = "pycodestyle,pydocstyle,pyflakes,pylint,eradicate,mypy"

[tool.pylama.linter.pycodestyle]
ignore = "W503,E203,E501"
Expand All @@ -63,8 +68,8 @@ ignore = ["D202", "D203", "D205", "D401", "D212"]

[tool.pylint]
format.max-line-length = 88
main.disable = ["C0301"] # ignore line-too-long
basic.good-names = ["e", "f", "py"]
main.disable = ["C0301"] # ignore line-too-long
basic.good-names = ["e", "f", "py"]

[tool.bandit]
exclude_dirs = ['tests']
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
30 changes: 30 additions & 0 deletions tests/test_req2flatpak.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
from itertools import product
from pathlib import Path
from typing import List
from unittest import skipUnless
from unittest.mock import patch

try:
import yaml
except ImportError:
yaml = None # type: ignore
cbm755 marked this conversation as resolved.
Show resolved Hide resolved

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

@skipUnless(yaml, "The yaml extra dependency is needed for this feature.")
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.safe_load(result.stdout)
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 All @@ -107,6 +123,20 @@ def test_cli_with_reqs_as_file(self):
build_module = json.loads(result.stdout)
self.validate_build_module(build_module)

@skipUnless(yaml, "The yaml extra dependency is needed for this feature.")
def test_cli_with_reqs_as_file_yaml(self):
"""Runs req2flatpak by passing requirements as requirements.txt file."""
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8") as req_file:
req_file.write("\n".join(self.requirements))
req_file.flush()
req_file.seek(0)
args = ["--requirements-file", req_file.name]
real-yfprojects marked this conversation as resolved.
Show resolved Hide resolved
args += ["--target-platforms"] + self.target_platforms
args += ["--yaml"]
result = self._run_r2f(args)
build_module = yaml.safe_load(result.stdout)
self.validate_build_module(build_module)

def test_api(self):
"""Runs req2flatpak by calling its python api."""
platforms = [
Expand Down