mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-10 04:25:47 +00:00
better templating
This commit is contained in:
@ -47,10 +47,10 @@ class Application:
|
||||
return cls(args.architecture, config)
|
||||
|
||||
def _known_packages(self) -> Set[str]:
|
||||
known_packages = set()
|
||||
known_packages: Set[str] = set()
|
||||
# local set
|
||||
for package in self.repository.packages():
|
||||
known_packages.update(package.packages)
|
||||
known_packages.update(package.packages.keys())
|
||||
known_packages.update(self.repository.pacman.all_packages())
|
||||
return known_packages
|
||||
|
||||
|
@ -20,11 +20,12 @@
|
||||
import jinja2
|
||||
import os
|
||||
|
||||
from typing import Dict
|
||||
from typing import Dict, Iterable
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.report.report import Report
|
||||
from ahriman.core.util import package_like
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.sign_settings import SignSettings
|
||||
|
||||
|
||||
@ -32,37 +33,42 @@ class HTML(Report):
|
||||
|
||||
def __init__(self, architecture: str, config: Configuration) -> None:
|
||||
Report.__init__(self, architecture, config)
|
||||
self.architecture = architecture
|
||||
section = config.get_section_name('html', architecture)
|
||||
self.report_path = config.get(section, 'path')
|
||||
|
||||
self.link_path = config.get(section, 'link_path')
|
||||
self.template_path = config.get(section, 'template_path')
|
||||
|
||||
# base template vars
|
||||
self.sign_targets = [SignSettings.from_option(opt) for opt in config.getlist('sign', 'target')]
|
||||
self.pgp_key = config.get('sign', 'key', fallback=None)
|
||||
self.homepage = config.get(section, 'homepage', fallback=None)
|
||||
self.repository = config.get('repository', 'name')
|
||||
|
||||
def generate(self, path: str) -> None:
|
||||
sign_section = config.get_section_name('sign', architecture)
|
||||
self.sign_targets = [SignSettings.from_option(opt) for opt in config.getlist(sign_section, 'target')]
|
||||
self.pgp_key = config.get(sign_section, 'key') if self.sign_targets else None
|
||||
|
||||
def generate(self, packages: Iterable[Package]) -> None:
|
||||
# idea comes from https://stackoverflow.com/a/38642558
|
||||
templates_dir, template_name = os.path.split(self.template_path)
|
||||
loader = jinja2.FileSystemLoader(searchpath=templates_dir)
|
||||
environment = jinja2.Environment(loader=loader)
|
||||
template = environment.get_template(template_name)
|
||||
|
||||
packages: Dict[str, str] = {}
|
||||
for fn in sorted(os.listdir(path)):
|
||||
if not package_like(fn):
|
||||
continue
|
||||
packages[fn] = f'{self.link_path}/{fn}'
|
||||
content = [
|
||||
{
|
||||
'filename': filename,
|
||||
'name': package,
|
||||
'version': base.version
|
||||
} for base in packages for package, filename in base.packages.items()
|
||||
]
|
||||
|
||||
html = template.render(
|
||||
architecture=self.architecture,
|
||||
homepage=self.homepage,
|
||||
link_path=self.link_path,
|
||||
has_package_signed=SignSettings.SignPackages in self.sign_targets,
|
||||
has_repo_signed=SignSettings.SignRepository in self.sign_targets,
|
||||
packages=packages,
|
||||
packages=content,
|
||||
pgp_key=self.pgp_key,
|
||||
repository=self.repository)
|
||||
|
||||
|
@ -19,8 +19,11 @@
|
||||
#
|
||||
import logging
|
||||
|
||||
from typing import Iterable
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.exceptions import ReportFailed
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.report_settings import ReportSettings
|
||||
|
||||
|
||||
@ -32,7 +35,7 @@ class Report:
|
||||
self.config = config
|
||||
|
||||
@staticmethod
|
||||
def run(architecture: str, config: Configuration, target: str, path: str) -> None:
|
||||
def run(architecture: str, config: Configuration, target: str, packages: Iterable[Package]) -> None:
|
||||
provider = ReportSettings.from_option(target)
|
||||
if provider == ReportSettings.HTML:
|
||||
from ahriman.core.report.html import HTML
|
||||
@ -41,10 +44,10 @@ class Report:
|
||||
report = Report(architecture, config)
|
||||
|
||||
try:
|
||||
report.generate(path)
|
||||
report.generate(packages)
|
||||
except Exception:
|
||||
report.logger.exception('report generation failed', exc_info=True)
|
||||
raise ReportFailed()
|
||||
|
||||
def generate(self, path: str) -> None:
|
||||
def generate(self, packages: Iterable[Package]) -> None:
|
||||
pass
|
@ -114,11 +114,12 @@ class Repository:
|
||||
except Exception:
|
||||
self.logger.exception(f'could not remove {package}', exc_info=True)
|
||||
|
||||
requested = set(packages)
|
||||
for local in self.packages():
|
||||
if local.base in packages:
|
||||
to_remove = local.packages
|
||||
elif local.packages.intersection(packages):
|
||||
to_remove = local.packages.intersection(packages)
|
||||
to_remove = set(local.packages.keys())
|
||||
elif requested.intersection(local.packages.keys()):
|
||||
to_remove = requested.intersection(local.packages.keys())
|
||||
else:
|
||||
to_remove = set()
|
||||
self.web.remove(local.base, to_remove)
|
||||
@ -131,7 +132,7 @@ class Repository:
|
||||
if targets is None:
|
||||
targets = self.config.getlist('report', 'target')
|
||||
for target in targets:
|
||||
Report.run(self.architecture, self.config, target, self.paths.repository)
|
||||
Report.run(self.architecture, self.config, target, self.packages())
|
||||
|
||||
def process_sync(self, targets: Optional[Iterable[str]]) -> None:
|
||||
if targets is None:
|
||||
|
@ -61,8 +61,8 @@ class Leaf:
|
||||
:param packages:
|
||||
:return: true if any of packages is dependency of the leaf, false otherwise
|
||||
'''
|
||||
for package in packages:
|
||||
if package.package.packages.intersection(self.dependencies):
|
||||
for leaf in packages:
|
||||
if self.dependencies.intersection(leaf.package.packages.keys()):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
@ -24,9 +24,9 @@ import os
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from dataclasses import dataclass
|
||||
from srcinfo.parse import parse_srcinfo
|
||||
from typing import List, Optional, Set, Type
|
||||
from typing import Dict, List, Optional, Set, Type
|
||||
|
||||
from ahriman.core.alpm.pacman import Pacman
|
||||
from ahriman.core.exceptions import InvalidPackageInfo
|
||||
@ -38,7 +38,7 @@ class Package:
|
||||
base: str
|
||||
version: str
|
||||
aur_url: str
|
||||
packages: Set[str] = field(default_factory=set)
|
||||
packages: Dict[str, str] # map of package name to archive name
|
||||
|
||||
@property
|
||||
def git_url(self) -> str:
|
||||
@ -82,12 +82,12 @@ class Package:
|
||||
@classmethod
|
||||
def from_archive(cls: Type[Package], path: str, pacman: Pacman, aur_url: str) -> Package:
|
||||
package = pacman.handle.load_pkg(path)
|
||||
return cls(package.base, package.version, aur_url, {package.name})
|
||||
return cls(package.base, package.version, aur_url, {package.name: os.path.basename(path)})
|
||||
|
||||
@classmethod
|
||||
def from_aur(cls: Type[Package], name: str, aur_url: str)-> Package:
|
||||
package = aur.info(name)
|
||||
return cls(package.package_base, package.version, aur_url, {package.name})
|
||||
return cls(package.package_base, package.version, aur_url, {package.name: ''})
|
||||
|
||||
@classmethod
|
||||
def from_build(cls: Type[Package], path: str, aur_url: str) -> Package:
|
||||
@ -95,7 +95,7 @@ class Package:
|
||||
src_info, errors = parse_srcinfo(fn.read())
|
||||
if errors:
|
||||
raise InvalidPackageInfo(errors)
|
||||
packages = set(src_info['packages'].keys())
|
||||
packages = {key: '' for key in src_info['packages'].keys()}
|
||||
version = cls.full_version(src_info.get('epoch'), src_info['pkgver'], src_info['pkgrel'])
|
||||
|
||||
return cls(src_info['pkgbase'], version, aur_url, packages)
|
||||
|
Reference in New Issue
Block a user