Skip to content

Commit

Permalink
feat(docs): generate badges for hosted packages
Browse files Browse the repository at this point in the history
  • Loading branch information
fyhertz committed Jan 30, 2022
1 parent dc3be4f commit 3d03a32
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ cd wakemeops
wakemebot docs
```

Wakemebot generates badges for package hosted by wakemeops:

```markdown
[![WakeMeOps](https://docs.wakemeops.com/badges/wakemebot.svg)](https://docs.wakemeops.com/packages/wakemebot)
```

## Building single binary application

Install required build dependencies:
Expand Down
17 changes: 16 additions & 1 deletion poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mkdocs = "^1.2.3"
mkdocs-material = "^8.0.1"
semver = "3.0.0-dev.2"
pyinstaller = { version = "*", optional = true }
anybadge = "^1.8.0"

[tool.poetry.scripts]
wakemebot = "wakemebot.cli:main"
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ disallow_untyped_defs = True
warn_redundant_casts = True
strict_equality = True

[mypy-anybadge]
ignore_missing_imports = True

[mypy-semver]
ignore_missing_imports = True

Expand Down
6 changes: 4 additions & 2 deletions src/wakemebot/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RepositoryPackage:
summary: str
description: str
homepage: str
latest_version: str
versions: Dict[str, List[str]] = field(default_factory=dict) # versions per arch


Expand Down Expand Up @@ -53,15 +54,16 @@ def _parse_repository_packages_file(
"""Extract package names and versions from a repo Packages file"""
for src in Packages.iter_paragraphs(content, use_apt_pkg=False):
package_name = src["Package"]
architecture = src["Architecture"]
version = Version(src["Version"]).upstream_version
if package_name not in packages.keys():
packages[package_name] = RepositoryPackage(
description="\n".join(src["Description"].split("\n")[1:]),
homepage=src.get("Homepage", None),
name=package_name,
summary=src["Description"].split("\n")[0],
latest_version=version or "unknown",
)
architecture = src["Architecture"]
version = Version(src["Version"]).upstream_version
if architecture not in packages[package_name].versions:
packages[package_name].versions[architecture] = []
package_versions = packages[package_name].versions[architecture]
Expand Down
32 changes: 29 additions & 3 deletions src/wakemebot/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
from pathlib import Path
from typing import Any, List

import anybadge
from jinja2 import Environment

from .apt import parse_repository
from .templates import COMPONENT_PACKAGE_LIST
from .apt import RepositoryComponent, parse_repository
from .templates import COMPONENT_PACKAGE_LIST, PACKAGE_PERMALINK

environment = Environment()


@lru_cache
Expand All @@ -30,10 +33,33 @@ def update_section(section_name: str, content: Any) -> None:
file_.write_text(output)


def update_package_permalinks(component: RepositoryComponent) -> None:
packages_path = Path("docs/packages")
template = environment.from_string(PACKAGE_PERMALINK)
for package in component.packages:
content = template.render(component=component.name, package=package.name)
package_path = packages_path / package.name
package_path.mkdir(exist_ok=True, parents=True)
(package_path / "index.html").write_text(content)
print(f"Updated permalinks for {component.name} component")


def update_package_badges(component: RepositoryComponent) -> None:
badges_path = Path("docs/badges")
badges_path.mkdir(exist_ok=True, parents=True)
for package in component.packages:
badge = anybadge.Badge(
label="wakemeops", value=f"v{package.latest_version}", default_color="purple"
)
badge.write_badge(str(badges_path / f"{package.name}.svg"), overwrite=True)
print(f"Updated badges for {component.name} component")


def update_documentation(repository_url: str, distribution: str) -> None:
environment = Environment()
repository = parse_repository(repository_url, distribution)
update_section("package_count", repository.package_count)
for component in repository.components:
template = environment.from_string(COMPONENT_PACKAGE_LIST)
update_section(component.name, template.render(component=component))
update_package_permalinks(component)
update_package_badges(component)
9 changes: 9 additions & 0 deletions src/wakemebot/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@
{% endfor %}
{% endfor %}
"""

PACKAGE_PERMALINK = """\
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; url=/components/{{component}}/#{{package}}" />
</head>
</html>
"""
1 change: 1 addition & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
summary="uppercase first word, DON'T change anything else",
description="Awesome package description:\n.\n- Item 1\n- Item 2",
homepage="http://sometool.io",
latest_version="1.0",
versions={"amd64": ["1.0"]},
)
],
Expand Down

0 comments on commit 3d03a32

Please sign in to comment.