# glance.common.utils module

System-level utilities and helper functions.

class glance.common.utils.CooperativeReader(fd)

  Bases: `object`

  A reader for image data that yields cooperatively on each read/iteration.

  When accessing data through the iterator or the read method, a short sleep is used so other concurrent uploads or downloads can make progress instead of one stream monopolizing the worker.

  read(length=None)

    Return the requested amount of bytes, fetching the next chunk of the underlying iterator when needed.

    This is replaced with cooperative_read in __init__ if the underlying fd already supports read().

class glance.common.utils.LimitingReader(data, limit, exception_class=<class 'glance.common.exception.ImageSizeLimitExceeded'>)

  Bases: `object`

  Reader designed to fail when reading image data past the configured allowable amount.

  read(i)

glance.common.utils.chunkiter(fp, chunk_size=65536)

  Return an iterator to a file-like obj which yields fixed size chunks

  Parameters:

    - fp – a file-like object

    - chunk_size – maximum size of chunk

glance.common.utils.chunkreadable(iter, chunk_size=65536)

  Wrap a readable iterator with a reader yielding chunks of a preferred size, otherwise leave iterator unchanged.

  Parameters:

    - iter – an iter which may also be readable

    - chunk_size – maximum size of chunk

glance.common.utils.cooperative_iter(iter)

  Return an iterator which yields briefly after each iteration so other concurrent work can run (cooperative scheduling).

  Parameters:

    iter – an iterator to wrap

glance.common.utils.cooperative_read(fd)

  Wrap a file descriptor’s read with a partial function which yields briefly after each read so other concurrent work can run.

  Parameters:

    fd – a file descriptor to wrap

glance.common.utils.create_mashup_dict(image_meta)

  Returns a dictionary-like mashup of the image core properties and the image custom properties from given image metadata.

  Parameters:

    image_meta – metadata of image with core and custom properties

glance.common.utils.evaluate_filter_op(value, operator, threshold)

  Evaluate a comparison operator. Designed for use on a comparative-filtering query field.

  Parameters:

    - value – evaluated against the operator, as left side of expression

    - operator – any supported filter operation

    - threshold – to compare value against, as right side of expression

  Raises:

    InvalidFilterOperatorValue – if an unknown operator is provided

  Returns:

    boolean result of applied comparison

glance.common.utils.get_image_meta_from_headers(response)

  Processes HTTP headers from a supplied response that match the x-image-meta and x-image-meta-property and returns a mapping of image metadata and properties

  Parameters:

    response – Response to process

glance.common.utils.get_stores_from_request(req, body)

  Processes a supplied request and extract stores from it

  Parameters:

    - req – request to process

    - body – request body

  Raises:

    glance_store.UnknownScheme – if a store is not valid

  Returns:

    a list of stores

glance.common.utils.get_test_suite_socket()

glance.common.utils.image_meta_to_http_headers(image_meta)

  Returns a set of image metadata into a dict of HTTP headers that can be fed to either a Webob Request object or an httplib.HTTP(S)Connection object

  Parameters:

    image_meta – Mapping of image metadata

glance.common.utils.is_db_lock_error(exc)

  Return True if exc is a sqlite database-is-locked error.

  Repo operations go through SQLAlchemy, which wraps the underlying sqlite3.OperationalError in sqlalchemy.exc.OperationalError.

glance.common.utils.is_http_store_configured(url)

glance.common.utils.is_valid_fqdn(fqdn)

  Verify whether a host is a valid FQDN.

glance.common.utils.is_valid_hostname(hostname)

  Verify whether a hostname (not an FQDN) is valid.

glance.common.utils.mutating(func)

  Decorator to enforce read-only logic

glance.common.utils.no_4byte_params(f)

  Checks that no 4 byte unicode characters are allowed in dicts’ keys/values and string’s parameters

glance.common.utils.normalize_hostname(host)

  Normalize IP address to standard format or return hostname.

  Uses ipaddress module to validate and normalize IP addresses, rejecting encoded formats. For hostnames, requires DNS resolution to ensure they are valid and not encoded IP attempts.

  Parameters:

    host – hostname or IP address

  Returns:

    normalized IP address, hostname unchanged, or None

glance.common.utils.parse_valid_host_port(host_port)

  Given a “host:port” string, attempts to parse it as intelligently as possible to determine if it is valid. This includes IPv6 [host]:port form, IPv4 ip:port form, and hostname:port or fqdn:port form.

  Invalid inputs will raise a ValueError, while valid inputs will return a (host, port) tuple where the port will always be of type int.

glance.common.utils.retry_on_db_lock(func, timeout=30.0, interval=0.05)

  Retry a DB operation that may fail with sqlite lock errors.

glance.common.utils.safe_mkdirs(path)

glance.common.utils.setup_remote_pydev_debug(host, port)

glance.common.utils.sort_image_locations(locations)

glance.common.utils.split_filter_op(expression)

  Split operator from threshold in an expression. Designed for use on a comparative-filtering query field. When no operator is found, default to an equality comparison.

  Parameters:

    expression – the expression to parse

  Returns:

    a tuple (operator, threshold) parsed from expression

glance.common.utils.split_filter_value_for_quotes(value)

  Split filter values

  Split values by commas and quotes for ‘in’ operator, according api-wg.

glance.common.utils.validate_import_uri(uri)

  Validate requested uri for Image Import web-download.

  Parameters:

    uri – target uri to be validated

glance.common.utils.validate_quotes(value)

  Validate filter values

  Validation opening/closing quotes in the expression.

glance.common.utils.wrap_data_for_store_upload(data, size_cap=None)

  Wrap an image data iterator for backend store upload.

this page last updated: 2026-08-19 03:42:39

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.

found an error? report a bug