|
| 1 | +# SPDX-FileCopyrightText: 2022-present Angus Hollands <[email protected]> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import json |
| 7 | +import os.path |
| 8 | +import re |
| 9 | +import urllib.parse |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +from hatchling.metadata.plugin.interface import MetadataHookInterface |
| 13 | + |
| 14 | +AUTHOR_PATTERN = r"^([^<(]+?)?[ \t]*(?:<([^>(]+?)>)?[ \t]*(?:\(([^)]+?)\)|$)" |
| 15 | +REPOSITORY_PATTERN = r"^(?:(gist|bitbucket|gitlab|github):)?(.*?)$" |
| 16 | +REPOSITORY_TABLE = { |
| 17 | + "gitlab": "https://gitlab.com", |
| 18 | + "github": "https://github.com", |
| 19 | + "gist": "https://gist.github.com", |
| 20 | + "bitbucket": "https://bitbucket.org", |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +class NodeJSMetadataHook(MetadataHookInterface): |
| 25 | + PLUGIN_NAME = "nodejs" |
| 26 | + |
| 27 | + def __init__(self, *args, **kwargs): |
| 28 | + super().__init__(*args, **kwargs) |
| 29 | + |
| 30 | + self.__path = None |
| 31 | + self.__fields = None |
| 32 | + self.__contributors_as_maintainers = None |
| 33 | + |
| 34 | + @property |
| 35 | + def path(self) -> str: |
| 36 | + if self.__path is None: |
| 37 | + version_file = self.config.get("path", "package.json") |
| 38 | + if not isinstance(version_file, str): |
| 39 | + raise TypeError( |
| 40 | + "Option `path` for build hook `{}` must be a string".format( |
| 41 | + self.PLUGIN_NAME |
| 42 | + ) |
| 43 | + ) |
| 44 | + |
| 45 | + self.__path = version_file |
| 46 | + |
| 47 | + return self.__path |
| 48 | + |
| 49 | + @property |
| 50 | + def fields(self) -> None | set[str]: |
| 51 | + if self.__fields is None: |
| 52 | + fields = self.config.get("fields", None) |
| 53 | + if fields is None: |
| 54 | + self.__fields = None |
| 55 | + else: |
| 56 | + if not ( |
| 57 | + isinstance(fields, list) and all(isinstance(f, str) for f in fields) |
| 58 | + ): |
| 59 | + raise TypeError( |
| 60 | + "Option `fields` for build hook `{}` must be a list of strings".format( |
| 61 | + self.PLUGIN_NAME |
| 62 | + ) |
| 63 | + ) |
| 64 | + self.__fields = set(fields) |
| 65 | + return self.__fields |
| 66 | + |
| 67 | + @property |
| 68 | + def contributors_as_maintainers(self) -> bool: |
| 69 | + if self.__contributors_as_maintainers is None: |
| 70 | + self.__contributors_as_maintainers = self.config.get( |
| 71 | + "contributors-as-maintainers", True |
| 72 | + ) |
| 73 | + return self.__contributors_as_maintainers |
| 74 | + |
| 75 | + def load_package_data(self): |
| 76 | + path = os.path.normpath(os.path.join(self.root, self.path)) |
| 77 | + if not os.path.isfile(path): |
| 78 | + raise OSError(f"file does not exist: {self.path}") |
| 79 | + |
| 80 | + with open(path, "r", encoding="utf-8") as f: |
| 81 | + return json.load(f) |
| 82 | + |
| 83 | + def _parse_bugs(self, bugs: str | dict[str, str]) -> str | None: |
| 84 | + if isinstance(bugs, str): |
| 85 | + return bugs |
| 86 | + |
| 87 | + if "url" not in bugs: |
| 88 | + return None |
| 89 | + |
| 90 | + return bugs["url"] |
| 91 | + |
| 92 | + def _parse_person(self, person: dict[str, str]) -> dict[str, str]: |
| 93 | + if {"url", "email"} & person.keys(): |
| 94 | + result = {"name": person["name"]} |
| 95 | + if "email" in person: |
| 96 | + result["email"] = person["email"] |
| 97 | + else: |
| 98 | + match = re.match(AUTHOR_PATTERN, person["name"]) |
| 99 | + if match is None: |
| 100 | + raise ValueError(f"Invalid author name: {person['name']}") |
| 101 | + name, email, _ = match.groups() |
| 102 | + result = {"name": name} |
| 103 | + if email is not None: |
| 104 | + result["email"] = email |
| 105 | + |
| 106 | + return result |
| 107 | + |
| 108 | + def _parse_repository(self, repository: str | dict[str, str]) -> str: |
| 109 | + if isinstance(repository, str): |
| 110 | + match = re.match(REPOSITORY_PATTERN, repository) |
| 111 | + if match is None: |
| 112 | + raise ValueError(f"Invalid repository string: {repository}") |
| 113 | + kind, identifier = match.groups() |
| 114 | + if kind is None: |
| 115 | + kind = "github" |
| 116 | + return urllib.parse.urljoin(REPOSITORY_TABLE[kind], identifier) |
| 117 | + |
| 118 | + return repository["url"] |
| 119 | + |
| 120 | + def update(self, metadata: dict[str, Any]): |
| 121 | + package = self.load_package_data() |
| 122 | + |
| 123 | + new_metadata = {"name": package["name"]} |
| 124 | + |
| 125 | + authors = None |
| 126 | + maintainers = None |
| 127 | + |
| 128 | + if "author" in package: |
| 129 | + authors = [self._parse_person(package["author"])] |
| 130 | + |
| 131 | + if "contributors" in package: |
| 132 | + contributors = [ |
| 133 | + self._parse_person(p) for p in package["contributors"] |
| 134 | + ] |
| 135 | + if self.contributors_as_maintainers: |
| 136 | + maintainers = contributors |
| 137 | + else: |
| 138 | + authors = [*(authors or []), *contributors] |
| 139 | + |
| 140 | + if authors is not None: |
| 141 | + new_metadata['authors'] = authors |
| 142 | + |
| 143 | + if maintainers is not None: |
| 144 | + new_metadata['maintainers'] = maintainers |
| 145 | + |
| 146 | + if "keywords" in package: |
| 147 | + new_metadata["keywords"] = package["keywords"] |
| 148 | + |
| 149 | + if "description" in package: |
| 150 | + new_metadata["description"] = package["description"] |
| 151 | + |
| 152 | + if "license" in package: |
| 153 | + new_metadata["license"] = package["license"] |
| 154 | + |
| 155 | + # Construct URLs |
| 156 | + urls = {} |
| 157 | + if "homepage" in package: |
| 158 | + urls["homepage"] = package["homepage"] |
| 159 | + if "bugs" in package: |
| 160 | + bugs_url = self._parse_bugs(package["bugs"]) |
| 161 | + if bugs_url is not None: |
| 162 | + urls["bug tracker"] = bugs_url |
| 163 | + if "repository" in package: |
| 164 | + urls["repository"] = self._parse_repository(package["repository"]) |
| 165 | + |
| 166 | + # Write URLs |
| 167 | + if urls: |
| 168 | + new_metadata["urls"] = urls |
| 169 | + |
| 170 | + # Only use required metadata |
| 171 | + metadata.update( |
| 172 | + { |
| 173 | + k: v |
| 174 | + for k, v in new_metadata.items() |
| 175 | + if (self.fields is None or k in self.fields) |
| 176 | + } |
| 177 | + ) |
0 commit comments