Coverage for r11k/puppet.py: 100%
6 statements
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-13 21:48 +0100
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-13 21:48 +0100
1"""
2Python mapping of Puppet's metadata files.
4Each Puppet module *ought* to contain a `metadata.json` file, and
5*all* modules on the Puppet Forge has a metadata file, including ways
6to retrieve only them through the Forge's API.
8Puppets metadata format is [documented here][metadata], and is mapped
9almost directly here. Worth noting that items are mapped to python
10types.
12All constructors take any extra keyword arguments to future proof
13against future versions of Puppets metadata files.
15[metadata]: https://puppet.com/docs/puppet/7/modules_metadata.html
16"""
18from typing import (
19 Any,
20 Optional,
21 Sequence,
22 Union,
23)
25from .interval import Interval
26from .util import unfix_name
27from .version import parse_version
30class DependencySpec: # pragma: no cover
31 """
32 A single dependency of a puppet module.
34 :param name: is (probly) a the Forge name
35 :param version_requirement: is a string on the form `>= 4.1.0 < 8.0.0"
36 """
38 def __init__(self, name: str, version_requirement: str):
39 self.name = unfix_name(name)
40 self.version_requirement = version_requirement
42 def interval(self, by: str) -> Interval:
43 """Return maching interval object."""
44 cmp_a, min, cmp_b, max = parse_version(self.version_requirement)
45 return Interval(min, max, cmp_a, cmp_b, by=by)
47 def __repr__(self) -> str:
48 return f'DependencySpec({repr(self.name)}, {repr(self.version_requirement)})'
51class OSSupport:
52 """Operatingsystem a puppet module supports."""
54 def __init__(self,
55 operatingsystem: str,
56 operatingsystemrelease: Optional[list[str]] = None,
57 **_: Any): # pragma: no cover
58 self.operatingsystem = operatingsystem
59 self.operatingsystemrelease = operatingsystemrelease
62class PuppetMetadata:
63 """
64 Metadata of a puppet module.
66 The parameters which are unions between a simple Python type, and
67 a fully realized type *will* get parsed into the fully realized
68 type in the constructor.
69 """
71 def __init__(self,
72 name: str,
73 version: str,
74 author: str,
75 license: str,
76 summary: str,
77 source: str,
78 dependencies: Sequence[Union[dict, DependencySpec]],
79 requirements: list[Union[dict, DependencySpec]] = [],
80 project_page: Optional[str] = None,
81 issues_url: Optional[str] = None,
82 operatingsystem_support: Optional[list[Union[dict, OSSupport]]] = None,
83 tags: Optional[list[str]] = None,
84 **_: Any): # pragma: no cover
85 self.name: str = name
86 """
87 Concatenation of owner's Forge username with the modules
88 name, on the form *forge username*-*module name*.
89 """
90 self.version: str = version
91 """Semver formatted string of version."""
92 self.author: str = author
93 """Arbitrarly formatted name of author."""
94 self.license: str = license
95 """Software License of module.
97 *Should* be present in [SPDX's list of licenses](https://spdx.org/licenses/)
98 """
99 self.summary: str = summary
100 """One-line summary of module."""
101 self.source: str = source
102 """URL to source code of module."""
103 self.dependencies: list[DependencySpec] \
104 = [d if isinstance(d, DependencySpec) else DependencySpec(**d)
105 for d in dependencies]
106 """List of other Puppet modules this module dependes on."""
107 self.requirements: list[DependencySpec] \
108 = [d if isinstance(d, DependencySpec) else DependencySpec(**d)
109 for d in requirements]
110 """List of external requirements.
112 This includes Puppet version."""
113 self.project_page: Optional[str] = project_page
114 """URL to project page of module."""
115 self.issues_url: Optional[str] = issues_url
116 """URL to where issues can be submitted."""
117 self.operatingsystem_support: Optional[list[OSSupport]]
118 """List of which operating systems this module is "compatible" with."""
119 if operatingsystem_support:
120 self.operatingsystem_support \
121 = [x if isinstance(x, OSSupport) else OSSupport(**x)
122 for x in operatingsystem_support]
123 else:
124 self.operatingsystem_support = None
125 self.tags: Optional[list[str]] = tags
126 """Keywords for module discovery."""