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 more information about this
    function"""
    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 = ''
    for i in range(len(parts)):
        domain_name = parts[i] + '.' + domain_name
        domain_name = domain_name.strip('.')
        clobber_objects = get_clobbered(domain_name)
        # need to be deleted and then recreated
        domain, created = Domain.objects.get_or_create(name=domain_name)
        if purgeable and created:
            domain.purgeable = True
            domain.save()

        if (inherit_soa and created and domain.master_domain and
                domain.master_domain.soa is not None):
            domain.soa = domain.master_domain.soa
            domain.save()
        for object_, views in clobber_objects:
            try:
                object_.domain = domain
                object_.clean()
                object_.save()
                for view_name in views:
                    view = View.objects.get(name=view_name)
                    object_.views.add(view)
                    object_.save()
            except ValidationError:
                # this is bad
                pdb.set_trace()
                pass
    return domain
dataset/ETHPy150Open rtucker-mozilla/mozilla_inventory/mozdns/utils.py/ensure_domain