oras.utils package

Submodules

oras.utils.fileio module

class oras.utils.fileio. PathAndOptionalContent ( path : str , content : str | None = None ) [source]

Bases: object

Class for holding a path reference and optional content parsed from a string.

oras.utils.fileio. copyfile ( source : str , destination : str , force : bool = True ) str [source]

Copy a file from a source to its destination.

Parameters :
  • source ( str ) – the source to copy from

  • destination ( str ) – the destination to copy to

  • force ( bool ) – force copy if destination already exists

oras.utils.fileio. extract_targz ( targz : str , outdir : str , numeric_owner : bool = False ) [source]

Extract a .tar.gz to an output directory.

oras.utils.fileio. get_file_hash ( path : str , algorithm : str = 'sha256' ) str [source]

Return an sha256 hash of the file based on an algorithm Raises AttributeError if incorrect algorithm supplied.

Parameters :
  • path ( str ) – the path to get the size for

  • algorithm ( str ) – the algorithm to use

oras.utils.fileio. get_size ( path : str ) int [source]

Get the size of a blob

:param path : the path to get the size for :type path: str

oras.utils.fileio. get_tmpdir ( tmpdir : str | None = None , prefix : str | None = '' , create : bool = True ) str [source]

Get a temporary directory for an operation.

Parameters :
  • tmpdir ( str ) – an optional temporary directory

  • prefix ( str ) – an optional prefix for the temporary path

  • create ( bool ) – create the directory

oras.utils.fileio. get_tmpfile ( tmpdir : str | None = None , prefix : str = '' , suffix : str = '' ) str [source]

Get a temporary file with an optional prefix.

:param tmpdir : an optional temporary directory :type tmpdir: str :param prefix: an optional prefix for the temporary path :type prefix: str :param suffix: an optional suffix (extension) :type suffix: str

oras.utils.fileio. is_within_directory ( directory : str , target : str ) bool [source]

Determine whether a file is within a directory

oras.utils.fileio. make_targz ( source_dir : str , dest_name : str | None = None ) str [source]

Make a targz (compressed) archive from a source directory.

oras.utils.fileio. mkdir_p ( path : str ) [source]

Make a directory path if it does not exist, akin to mkdir -p

:param path : the path to create :type path: str

oras.utils.fileio. print_json ( json_obj : dict ) str [source]

Pretty print json.

Parameters :

json_obj ( dict ) – json object to print

oras.utils.fileio. read_file ( filename : str , mode : str = 'r' ) str [source]

Read a file.

Parameters :
  • filename ( str ) – filename to read

  • mode ( str ) – mode to read

oras.utils.fileio. read_in_chunks ( image : TextIO | BufferedReader , chunk_size : int = 1024 ) [source]

Helper function to read file in chunks, with default size 1k.

Parameters :
  • image ( TextIO or io.BufferedReader ) – file descriptor

  • chunk_size ( int ) – size of the chunk

oras.utils.fileio. read_json ( filename : str , mode : str = 'r' ) dict [source]

Read a json file to a dictionary.

Parameters :
  • filename ( str ) – filename to read

  • mode ( str ) – mode to read

oras.utils.fileio. readline ( ) str [source]

Read lines from stdin

oras.utils.fileio. recursive_find ( base : str , pattern : str | None = None ) Generator [source]

Find filenames that match a particular pattern, and yield them.

:param base : the root to search :type base: str :param pattern: an optional file pattern to use with fnmatch :type pattern: str

oras.utils.fileio. sanitize_path ( expected_dir , path ) [source]

Ensure a path resolves to be in the expected parent directory.

It can be directly there or a child, but not outside it. We raise an error if it does not - this should not happen

oras.utils.fileio. split_path_and_content ( ref : str ) PathAndOptionalContent [source]

Parse a string containing a path and an optional content

Examples

<path>:<content-type> path/to/config:application/vnd.oci.image.config.v1+json /dev/null:application/vnd.oci.image.config.v1+json C:myconfig:application/vnd.oci.image.config.v1+json

Or, <path> /dev/null C:myconfig

param ref :

the manifest reference to parse (examples above)

type ref :

str

: return: A Tuple of the path in the reference, and the content-type if one found,

otherwise None.

oras.utils.fileio. workdir ( dirname ) [source]

Provide context for a working directory, e.g.,

with workdir(name):

# do stuff

oras.utils.fileio. write_file ( filename : str , content : str , mode : str = 'w' , make_exec : bool = False ) str [source]

Write content to a filename

Parameters :
  • filename ( str ) – filname to write

  • content ( str ) – content to write

  • mode ( str ) – mode to write

  • make_exec ( bool ) – make executable

oras.utils.fileio. write_json ( json_obj : dict , filename : str , mode : str = 'w' ) str [source]

Write json to a filename

Parameters :
  • json_obj ( dict ) – json object to write

  • filename ( str ) – filename to write

  • mode ( str ) – mode to write

oras.utils.request module

oras.utils.request. append_url_params ( url : str , params : dict ) str [source]

Given a dictionary of params and a url, parse the url and add extra params.

Parameters :
  • url ( str ) – the url string to parse

  • params ( dict ) – parameters to add

oras.utils.request. find_docker_config ( exists : bool = True ) [source]

Return the docker default config path.

oras.utils.request. get_docker_client ( tls_verify : bool = True , ** kwargs ) [source]

Get a docker client.

:param tls_verify : enable tls :type tls_verify: bool

oras.utils.request. iter_localhosts ( name : str ) [source]

Given a url with localhost, always resolve to 127.0.0.1.

:param name : the name of the original host string :type name: str

Module contents