File: record.py

package info (click to toggle)
python-dist-meta 0.9.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,964 kB
  • sloc: python: 2,944; makefile: 9
file content (319 lines) | stat: -rw-r--r-- 8,737 bytes parent folder | download
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python3
#
#  record.py
"""
Classes to model parts of ``RECORD`` files.
"""
#
#  Copyright © 2021 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 csv
import os
import pathlib
import posixpath
import sys
from base64 import urlsafe_b64decode, urlsafe_b64encode
from typing import TYPE_CHECKING, NamedTuple, Optional, Type, TypeVar

# 3rd party
from domdf_python_tools.stringlist import DelimitedList
from domdf_python_tools.typing import PathLike

if TYPE_CHECKING:
	# stdlib
	from hashlib import _Hash

	# this package
	from dist_meta.distributions import Distribution
else:
	try:
		# 3rd party
		from _hashlib import HASH as _Hash
	except ImportError:  # pragma: no cover
		try:
			# 3rd party
			from _hashlib import Hash as _Hash
		except ImportError:
			pass

__all__ = ("FileHash", "RecordEntry")

_RE = TypeVar("_RE", bound="RecordEntry")
_FH = TypeVar("_FH", bound="FileHash")


class RecordEntry(pathlib.PurePosixPath):
	"""
	Represents a path in a distribution.

	:param path: The path to the file in the distribution, relative to the distribution root
		(i.e. the ``site-packages`` directory).
	:param hash: The hash/checksum of the file.
	:param size: The size of the file.
	:param distro: The distribution the file belongs to.

	.. note::

		Path operations (:meth:`~.pathlib.PurePath.joinpath`, :attr:`~.pathlib.PurePath.parent` etc.)
		will return a standard :class:`pathlib.PurePosixPath` object without the extended attributes of this class.
	"""

	__slots__ = ("hash", "size", "distro")

	#: The name of the file in the distribution.
	name: str

	#: The hash/checksum of the file.
	hash: Optional["FileHash"]  # noqa: A003  # pylint: disable=redefined-builtin

	#: The size of the file.
	size: Optional[int]

	#: The distribution the file belongs to.
	distro: Optional["Distribution"]

	def __init__(
			self,
			path: PathLike,
			hash: Optional["FileHash"] = None,  # noqa: A002  # pylint: disable=redefined-builtin
			size: Optional[int] = None,
			distro: Optional["Distribution"] = None,
			):
		if sys.version_info < (3, 12):  # pragma: no cover (py312+)
			super().__init__()
		else:  # pragma: no cover (<py312)
			super().__init__(self._coerce_path(path))

	def __new__(
			cls: Type[_RE],
			path: PathLike,
			hash: Optional["FileHash"] = None,  # noqa: A002  # pylint: disable=redefined-builtin
			size: Optional[int] = None,
			distro: Optional["Distribution"] = None,
			) -> _RE:
		"""
		Construct a :class:`RecordEntry` from one a string or an existing :class:`pathlib.PurePath` object.
		"""

		self = super().__new__(cls, cls._coerce_path(path))
		self.hash = hash
		self.size = size
		self.distro = distro
		return self

	@classmethod
	def _coerce_path(cls, path: PathLike) -> PathLike:
		"""
		Necessary to fix issue in Python 3.12 where path separators are no longer converted.
		"""

		if os.path.isabs(path):
			raise ValueError("RecordEntry paths cannot be absolute")

		if isinstance(path, pathlib.PurePath):
			path_p = path
		else:
			path_p = pathlib.PurePath(path)

		if path_p.is_absolute():
			# Catch absolute paths from other platform
			raise ValueError("RecordEntry paths cannot be absolute")
		path = path_p.as_posix()
		if posixpath.isabs(path):
			raise ValueError("RecordEntry paths cannot be absolute")

		return path

	def read_text(
			self,
			encoding: Optional[str] = "UTF-8",
			errors: Optional[str] = None,
			) -> str:
		"""
		Open the file in text mode, read it, and close the file.

		:param encoding: The encoding to write to the file in.
		:param errors:

		:return: The content of the file.

		.. attention:: This operation requires a value for :attr:`self.distro <.RecordEntry.distro>`.
		"""

		if self.distro is None:
			raise ValueError("Cannot read files with 'self.distro = None'")

		return (self.distro.path.parent / self).read_text(encoding=encoding, errors=errors)

	def read_bytes(self) -> bytes:
		"""
		Open the file in bytes mode, read it, and close the file.

		:return: The content of the file.

		.. attention:: This operation requires a value for :attr:`self.distro <.RecordEntry.distro>`.
		"""

		if self.distro is None:
			raise ValueError("Cannot read files with 'self.distro = None'")

		return (self.distro.path.parent / self).read_bytes()

	def __repr__(self) -> str:
		"""
		Return a string representation of the :class:`~.RecordEntry`.
		"""

		parts = DelimitedList([f"{os.fspath(self)!r}"])
		if self.hash is not None:
			parts.append(f"hash={self.hash!r}")
		if self.size is not None:
			parts.append(f"size={self.size!r}")
		if self.distro is not None:
			parts.append(f"distro={self.distro!r}")
		return f"{self.__class__.__name__}({parts:, })"

	def as_record_entry(self) -> str:
		"""
		Returns an entry for a ``RECORD`` file, in the form ``<name>,<hash>,<size>``.
		"""

		parts = [self.as_posix()]

		if self.hash is not None:
			parts.append(self.hash.to_string())
		else:
			parts.append('')

		if self.size is not None:
			parts.append(str(self.size))
		else:
			parts.append('')

		return ','.join(parts)

	@classmethod
	def from_record_entry(
			cls: Type[_RE],
			entry: str,
			distro: Optional["Distribution"] = None,
			) -> _RE:
		"""
		Construct a :class:`~.RecordEntry` from a line in a ``RECORD`` file, in the form ``<name>,<hash>,<size>``.

		.. versionadded:: 0.2.0

		:param entry:
		:param distro: The distribution the ``RECORD`` file belongs to. Optional.

		:rtype: :class:`~.RecordEntry`
		"""

		entry = entry.strip()
		lines = entry.splitlines()

		if len(lines) != 1:
			raise ValueError("'entry' must be a single-line entry.")

		entry = lines[0]

		if '"' in entry:
			name, hash_, size_str, *_ = next(csv.reader((entry, )))
		else:
			name, hash_, size_str, *_ = entry.split(',')

		hash_ = hash_.strip()
		size_str = size_str.strip()

		return cls(
				name.strip(),
				hash=FileHash.from_string(hash_) if hash_ else None,
				size=int(size_str) if size_str else None,
				distro=distro,
				)


