<commit_before>#! /usr/bin/python
from django.core.exceptions import ValidationError
from django.core.management.base import BaseCommand
from django.conf import settings
from sys import stderr
from settings import NONDELEGATED_NS, SECONDARY_ZONES

from cyder.base.eav.models import Attribute
from cyder.core.system.models import System, SystemAV

from cyder.core.ctnr.models import Ctnr
from cyder.cydhcp.interface.static_intr.models import StaticInterface
from cyder.cydhcp.workgroup.models import Workgroup
from cyder.cydns.address_record.models import AddressRecord
from cyder.cydns.cname.models import CNAME
from cyder.cydns.domain.models import Domain
from cyder.cydns.mx.models import MX
from cyder.cydns.nameserver.models import Nameserver
from cyder.cydns.ptr.models import PTR
from cyder.cydns.soa.models import SOA
from cyder.cydns.utils import ensure_domain
from cyder.cydns.models import View

import MySQLdb
from optparse import make_option
from datetime import datetime
from lib import maintain_dump, fix_maintain
from lib.utilities import (clean_mac, ip2long, long2ip, fix_attr_name,
                           range_usage_get_create, get_label_domain_workaround,
                           ensure_domain_workaround)

public, _ = View.objects.get_or_create(name="public")
private, _ = View.objects.get_or_create(name="private")

BAD_DNAMES = ['', '.', '_']
connection = MySQLdb.connect(host=settings.MIGRATION_HOST,
                             user=settings.MIGRATION_USER,
                             passwd=settings.MIGRATION_PASSWD,
                             db=settings.MIGRATION_DB,
                             charset='utf8')
cursor = connection.cursor()


def get_delegated():
    global delegated_dnames
    if delegated_dnames is None:
        print 'Fetching delegated domain names...'

        sql = ("SELECT domain.name FROM maintain_sb.domain "
               "INNER JOIN maintain_sb.nameserver "
               "ON domain.id=nameserver.domain "
               "WHERE %s")
        where = ' and '.join(["nameserver.name != '%s'" % ns
                              for ns in NONDELEGATED_NS])
        cursor.execute(sql % where)
        results = [i for (i,) in cursor.fetchall()]

        delegated_dnames = set(results)
    return delegated_dnames


delegated_dnames = None


class Zone(object):

    def __init__(self, domain_id=None, dname=None, soa=None,
                 gen_recs=True, secondary=False):
        self.domain_id = domain_id
        self.dname = self.get_dname() if dname is None else dname
        self.dname = self.dname.lower()
        self.domain = None

        if gen_recs:
            try:
                self.domain = Domain.objects.get(name=self.dname)
            except Domain.DoesNotExist:
                print "WARNING: Domain %s does not exist." % self.dname
                return

            if self.dname in SECONDARY_ZONES or secondary:
                print ("WARNING: Domain %s is a secondary, so its records "
                       "will not be migrated." % self.dname)
                secondary = True
            else:
                if self.dname in get_delegated():
                    self.domain.soa = self.gen_SOA() or soa
                    if not self.domain.soa:
                        print ("WARNING: Could not migrate domain %s; no SOA"
                               % self.domain.name)
                        self.domain.delete()
                        return
                    else:
                        self.domain.delegated = True
                        print "%s has been marked as delegated." % self.dname
                        self.domain.save()

                elif self.domain_id is not None:
                    # XXX: if SOA is created before AR and NS, then
                    # creating glue will raise an error. However,
                    # when creating delegated domains, an SOA is needed
                    self.gen_MX()
                    self.gen_static()
                    self.gen_AR()
                    self.gen_NS()
                    self.domain.soa = self.gen_SOA() or soa

        else:
            self.domain = self.gen_domain()
            if not self.domain:
                return
            self.domain.save()

        if self.domain:
            master_domain = self.domain.master_domain
            if master_domain and master_domain.delegated:
                raise Exception("Whoa dude %s has a delegated master"
                                % self.domain.name)

        if self.domain and self.domain_id is not None:
            self.walk_zone(gen_recs=gen_recs, secondary=secondary)

    def gen_SOA(self):<commit_msg>migrate delegated nameservers<commit_after>#! /usr/bin/python
