Release notes for pymetadata 0.9.0
Changes
-
pymetadata.omexreads and writes COMBINE archives throughpymetadata-core, which is now a dependency of pymetadata with a floor of 0.4.0 (#99). There is no pure-python fallback. The public API ofOmex,ManifestandManifestEntryis unchanged. -
pymetadata is therefore no longer pure python.
pymetadata-coreis a Rust extension and ships one abi3 wheel per platform, which every CPython from 3.11 on installs:manylinux2014_x86_64andmanylinux2014_aarch64(linux with glibc),macosx_10_12_x86_64,macosx_11_0_arm64andwin_amd64. On every other platform, e.g. musl based linux (Alpine), linux on armv7, ppc64le or s390x, FreeBSD, 32 bit or ARM Windows, or an implementation other than CPython such as PyPy,pip install pymetadatafalls back to the source distribution of the core and compiles it, which needs a Rust toolchain, i.e.cargo; without one the build fails, whilepymetadata-coreis built rather than while pymetadata is installed. See Installation. -
Reading is lazy.
Omex.from_omexno longer extracts the archive into a temporary directory,Omex.get_pathextracts a member when something asks for it. Opening still reads the central directory and themanifest.xml, and reads the first bytes of an unlisted.xmlmember to classify its format, without inflating the rest of it. -
A member which nobody touched is copied compressed when the archive is written, without being inflated and compressed again. Its compression method, compressed size and CRC are preserved exactly.
-
The effect, in ms: reading with
Omex.from_omex, writing withOmex.to_omexwithout a change. Median of 15 runs after one uncounted warm-up run. Both columns were taken withscripts/omex_timing.py --runs 15, one after the other on the same machine, the before column on a worktree of the previous release (c42df44), so the two are comparable and you can reproduce them:archive size (KiB) entries read before read after write before write after BIOMD0000000001.omex 277 16 2.50 0.24 11.56 0.23 CombineArchiveShowCase.omex 1073 22 6.08 0.74 44.37 0.44 CompModels.omex 6 6 0.72 0.10 0.48 0.10 iCGB21FR.omex 1375 23 18.24 1.07 226.68 0.49 -
Reading reads the whole file: the
bytesof the archive and the container the core keeps from them. Writing streams into the file, see the entry onOmex.to_omexbelow. -
Omex.to_directorywrites each member once, straight to its destination. An entry whose file is missing raises aFileNotFoundErrorwhich names that file and sets itsfilename, on every platform: on windowsshutil.copy2, which wrote every member before, copies through theCopyFile2API, whose error names no file at all ([WinError 2] The system cannot find the file specified). -
One
Omexcan still be used from several threads. The core gives access to one archive to one thread at a time and refuses a second one, so the calls which reach it,Omex.get_path,Omex.to_omexandOmex.to_directory, are serialized with a lock of the instance. Extracting the members of one archive from a thread pool therefore keeps working, as it did whenfrom_omexextracted everything up front, but the extractions happen one after the other: threads do not make reading one archive faster. TwoOmexinstances are independent of each other. AnArchiveof the core which is shared outside ofOmexraisespymetadata.omex.ConcurrentUseError, aRuntimeError, instead of theArchiveBusyErrorof the binding. Changing themanifestof oneOmexfrom several threads at the same time is not supported.
Behaviour which changed
- A wrong or missing password raises a
ValueErrorwherezipfileraised aRuntimeError. The exception isPasswordError, whichpymetadata.omexexports besideConcurrentUseError: code which catchesValueErrorkeeps working, and code which tells a protected archive from a container which is no archive catchesPasswordError. - A member with an unsupported compression method, or one which inflates beyond the 1 GiB cap of the core, is refused when its bytes are read, i.e., at
Omex.get_path, and no longer when the archive is opened. A member with an unsupported method which the manifest does not list gets its format guessed from its suffix alone; one over the inflate cap is classified from its first bytes, which are read without inflating the rest. - A member whose name points outside of the archive, or is absolute, is refused when the archive is opened with a
ValueError.zipfilesanitized such a name and read the member, this is deliberate hardening.Omex.is_omexstill answersTruefor such a file, because it asks about the structure of the archive. - The
manifest.xmlof a written archive is serialized by the core: there is nomaster="false"attribute and the XML declaration is single quoted. The content is the same, the bytes are not. - A member which pymetadata writes anew, e.g., an added file or the
manifest.xml, carries the zip epoch 1980-01-01 00:00:00 as its modification time, where the previous writer used the time of the write. A member which is copied keeps the date it had in the source archive. The upside is that a written archive is reproducible: identical content gives identical bytes. The core offers no way to set a timestamp in 0.4.0. - A member which is copied is not inflated, so a member with a compression method the core cannot inflate, such as bzip2, survives a write unchanged (it stays bzip2), although its bytes are refused when read. Such a member makes a write at a non-default
compresslevel, or with acompressionother thanZIP_DEFLATED, fail with aValueError, because those writes inflate every member. - A write applies the removals of the manifest to the archive the
Omexwas read from, soOmex.to_omexis not free of side effects. An entry which was removed frommanifestloses its content in thatOmexwhen the archive is written: declaring the location again afterwards and writing a second time fails with aFileNotFoundError, where the previous writer wrote the member again, because it still had every member as a file in the temporary directory. A write which repeats the same entries writes the same bytes again, and a removal before the first write is unaffected. Omex.is_omexis answered by the core, which reads the end of the file and its central directory and no member at all. The names of the container are normalized before the manifest is looked for, so a container whose manifest member is stored as./manifest.xmlinstead ofmanifest.xmlis an archive. A file which is no COMBINE archive is logged with one warning naming the missing manifest, where the previous release logged a different one for a file which is no zip.from_omexreads such a container, where the previous release refused it, becausefrom_omexwas gated onis_omex.- A location which a manifest declares twice is written once, the last entry winning, instead of producing two members of the same name.
Omex.to_omexwrites the archive through the core into a temporary file next to the target and moves it over the target, so the archive is never held in memory as a whole and the target is either the new archive or untouched. Reading still holds the container: for a 268.5 MB archive the peak of a read and write is 553.5 MB, where 0.8.3 peaked at 37.3 MB because it extracted to a temporary directory and streamed the members from there. A write which cannot be carried out, e.g. a full disk, raises anOSErrorand leaves the target alone.compression=zipfile.ZIP_STOREDis written as deflate at level 0, with a warning, because the core has no stored path. Any othercompressionthanZIP_DEFLATEDalso deflates every member again, which gives up the copy of untouched members and its speed.compresslevelkeeps its meaning, and-1is accepted as the default level (9), as withzipfile. Acompressleveloutside of 0 to 9 raises aValueErroras before, with a different message.- A member which the core deflates, e.g., an added file, the
manifest.xml, or any member of a write at a non-defaultcompresslevel, has different bytes than before, because the deflate of the core is not the one of CPython'szlib. The content is identical. A member whose format ormasterflag changed is not one of them: those live in the manifest, so the member is copied as the blob it is. - When an archive is read with
from_omex, one warning moved to the loggerpymetadata._core_bridgefrompymetadata.omex:Entry with location missing in manifest.xml, for a member the manifest does not list (from_directorystill logs it underpymetadata.omex). Two are new, both from that logger: a manifest entry which no member of the archive provides is warned about, where it used to be dropped silently (it is still dropped), and an unlisted.xmlmember whose bytes cannot be read gets its format guessed from its suffix with a warning. The warning about a member which is neither stored nor deflated stays inpymetadata.omexand names the member as the core normalizes it, e.g../b.txtinstead ofb.txt. - The message for a file which is not a COMBINE archive changed to
Not an OMEX archive, no readable manifest.xml: malformed ...: '<path>'., still aValueError. Code which matches on the old text needs to be adapted.
Your pymetadata team