class FileHash(NamedTuple):
	"""
	Represents a checksum for a file in a ``RECORD`` file, or as the URL fragment in a :pep:`503` repository URL.
	"""

	#: The name of the hash algorithm.
	name: str

	#: The :func:`~.base64.urlsafe_b64encode`'d hexdigest of the hash.
	value: str

	@classmethod
	def from_string(cls: Type[_FH], string: str) -> _FH:
		"""
		Constructs a  :class:`~.FileHash` from a  string in the form ``<name>=<value>``.

		:param string:

		:rtype: :class:`~.FileHash`
		"""

		name, _, value = string.partition('=')
		return cls(name.strip(), value.strip())

	def to_string(self) -> str:
		"""
		Returns the :class:`~.FileHash` as a string, in the form ``<name>=<value>``.
		"""

		return f"{self.name}={self.value}"

	def digest(self) -> bytes:
		"""
		Returns the digest of the hash.

		This is a bytes object which may contain bytes in the whole range from 0 to 255.
		"""

		return urlsafe_b64decode(f"{self.value}==".encode("latin1"))

	def hexdigest(self) -> str:
		"""
		Like :meth:`self.digest() <.FileHash.digest>` except the digest is returned as
		a string object of double length, containing only hexadecimal digits.

		This may be used to exchange the value safely in email or other non-binary environments.
		"""  # noqa: D400

		return ''.join(f"{x:0{2}x}" for x in self.digest())

	@classmethod
	def from_hash(cls: Type[_FH], the_hash: "_Hash") -> _FH:
		"""
		Construct a :class:`~.FileHash` object from a :mod:`hashlib` hash object.

		:param the_hash:
		:type the_hash: :mod:`hashlib.HASH <hashlib>`

		:rtype: :class:`~.FileHash`
		"""

		name = the_hash.name
		value = urlsafe_b64encode(the_hash.digest()).decode("latin1").rstrip('=')
		return cls(name, value)