def ensure_domain(name, purgeable=False, inherit_soa=False, force=False):
    try:
        domain = Domain.objects.get(name=name)
        return domain
    except ObjectDoesNotExist:
        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
The total number number of commands in the history. Identical to
dataset/ETHPy150Open mozilla/inventory/mozdns/utils.py/ensure_domain--crossover with--dataset/ETHPy150Open AppScale/appscale/AppServer/lib/grizzled/grizzled/history.py/History.total