from django.core.exceptions import ValidationError
from django.core.management.base import BaseCommand
from django.conf import settings
from sys import stderr
from settings import NONDELEGATED_NS, SECONDARY_ZONES

from cyder.base.eav.models import Attribute
from cyder.core.system.models import System, SystemAV

from cyder.core.ctnr.models import Ctnr
from cyder.cydhcp.interface.static_intr.models import StaticInterface
from cyder.cydhcp.workgroup.models import Workgroup
from cyder.cydns.address_record.models import AddressRecord
from cyder.cydns.cname.models import CNAME
from cyder.cydns.domain.models import Domain
from cyder.cydns.mx.models import MX
from cyder.cydns.nameserver.models import Nameserver
from cyder.cydns.ptr.models import PTR
from cyder.cydns.soa.models import SOA
from cyder.cydns.utils import ensure_domain
from cyder.cydns.models import View

import MySQLdb
from optparse import make_option
from datetime import datetime
from lib import maintain_dump, fix_maintain
from lib.utilities import (clean_mac, ip2long, long2ip, fix_attr_name,
                           range_usage_get_create, get_label_domain_workaround,
                           ensure_domain_workaround)

public, _ = View.objects.get_or_create(name="public")
private, _ = View.objects.get_or_create(name="private")

BAD_DNAMES = ['', '.', '_']
connection = MySQLdb.connect(host=settings.MIGRATION_HOST,
                             user=settings.MIGRATION_USER,
                             passwd=settings.MIGRATION_PASSWD,
                             db=settings.MIGRATION_DB,
                             charset='utf8')
cursor = connection.cursor()


def get_delegated():
    global delegated_dnames
    if delegated_dnames is None:
        print 'Fetching delegated domain names...'

        sql = ("SELECT domain.name FROM maintain_sb.domain "
               "INNER JOIN maintain_sb.nameserver "
               "ON domain.id=nameserver.domain "
               "WHERE %s")
        where = ' and '.join(["nameserver.name != '%s'" % ns
                              for ns in NONDELEGATED_NS])
        cursor.execute(sql % where)
        results = [i for (i,) in cursor.fetchall()]

        delegated_dnames = set(results)
    return delegated_dnames


delegated_dnames = None


class Zone(object):

    def __init__(self, domain_id=None, dname=None, soa=None,
                 gen_recs=True, secondary=False):
        self.domain_id = domain_id
        self.dname = self.get_dname() if dname is None else dname
        self.dname = self.dname.lower()
        self.domain = None

        if gen_recs:
            try:
                self.domain = Domain.objects.get(name=self.dname)
            except Domain.DoesNotExist:
                print "WARNING: Domain %s does not exist." % self.dname
                return

            if self.dname in SECONDARY_ZONES or secondary:
                print ("WARNING: Domain %s is a secondary, so its records "
                       "will not be migrated." % self.dname)
                secondary = True
            else:
                if self.dname in get_delegated():
                    self.domain.soa = self.gen_SOA() or soa
                    if not self.domain.soa:
                        print ("WARNING: Could not migrate domain %s; no SOA"
                               % self.domain.name)
                        self.domain.delete()
                        return
                    else:
                        self.domain.delegated = True
                        print "%s has been marked as delegated." % self.dname
                        self.domain.save()

                if self.domain_id is not None:
                    # XXX: if SOA is created before AR and NS, then
                    # creating glue will raise an error. However,
                    # when creating delegated domains, an SOA is needed
                    if self.dname not in get_delegated():
                        self.gen_MX()
                        self.gen_static()
                        self.gen_AR()
                    self.gen_NS()
                    if self.dname not in get_delegated():
                        self.domain.soa = self.gen_SOA() or soa

        else:
            self.domain = self.gen_domain()
            if not self.domain:
                return
            self.domain.save()

        if self.domain:
            master_domain = self.domain.master_domain
            if master_domain and master_domain.delegated:
                raise Exception("Whoa dude %s has a delegated master"
                                % self.domain.name)

        if self.domain and self.domain_id is not None:
            self.walk_zone(gen_recs=gen_recs, secondary=secondary)

    def gen_SOA(self):
{'fraction_non_alphanumeric': 0.07559486207622658, 'fraction_numerical': 0.0006317119393556538, 'mean_word_length': 1.8380639378547954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7235916', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2579, 'n_words': 687}