genvid.toolbox.PackageArchiver

class genvid.toolbox.PackageArchiver

Bases: object

This class is grouping the functions for creating a package suitable to be used as an artifact in Nomad.

Example of usage:

import pathlib

dest = pathlib.Path("test.zip")
source = pathlib.Path("source")
packager = PackageArchiver()
path2name = packager.relative_path(source)
packager.make_archive(dest, source.rglob("*.*"), path2name)

New in version 1.30.0.

PATH_TO_ARCHIVE_NAME_CALLABLE

The function type for transforming pathlib.Path to archive name.

alias of Callable[[Path], str]

ARCHIVE_NAME_TEMPLATE = '{name}_{version}_{checksum}{archfmt}'

Template used for the filename in update_archive().

static relative_path(relpath: Path) Callable[[Path], str]

Create a callable that transforms every path name to its relative form under relpath.

Parameters

relpath – the base path to which other paths are relative.

Returns

A PATH_TO_ARCHIVE_NAME_CALLABLE.

static flatten_path(filepath: Path) str

A PATH_TO_ARCHIVE_NAME_CALLABLE that returns the base name of filepath.

Parameters

filepath – The Path to transform.

Returns

the name of the file.

static make_archive(archive: Union[Path, ZipFile, str], files: Iterable[Path], path_to_name: Callable[[Path], str]) Path

This method creates a zip archive in destination, and adds each file files to it, with the name returned by path_to_name.

Parameters
  • archive – A Path or a ZipFile archive. If a Path or a string is passed, a ZipFile with this name will be created in overwrite (w) mode, and the compression method ZIP_DEFLATED.

  • files – An iterable list of files to add to the archive. The files must be reachable in the context of execution.

  • path_to_name – An function that can transform the archive name to a path name. The methods relative_path() and flatten_path() are provided as convenience.

Returns

A Path object of the final archive.

static stable_checksum(archive: Path) str

Returns a checksum from a Zip archive based only on the filenames and their content.

Parameters

archive – Path of the zipfile to do the checksum on.

static standard_checksum(archive: Path) str

Returns the checksum of the file passed in argument.

Note

It is recommended to use stable_checksum() for zip archive. Although slower, the checksum is independant of when the file was created.

Parameters

archive – the file to count the checksum of.

classmethod update_archive(archive: Union[Path, str], version: str, checksum: str, *, name: Optional[str] = None, destdir: Optional[Union[Path, str]] = None, clean: bool = True, keep: bool = False, logger: Optional[Logger] = None)

Update a binary archive to conform to the image format expected.

Parameters
  • archive – The path to the archive file.

  • version – Version of the archive.

  • checksum – The checksum to use for the archive. standard_checksum() and stable_checksum() are two methods that return a good value.

  • name – The name to use for the new archive. If None, the stem is used instead.

  • destdir – Destination directory. Defaults to the same as the original archive.

  • clean – Clean old versions of the archive (the ones with the same name).

  • keep – Keep the original archives. You should almost always use it with a different destdir.

  • logger – The logger to use. If None, a default logger will be created.

New in version 1.30.0.

class package_archive.PackageArchiver

Implementation of genvid.toolbox.PackageArchiver