<commit_before>from django.core.management.base import BaseCommand
from django.conf import settings
from django.contrib.auth.models import User
from django.db import transaction

from cyder.core.ctnr.models import Ctnr, CtnrUser
from cyder.core.system.models import System, SystemKeyValue
from cyder.cydns.domain.models import Domain
from cyder.cydhcp.constants import (ALLOW_ANY, ALLOW_KNOWN, ALLOW_VRF,
                                    ALLOW_LEGACY, ALLOW_LEGACY_AND_VRF)
from cyder.cydhcp.interface.dynamic_intr.models import (DynamicInterface,
                                                        DynamicIntrKeyValue)
from cyder.cydhcp.network.models import Network, NetworkKeyValue
from cyder.cydhcp.range.models import Range, RangeKeyValue
from cyder.cydhcp.site.models import Site
from cyder.cydhcp.vlan.models import Vlan
from cyder.cydhcp.vrf.models import Vrf
from cyder.cydhcp.workgroup.models import Workgroup, WorkgroupKeyValue

import ipaddr
import MySQLdb
from optparse import make_option

from lib.utilities import long2ip


cached = {}
host_option_values = None


allow_all_subnets = [
    '10.192.76.2', '10.192.103.150', '10.192.15.2',
    '10.197.32.0', '10.192.148.32', '10.192.144.32',  '10.192.140.32',
    '10.196.0.32', '10.196.4.32', '10.192.136.63', '10.196.8.8',
    '10.196.16.8', '10.196.24.8', '10.196.32.8', '10.196.40.8',
    '10.162.128.32', '10.162.136.32', '10.162.144.32', '10.198.0.80',
    '10.198.0.140', '10.192.131.9', '10.255.255.255']


class NotInMaintain(Exception):
    """"""


def calc_prefixlen(netmask):
    bits = 0
    while netmask:
        bits += netmask & 1
        netmask >>= 1
    return bits

connection = MySQLdb.connect(host=settings.MIGRATION_HOST,
                             user=settings.MIGRATION_USER,
                             passwd=settings.MIGRATION_PASSWD,
                             db=settings.MIGRATION_DB)

cursor = connection.cursor()


def clean_zone_name(name):
    name = name.replace(' ', '')
    if name[:5] == "zone.":
        name = name[5:]
    return name


def create_subnet(subnet_id, name, subnet, netmask, status, vlan):
    """
    Takes a row from the Maintain subnet table
    returns a new network object and creates the vlan it is associated with
    """
    prefixlen = str(calc_prefixlen(netmask))
    network = str(ipaddr.IPv4Address(subnet & netmask))
    s, _ = Site.objects.get_or_create(name='Campus')
    v = None
    if cursor.execute("SELECT * "
                      "FROM vlan "
                      "WHERE vlan_id = %s" % vlan):
        vlan_id, vlan_name, vlan_number = cursor.fetchone()
        v = Vlan.objects.get(name=vlan_name)
    n, created = Network.objects.get_or_create(
        network_str=network + '/' + prefixlen, ip_type='4',
        site=s, vlan=v)
    cursor.execute("SELECT dhcp_option, value "
                   "FROM object_option "
                   "WHERE object_id = {0} "
                   "AND type = 'subnet'".format(subnet_id))
    results = cursor.fetchall()
    for dhcp_option, value in results:
        cursor.execute("SELECT name, type "
                       "FROM dhcp_options "
                       "WHERE id = {0}".format(dhcp_option))
        name, type = cursor.fetchone()
        kv, _ = NetworkKeyValue.objects.get_or_create(
            value=str(value), key=name, network=n)
    return (n, created)


