1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
#!/usr/bin/env python
#
# additional_files.py
"""
Parser for the ``additional-files`` option.
"""
#
# Copyright © 2020-2023 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
#
# stdlib
import abc
from typing import Any, Dict, Iterable, Iterator, List, Optional
from warnings import warn
# 3rd party
import attr
from dom_toml.parser import BadConfigError
from domdf_python_tools.paths import PathPlus, sort_paths
__all__ = ["AdditionalFilesEntry", "Exclude", "Include", "RecursiveExclude", "RecursiveInclude", "from_entry"]
class AdditionalFilesEntry(abc.ABC):
"""
An abstract command in ``additional-files``.
"""
@classmethod
@abc.abstractmethod
def parse(cls, parameters: str) -> "AdditionalFilesEntry":
"""
Parse the command's parameters.
:param parameters:
"""
raise NotImplementedError
@abc.abstractmethod
def iter_files(self, directory: PathPlus) -> Iterator[PathPlus]:
"""
Returns an iterator over files to be included or excluded by this command.
:param directory: The project or build directory.
"""
raise NotImplementedError
@abc.abstractmethod
def to_dict(self) -> Dict[str, Any]:
"""
Returns a dictionary representation of the command entry.
"""
raise NotImplementedError
def _to_list(_: Iterable[str]) -> List[str]:
return list(_)
@attr.define
class Include(AdditionalFilesEntry):
"""
Include a single file, or multiple files with a pattern.
"""
#: Glob patterns (with complete paths from the project root)
patterns: List[str] = attr.field(converter=_to_list)
@classmethod
def parse(cls, parameters: str) -> "Include":
"""
Parse the command's parameters.
:param parameters:
"""
if not parameters:
raise BadConfigError(f"additional-files: 'include' must have at least one path or pattern specified.")
return cls(parameters.split(' '))
def iter_files(self, directory: PathPlus) -> Iterator[PathPlus]:
"""
Returns an iterator over files to be included by this command.
:param directory: The project directory.
"""
for include_pat in self.patterns:
for include_file in sorted(directory.glob(include_pat)):
if include_file.is_file():
yield include_file
def to_dict(self) -> Dict[str, Any]:
"""
Returns a dictionary representation of the command entry.
"""
return {
"command": "include",
**attr.asdict(self),
}
@attr.define
class Exclude(AdditionalFilesEntry):
"""
Exclude a single file, or multiple files with a pattern.
"""
#: Glob patterns (with complete paths from the project root)
patterns: List[str] = attr.field(converter=_to_list)
@classmethod
def parse(cls, parameters: str) -> "Exclude":
"""
Parse the command's parameters.
:param parameters:
"""
if not parameters:
raise BadConfigError(f"additional-files: 'exclude' must have at least one path or pattern specified.")
return cls(parameters.split(' '))
def iter_files(self, directory: PathPlus) -> Iterator[PathPlus]:
"""
Returns an iterator over files to be excluded by this command.
:param directory: The build directory.
"""
for exclude_pat in self.patterns:
for exclude_file in sorted(directory.glob(exclude_pat)):
if exclude_file.is_file():
yield exclude_file
def to_dict(self) -> Dict[str, Any]:
"""
Returns a dictionary representation of the command entry.
"""
return {
"command": "exclude",
**attr.asdict(self),
}
@attr.define
class RecursiveInclude(AdditionalFilesEntry):
"""
Recursively include files in a directory based on patterns.
"""
#: The directory to start from.
path: str
#: Glob patterns.
patterns: List[str] = attr.field(converter=_to_list)
@classmethod
def parse(cls, parameters: str) -> "RecursiveInclude":
"""
Parse the command's parameters.
:param parameters:
"""
parts = parameters.split(' ')
if len(parts) < 2:
raise BadConfigError(
f"additional-files: 'recursive-include' must have one path and at least one pattern specified."
)
return cls(parts[0], parts[1:])
def iter_files(self, directory: PathPlus) -> Iterator[PathPlus]:
"""
Returns an iterator over files to be included by this command.
:param directory: The project directory.
"""
for include_pat in self.patterns:
for include_file in sort_paths(*(directory / self.path).rglob(include_pat)):
if "__pycache__" in include_file.parts:
continue
if include_file.is_file():
yield include_file
def to_dict(self) -> Dict[str, Any]:
"""
Returns a dictionary representation of the command entry.
"""
return {
"command": "recursive-include",
**attr.asdict(self),
}
@attr.define
class RecursiveExclude(AdditionalFilesEntry):
"""
Recursively exclude files in a directory based on patterns.
"""
#: The directory to start from.
path: str
#: Glob patterns.
patterns: List[str] = attr.field(converter=_to_list)
@classmethod
def parse(cls, parameters: str) -> "RecursiveExclude":
"""
Parse the command's parameters.
:param parameters:
"""
parts = parameters.split(' ')
if len(parts) < 2:
raise BadConfigError(
f"additional-files: 'recursive-exclude' must have one path and at least one pattern specified."
)
return cls(parts[0], parts[1:])
def iter_files(self, directory: PathPlus) -> Iterator[PathPlus]:
"""
Returns an iterator over files to be excluded by this command.
:param directory: The build directory.
"""
for exclude_pat in self.patterns:
for exclude_file in sort_paths(*(directory / self.path).rglob(exclude_pat)):
if exclude_file.is_file():
yield exclude_file
def to_dict(self) -> Dict[str, Any]:
"""
Returns a dictionary representation of the command entry.
"""
return {
"command": "recursive-exclude",
**attr.asdict(self),
}
def from_entry(line: str) -> Optional[AdditionalFilesEntry]:
"""
Parse a `MANIFEST.in`_-style entry.
.. _MANIFEST.in: https://packaging.python.org/guides/using-manifest-in/
:param line:
:returns: An :class:`~.AdditionalFilesEntry` for known commands,
or :py:obj:`None` if an unknown command is found in the entry.
"""
command, *parameters = line.split(' ')
parameter_string = ' '.join(parameters)
if command == "include":
return Include.parse(parameter_string)
elif command == "exclude":
return Exclude.parse(parameter_string)
elif command == "recursive-include":
return RecursiveInclude.parse(parameter_string)
elif command == "recursive-exclude":
return RecursiveExclude.parse(parameter_string)
else: # pragma: no cover
warn(f"Unsupported command in 'additional-files': {line}")
return None
|