portage.gpkg module¶
- class portage.gpkg.checksum_helper(settings, gpg_operation=None, detached=True, signature=None)¶
Bases:
object
Do checksum generation and GPG Signature generation and verification
- SIGNING = 0¶
- VERIFY = 1¶
- _check_gpg_status(gpg_status)¶
Check GPG status log for extra info. GPG will return OK even if the signature owner is not trusted.
- _drop_privileges()¶
- finish()¶
Tell GPG file is EOF, and get results, then do clean up.
- update(data)¶
Write data to hash libs and GPG stdin.
- class portage.gpkg.gpkg(settings, base_name=None, gpkg_file=None)¶
Bases:
object
Gentoo binary package https://www.gentoo.org/glep/glep-0078.html
- _add_manifest(container)¶
Add Manifest to the container based on current checksums. Creare GPG signatue if needed.
- _add_metadata(container, metadata, compression_cmd)¶
add metadata to container
- _add_signature(checksum_info, tarinfo, container, manifest=True)¶
Add GPG signature for the given tarinfo file. manifest: add to manifest
- _check_pre_image_files(root_dir, image_prefix='image')¶
Check the pre image files size and path, return the longest path length, largest single file size, and total files size.
- _check_pre_quickpkg_files(contents, root, image_prefix='image')¶
Check the pre quickpkg files size and path, return the longest path length, largest single file size, and total files size.
- _create_tarinfo(file_name)¶
Create new tarinfo for the new file
- _extract_filename_compression(file_name)¶
Extract the file basename and compression method
- _generate_metadata_from_dir(metadata_dir)¶
read all files in metadata_dir and return as dict
- _get_binary_cmd(compression, mode)¶
get command list form portage and try match compressor
- _get_compression_cmd(compression=None)¶
return compression command for Popen
- _get_decompression_cmd(compression=None)¶
return decompression command for Popen
- _get_inner_tarinfo(tar, file_name)¶
Get inner tarinfo from given container. Will try get file_name from correct basename first, if it fail, try any file that have same name as file_name, and return the first one.
- _get_tar_format(fileobj)¶
Try to detect tar version
- _get_tar_format_from_stats(image_max_prefix_length, image_max_name_length, image_max_linkname_length, image_max_file_size, image_total_size)¶
Choose the corresponding tar format according to the image information
- _load_manifest(manifest_string)¶
Check, load, and return manifest in a list by files
- _quickpkg(contents, metadata, root_dir, protect=None)¶
Similar to compress, but for quickpkg. Will compress the given files to image with root, ignoring all other files.
- _record_checksum(checksum_info, tarinfo)¶
Record checksum result for the given file. Replace old checksum if already exists.
- _verify_binpkg(metadata_only=False)¶
Verify current GPKG file.
- compress(root_dir, metadata, clean=False)¶
Use initialized configuation create new gpkg file from root_dir. Will overwrite any exists file. metadata is a dict, the key will be file name, the value will be the file contents.
- decompress(decompress_dir)¶
decompress current gpkg to decompress_dir
- get_metadata(want=None)¶
get package metadata. if want is list, return all want key-values in dict if want is str, return the want key value
- get_metadata_url(url, want=None)¶
Return the requested metadata from url gpkg. Default return all meta data. Use ‘want’ to get specific name from metadata. This method only support the correct package format. Wrong files order or incorrect basename will be considered invalid to reduce potential attacks. Only signature will be check if the signature file is the next file. Manifest will be ignored since it will be at the end of package.
- unpack_metadata(dest_dir=None)¶
Unpack metadata to dest_dir. If dest_dir is None, return files and values in dict. The dict key will be UTF-8, not bytes.
- update_metadata(metadata, newcpv=None)¶
Update metadata in the gpkg file.
- class portage.gpkg.tar_safe_extract(tar: tarfile.TarFile, prefix: str = '')¶
Bases:
object
A safer version of tar extractall that doing sanity check. Note that this does not solve all security problems.
- extractall(dest_dir: str)¶
Extract all files to a temporary directory in the dest_dir, and move them to the dest_dir after sanity check.
- class portage.gpkg.tar_stream_reader(fileobj, cmd=None, uid=None, gid=None)¶
Bases:
object
helper function that return a file-like object for read a file inside of a tar container.
This helper allowed transparently streaming read a compressed file in tar.
With optional call and pipe compressed data through external program, and return the uncompressed data.
- reader = tar_stream_reader(
fileobj, # the fileobj from tarfile.extractfile(f) [“gzip”, “-d”], # decompression command
)
reader.read() reader.close()
- _drop_privileges()¶
- _write_thread()¶
writing thread to avoid full buffer blocking
- close()¶
wait external program complete and do clean up
- kill()¶
kill external program if any error happened in python
- read(bufsize=- 1)¶
return decompressor stdout data
- class portage.gpkg.tar_stream_writer(tarinfo, container, tar_format, cmd=None, checksum_helper=None, uid=None, gid=None)¶
Bases:
object
One-pass helper function that return a file-like object for create a file inside of a tar container.
This helper allowed streaming add a new file to tar without prior knows the file size.
With optional call and pipe data through external program, the helper can transparently save compressed data.
With optional checksum helper, this helper can create corresponding checksum and GPG signature.
Example:
- writer = tar_stream_writer(
file_tarinfo, # the file tarinfo that need to be added container, # the outer container tarfile object tarfile.USTAR_FORMAT, # the outer container format [“gzip”], # compression command checksum_helper # checksum helper
)
writer.write(data) writer.close()
- _cmd_read_thread()¶
Use thread to avoid block. Read stdout from external compressor, then write to the file in container, and to checksum helper if needed.
- _drop_privileges()¶
- close()¶
Update the new file tar header when close
- kill()¶
kill external program if any error happened in python
- write(data)¶
Write data to tarfile or external compressor stdin