def create_range(range_id, start, end, range_type, subnet_id, comment, en, known):
    """
    Takes a row form the Maintain range table
    returns a range which is saved in cyder
    """
    # Set the allow statement
    n = None
    r = None
    r_type = 'st' if range_type == 'static' else 'dy'
    allow = ALLOW_LEGACY
    if cursor.execute("SELECT * FROM subnet WHERE id = {0}".format(subnet_id)):
        id, name, subnet, netmask, status, vlan = cursor.fetchone()
        n = Network.objects.get(ip_lower=subnet,
                                prefixlen=str(calc_prefixlen(netmask)))
        n.update_network()

        # The allow options here are a specific order to match the way Maintain
        # generates allow statements. Be careful when modifying this section.

        if '192.168.3.11' == str(ipaddr.IPv4Address(start)):
            allow = ALLOW_LEGACY_AND_VRF
            v, _ = Vrf.objects.get_or_create(name="ip-phones-hack")
            n.vrf = v
            n.save()
        if '172.16.58.3' == str(ipaddr.IPv4Address(start)):
            allow = ALLOW_LEGACY_AND_VRF
            v, _ = Vrf.objects.get_or_create(name="avaya-hack")
            n.vrf = v
            n.save()
        if known:
            allow = ALLOW_KNOWN
        if str(ipaddr.IPv4Address(start)) in allow_all_subnets:
            allow = ALLOW_ANY

        if int(n.network.network) < start < end < int(n.network.broadcast):
            r, created = Range.objects.get_or_create(
                start_lower=start, start_str=ipaddr.IPv4Address(start),
                end_lower=end, end_str=ipaddr.IPv4Address(end),
                range_type=r_type, allow=allow, ip_type='4',
                network=n)
    if not r:
        r, created = Range.objects.get_or_create(
            start_lower=start, start_str=ipaddr.IPv4Address(start),
            end_lower=end, end_str=ipaddr.IPv4Address(end),<commit_msg>Use if...elif... to make logic clearer<commit_after>from django.core.management.base import BaseCommand
from django.conf import settings
from django.contrib.auth.models import User
from django.db import transaction

from cyder.core.ctnr.models import Ctnr, CtnrUser
from cyder.core.system.models import System, SystemKeyValue
from cyder.cydns.domain.models import Domain
from cyder.cydhcp.constants import (ALLOW_ANY, ALLOW_KNOWN, ALLOW_VRF,
                                    ALLOW_LEGACY, ALLOW_LEGACY_AND_VRF)
from cyder.cydhcp.interface.dynamic_intr.models import (DynamicInterface,
                                                        DynamicIntrKeyValue)
from cyder.cydhcp.network.models import Network, NetworkKeyValue
from cyder.cydhcp.range.models import Range, RangeKeyValue
from cyder.cydhcp.site.models import Site
from cyder.cydhcp.vlan.models import Vlan
from cyder.cydhcp.vrf.models import Vrf
from cyder.cydhcp.workgroup.models import Workgroup, WorkgroupKeyValue

import ipaddr
import MySQLdb
from optparse import make_option

from lib.utilities import long2ip


cached = {}
host_option_values = None


allow_all_subnets = [
    '10.192.76.2', '10.192.103.150', '10.192.15.2',
    '10.197.32.0', '10.192.148.32', '10.192.144.32',  '10.192.140.32',
    '10.196.0.32', '10.196.4.32', '10.192.136.63', '10.196.8.8',
    '10.196.16.8', '10.196.24.8', '10.196.32.8', '10.196.40.8',
    '10.162.128.32', '10.162.136.32', '10.162.144.32', '10.198.0.80',
    '10.198.0.140', '10.192.131.9', '10.255.255.255']


class NotInMaintain(Exception):
    """"""


def calc_prefixlen(netmask):
    bits = 0
    while netmask:
        bits += netmask & 1
        netmask >>= 1
    return bits

connection = MySQLdb.connect(host=settings.MIGRATION_HOST,
                             user=settings.MIGRATION_USER,
                             passwd=settings.MIGRATION_PASSWD,
                             db=settings.MIGRATION_DB)

cursor = connection.cursor()


def clean_zone_name(name):
    name = name.replace(' ', '')
    if name[:5] == "zone.":
        name = name[5:]
    return name


def create_subnet(subnet_id, name, subnet, netmask, status, vlan):
    """
    Takes a row from the Maintain subnet table
    returns a new network object and creates the vlan it is associated with
    """
    prefixlen = str(calc_prefixlen(netmask))
    network = str(ipaddr.IPv4Address(subnet & netmask))
    s, _ = Site.objects.get_or_create(name='Campus')
    v = None
    if cursor.execute("SELECT * "
                      "FROM vlan "
                      "WHERE vlan_id = %s" % vlan):
        vlan_id, vlan_name, vlan_number = cursor.fetchone()
        v = Vlan.objects.get(name=vlan_name)
    n, created = Network.objects.get_or_create(
        network_str=network + '/' + prefixlen, ip_type='4',
        site=s, vlan=v)
    cursor.execute("SELECT dhcp_option, value "
                   "FROM object_option "
                   "WHERE object_id = {0} "
                   "AND type = 'subnet'".format(subnet_id))
    results = cursor.fetchall()
    for dhcp_option, value in results:
        cursor.execute("SELECT name, type "
                       "FROM dhcp_options "
                       "WHERE id = {0}".format(dhcp_option))
        name, type = cursor.fetchone()
        kv, _ = NetworkKeyValue.objects.get_or_create(
            value=str(value), key=name, network=n)
    return (n, created)


def create_range(range_id, start, end, range_type, subnet_id, comment, en, known):
    """
    Takes a row form the Maintain range table
    returns a range which is saved in cyder
    """
    # Set the allow statement
    n = None
    r = None
    r_type = 'st' if range_type == 'static' else 'dy'
    allow = ALLOW_LEGACY
    if cursor.execute("SELECT * FROM subnet WHERE id = {0}".format(subnet_id)):
        id, name, subnet, netmask, status, vlan = cursor.fetchone()
        n = Network.objects.get(ip_lower=subnet,
                                prefixlen=str(calc_prefixlen(netmask)))
        n.update_network()

        if str(ipaddr.IPv4Address(start)) in allow_all_subnets:
            allow = ALLOW_ANY
        elif known:
            allow = ALLOW_KNOWN
        elif '192.168.3.11' == str(ipaddr.IPv4Address(start)):
            allow = ALLOW_LEGACY_AND_VRF
            v, _ = Vrf.objects.get_or_create(name="ip-phones-hack")
            n.vrf = v
            n.save()
        elif '172.16.58.3' == str(ipaddr.IPv4Address(start)):
            allow = ALLOW_LEGACY_AND_VRF
            v, _ = Vrf.objects.get_or_create(name="avaya-hack")
            n.vrf = v
            n.save()

        if int(n.network.network) < start < end < int(n.network.broadcast):
            r, created = Range.objects.get_or_create(
                start_lower=start, start_str=ipaddr.IPv4Address(start),
                end_lower=end, end_str=ipaddr.IPv4Address(end),
                range_type=r_type, allow=allow, ip_type='4',
                network=n)
    if not r:
        r, created = Range.objects.get_or_create(
            start_lower=start, start_str=ipaddr.IPv4Address(start),
            end_lower=end, end_str=ipaddr.IPv4Address(end),
{'fraction_non_alphanumeric': 0.11646201074443592, 'fraction_numerical': 0.04470452801227936, 'mean_word_length': 2.374878601489155, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 48, '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': '3308781', 'n_tokens_mistral': 3758, 'n_tokens_neox': 3430, 'n_words': 732}