def ensure_domain(name, purgeable=False, inherit_soa=False, force=False):
    """This function will take ``domain_name`` and make sure that a domain with
    that name exists. If this function creates a domain it will set the
    domain's purgeable flag to the value of the named arguement ``purgeable``.
    See the doc page about Labels and Domains for other information about this
    function.

    This function will attempt to prevent you from:
        * creating a new TLD
        * creating a domain that would be a child of a delegated domain
        * creating a domain that wouldn't exist in an existing zone (it
            wouldn't have an SOA)

    You can override these constrains by setting the ``force`` named argument
    to ``True``.

    By default if a domain's parent has an SOA, the new domain will not inherit
    that SOA. You can cause the domain to inherit the SOA by passing the named
    argument ``inherit_soa`` as ``True``. For example, the function
    :func:`ensure_label_domain` passes ``inherit_soa=True``.

    If a domain already exists with a name equal to ``name`` it is immediately
    returned. It is up to the caller to ensure that ``purgeable`` is set if it
    is needed.
    """
    try:
        domain = Domain.objects.get(name=name)
        return domain
    except __HOLE__:
        pass

    # Looks like we are creating some domains. Make sure the first domain we
    # create is under a domain that has a non-None SOA reference.
    parts = list(reversed(name.split('.')))

    if not force:
        domain_name = ''
        leaf_domain = None
        # Find the leaf domain.
        for i in range(len(parts)):
            domain_name = parts[i] + '.' + domain_name
            domain_name = domain_name.strip('.')
            try:
                tmp_domain = Domain.objects.get(name=domain_name)
                # It got here so we know we found a domain.
                leaf_domain = tmp_domain
            except ObjectDoesNotExist:
                continue

        if not leaf_domain:
            raise ValidationError(
                "Creating this record would cause the "
                "creation of a new TLD. Please contact "
                "http://www.icann.org/ for more information.")
        if leaf_domain.delegated:
            raise ValidationError(
                "Creating this record would cause the "
                "creation of a domain that would be a child of a "
                "delegated domain.")
        if not leaf_domain.soa:
            raise ValidationError(
                "Creating this record would cause the "
                "creation of a domain that would not be in an existing "
                "DNS zone.")

    domain_name = ''
    # We know the end domain doesn't exist, so we build it up incrementally
    # starting at the very right most parent domain. If we find that a parent
    # domain already exists we continue until we find the domain that doesn't
    # exist.
    for i in range(len(parts)):
        domain_name = parts[i] + '.' + domain_name
        domain_name = domain_name.strip('.')
        if Domain.objects.filter(name=domain_name).exists():
            continue

        # We are about to create a domain that previously didn't exist!
        clobber_querysets = get_clobbered(domain_name)
        # Don't use get_or_create here because we need to pass certain kwargs
        # to clean()
        domain = Domain(name=domain_name)
        # we know the clobber_querysets may conflict, but we will handle
        # resolving the conflict later in this function

        if purgeable:
            domain.purgeable = True

        domain.clean(ignore_conflicts=True)  # master domain is set here

        if (inherit_soa and domain.master_domain and
                domain.master_domain.soa is not None):
            domain.soa = domain.master_domain.soa

        # get the domain in the db so we can save the clobbered objects
        domain.save(do_full_clean=False)

        # update the objects that initially conflicted with this domain
        for qs in clobber_querysets:
            qs.update(domain=domain, label='')

        # full_clean() wasn't called on this domain but clean() was. Calling
        # clean_feilds() will ensure we have done the equivalent of a
        # full_clean() on the domain.
        domain.clean_fields()

    return domain
dataset/ETHPy150Open mozilla/inventory/mozdns/utils.py/ensure_domain