Archive Paths API
pathapi.archive
Zip and tar archives as trees of paths, on the standard library alone
(zipfile, tarfile).
ArchivePath, ZipPath and TarPath are Paths for the members of an
archive file, which is any Path; they need no URI.
MAX_LINKS = 40
module-attribute
The most link members one lookup follows before it is a loop
(OSError(ELOOP)).
ArchivePath(*segments, archive=None)
Bases: _WritableMembers, TreePath
A member path of an archive file, which is any Path (a LocalPath, a
MemPath, a path on a remote store). The segments are the member path; the
archive's format, zip or tar, is read from its extension or its first bytes
on first use, and ZipPath and TarPath fix it. Building a path, printing
it and joining names onto it read nothing.
A zip archive that is a local file can be written (new members,
unlink/rmdir/rename, batch()); every other archive is read-only.
One backend serves each archive file and format, so paths built separately
for one file see each other's writes.
Source code in src/pathapi/archive/_path.py
58 59 60 61 62 63 64 65 66 67 | |
archive
property
The archive file. Never decides the format.
backend
property
The handle on the archive, shared by every path of it: settles the
format of an ArchivePath on the first call, which reads the archive
when its extension does not say.
as_uri()
Source code in src/pathapi/archive/_path.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
backend_for(archive, *, shared=True, identity=None)
classmethod
The backend that serves archive, to pass as archive= of any path
of the class: the backend of another path of the same archive is
returned as it is. With shared the one backend of that archive file
and format (found by its real path for a local file, else by identity
or the node or URI of the path); with shared=False a backend of its
own. Reads nothing: the format of ArchivePath is decided on first
use.
Source code in src/pathapi/archive/_path.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
iterdir()
Source code in src/pathapi/archive/_path.py
226 227 228 | |
readlink()
The text a link member stores, as a LinkTarget (OSError(EINVAL)
when this member is not a link). Links on the way to it are followed.
Source code in src/pathapi/archive/_path.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
refresh()
Forgets what is held of the archive file, so the next use reads it again. A non-local archive is read once and kept while any path to it lives, so this is how a later change at its source is seen; a local one is checked against the file on every use. Every path to the same archive shares the refresh.
Source code in src/pathapi/archive/_path.py
113 114 115 116 117 118 119 120 121 122 123 124 | |
stat(*, follow_symlinks=True)
Source code in src/pathapi/archive/_path.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
LinkTarget
Bases: PurePosixPath
What readlink() of a link member returns: the stored text as a
PurePosixPath, which spells it normalized (./a//b is a/b), and as
path, which is the text exactly as stored. PathSyncer and copy()
recreate a link from path.
path
property
TarPath(*segments, archive=None)
Bases: ArchivePath
A member path of a tar archive, plain or compressed (tarfile's
detected r:* mode), whatever its extension. Read-only. Members stored
with a ./ prefix are addressed without it.
Source code in src/pathapi/archive/_path.py
58 59 60 61 62 63 64 65 66 67 | |
ZipPath(*segments, archive=None)
Bases: ArchivePath
A member path of a zip archive, whatever its extension. Read/write when
the archive file is a local Path; every other archive is read-only (it
is fetched whole into memory first). Every change replaces the archive
atomically (a temporary file next to it, then os.replace), so the result
is a new file: other hard links keep the old content, and an archive the
caller may not write is refused first. A new member is appended, deflated,
to a byte copy of the archive (nothing recompressed); overwriting,
deleting or renaming a member rewrites the archive, keeping every other
member's metadata and compression method.
Source code in src/pathapi/archive/_path.py
58 59 60 61 62 63 64 65 66 67 | |
detect_format(name, peek=None)
Detect "zip" vs "tar" from name's extension, falling back to
magic-byte sniffing (PK header -> zip) via peek() -- called lazily,
only when the extension is inconclusive, so callers backed by a remote
Path don't pay for a round trip in the common case. Shared by
unpack_archive and ArchivePath so both use one detection policy.
Source code in src/pathapi/archive/_pack.py
19 20 21 22 23 24 25 26 27 28 29 30 31 | |
make_archive(src, format, target)
Create an archive file from src at target.
Supports format='zip' and format='tar'.
Operations run stream-first to support any Path implementation, for
src as well as target. The archive is built in a temporary buffer
and written to target only once complete, so a failure leaves an
existing target untouched. Zip members always carry zip64 headers,
so a member over 2 GiB is stored instead of failing after the fact.
A directory is a member of its own, empty ones included. A member carries
the modification time its source reports (a zip date from 1980, a tar time
in whole seconds; none reported gives 1980-01-01 or the epoch) and, in a
tar, the permission bits it reports (0o644 for a file and 0o755 for a
directory when it reports none). A tar member's size is the number of
bytes read from the source, not what stat() says: a backend may not know
it, or count it differently.
Source code in src/pathapi/archive/_pack.py
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 | |
unpack_archive(archive, dest)
Extract archive file into dest directory.
Supports format detection from filename.
Operations run stream-first to support any Path implementation.
Members whose name would land outside dest (a .. part, or on a
Windows-flavoured dest a drive such as D:x or C:..) are skipped.
On a Windows-flavoured dest so is a member with a part Windows would
store under another name (a trailing dot or space) or send to a device
(NUL, CON, COM1, ...), with a UserWarning naming it.
A non-seekable archive stream (e.g. an HTTP response) is buffered first.
A tar hard link, or a symlink to a regular file inside the archive, is
extracted as a regular file with the target member's content (no link
is created, so dest needs no symlink support); a link that does not
resolve to such a member (it names nothing, a directory, or itself
through other links) is skipped with a UserWarning.
Source code in src/pathapi/archive/_pack.py
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |