(
cls,
script, # type: Text
source=_UNSPECIFIED_SOURCE, # type: str
)
| 233 | class ScriptMetadata(object): |
| 234 | @classmethod |
| 235 | def parse( |
| 236 | cls, |
| 237 | script, # type: Text |
| 238 | source=_UNSPECIFIED_SOURCE, # type: str |
| 239 | ): |
| 240 | # type: (...) -> ScriptMetadata |
| 241 | |
| 242 | # The spec this code follows was defined in PEP-723: https://peps.python.org/pep-0723/ |
| 243 | # and now lives here: |
| 244 | # https://packaging.python.org/specifications/inline-script-metadata#inline-script-metadata |
| 245 | script_metadata_block = parse_metadata_blocks(script, source=source).get("script") |
| 246 | if not script_metadata_block: |
| 247 | return cls() |
| 248 | script_metadata = script_metadata_block.parse_metadata() |
| 249 | |
| 250 | raw_dependencies = script_metadata.get("dependencies", []) |
| 251 | if not isinstance(raw_dependencies, list): |
| 252 | raise script_metadata_block.create_metadata_error( |
| 253 | "contains an invalid `dependencies` value of type `{type}`\n" |
| 254 | "Expected a list of dependency specifier strings".format( |
| 255 | type=type(raw_dependencies).__name__ |
| 256 | ) |
| 257 | ) |
| 258 | |
| 259 | invalid_dependencies = [] # type: List[str] |
| 260 | dependencies = [] # type: List[ParsedRequirement] |
| 261 | for index, req in enumerate(raw_dependencies): |
| 262 | try: |
| 263 | dependencies.append( |
| 264 | as_parsed_requirement(Requirement.parse(req), line=LogicalLine.from_str(req)) |
| 265 | ) |
| 266 | except RequirementParseError as e: |
| 267 | invalid_dependencies.append( |
| 268 | "+ dependencies[{index}] {req!r}: {err}".format(index=index, req=req, err=e) |
| 269 | ) |
| 270 | if invalid_dependencies: |
| 271 | raise script_metadata_block.create_metadata_error( |
| 272 | "contains a `dependencies` list with {count} invalid dependency {specifiers}:\n" |
| 273 | "{invalid_dependencies}".format( |
| 274 | count=len(invalid_dependencies), |
| 275 | specifiers=pluralize(invalid_dependencies, "specifier"), |
| 276 | invalid_dependencies="\n".join(invalid_dependencies), |
| 277 | ) |
| 278 | ) |
| 279 | |
| 280 | raw_requires_python = script_metadata.get("requires-python", "") |
| 281 | if not isinstance(raw_requires_python, string): |
| 282 | raise script_metadata_block.create_metadata_error( |
| 283 | "contains an invalid `requires-python` value of type `{type}`\n" |
| 284 | "Expected a version specifier string".format( |
| 285 | type=type(raw_requires_python).__name__ |
| 286 | ) |
| 287 | ) |
| 288 | try: |
| 289 | requires_python = SpecifierSet(raw_requires_python) |
| 290 | except InvalidSpecifier as e: |
| 291 | raise script_metadata_block.create_metadata_error( |
| 292 | "contains an invalid `requires-python` value {value!r}: {err}".format( |
nothing calls this directly
no test coverage detected