<commit_before>from ipaddress import IPv4Interface  # NOQA
import os
from shutil import rmtree
from typing import Any, Dict, List, Optional, Tuple  # NOQA

from netns import NetNS as nsscope

from iptc import Chain, Match, Policy, Rule, Table, Target

from pyroute2 import IPRoute
from pyroute2 import NetlinkError
from pyroute2 import NetNS
from pyroute2 import NSPopen

from n0library.logger import Logger


logger = Logger(name=__name__)  # type: Logger


class Dnsmasq(object):
    """
    Manage namespace, veth pair, directory and dnsmasq process.

    Example:
        Create DHCP server.
        >>> masq = dnsmasq("subnet01")
        >>> ip_address = ipaddress.IPv4Interface.ip_interface('192.168.1.1/24')
        >>> pool = ['192.168.1.2', '192.168.1.254']
        >>> bridge = 'br-subnet01'
        >>> masq.create_dhcp_server(ip_address, bridge, pool)

        Enable VM having the MAC address to communicate dnsmasq process with DHCP.
        >>> masq.add_allowed_host('9e:e8:1d:d0:2c:c9')

        Delete allowed host.
        >>> masq.delete_allowed_host('9e:e8:1d:d0:2c:c9')

        Add static MAC-IP map entry. VM having the MAC address always receives specified IP address.
        >>> masq.add_host_entry('9e:e8:1d:d0:2c:c9', '192.168.1.11')

        Delete MAC-IP entry.
        >>> masq.delete_host_entry('9e:e8:1d:d0:2c:c9')

        Stop dnsmasq process.
        >>> masq.stop_process()

        Start dnsmasq process.
        >>> masq.start_process(pool)

        Respawn dnsmasq process to change its DHCP allocation pool.
        >>> pool = ['192.168.1.2', '192.168.1.128']
        >>> masq.respawn_procces(pool)

        Delete DHCP server.
        >>> masq.delete_dhcp_server()
    """
    ip = IPRoute()  # type: IPRoute

    def __init__(self, subnet_id, log_dir):
        # type: (str, str) -> None
        """
        Set names in order to create or delete resources.

        Args:
            subnet_id: Subnet ID.
            log_dir: Directory path to save log file.
        """
        self.netns_name = 'dhcp-{}'.format(subnet_id)  # type: str
        self.tap_name = 'tap-dhcp-{}'.format(subnet_id)  # type: str
        self.peer_name = 'eth-dhcp-{}'.format(subnet_id)  # type: str

        self.dirname = os.path.join('/var/lib/n0stack/', self.netns_name)  # type: str
        self.pid_filename = os.path.join(self.dirname, 'pid')  # type: str
        self.dhcp_hostsfilename = os.path.join(self.dirname, 'hosts')  # type: str
        self.dhcp_leasefilename = os.path.join(self.dirname, 'lease')  # type: str
        self.dhcp_optsfilename = os.path.join(self.dirname, 'opts')  # type: str
        self.log_path = os.path.join(log_dir, 'dnsmasq-{}.log'.format(self.netns_name))  # type: str
<commit_msg>sorry, fix typo agin<commit_after>from ipaddress import IPv4Interface  # NOQA
import os
from shutil import rmtree
from typing import Any, Dict, List, Optional, Tuple  # NOQA

from netns import NetNS as nsscope

from iptc import Chain, Match, Policy, Rule, Table, Target

from pyroute2 import IPRoute
from pyroute2 import NetlinkError
from pyroute2 import NetNS
from pyroute2 import NSPopen

from n0library.logger import Logger


logger = Logger(name=__name__)  # type: Logger


class Dnsmasq(object):
    """
    Manage namespace, veth pair, directory and dnsmasq process.

    Example:
        Create DHCP server.
        >>> masq = dnsmasq("subnet01")
        >>> ip_address = ipaddress.IPv4Interface.ip_interface('192.168.1.1/24')
        >>> pool = ['192.168.1.2', '192.168.1.254']
        >>> bridge = 'br-subnet01'
        >>> masq.create_dhcp_server(ip_address, bridge, pool)

        Enable VM having the MAC address to communicate dnsmasq process with DHCP.
        >>> masq.add_allowed_host('9e:e8:1d:d0:2c:c9')

        Delete allowed host.
        >>> masq.delete_allowed_host('9e:e8:1d:d0:2c:c9')

        Add static MAC-IP map entry. VM having the MAC address always receives specified IP address.
        >>> masq.add_host_entry('9e:e8:1d:d0:2c:c9', '192.168.1.11')

        Delete MAC-IP entry.
        >>> masq.delete_host_entry('9e:e8:1d:d0:2c:c9')

        Stop dnsmasq process.
        >>> masq.stop_process()

        Start dnsmasq process.
        >>> masq.start_process(pool)

        Respawn dnsmasq process to change its DHCP allocation pool.
        >>> pool = ['192.168.1.2', '192.168.1.128']
        >>> masq.respawn_process(pool)

        Delete DHCP server.
        >>> masq.delete_dhcp_server()
    """
    ip = IPRoute()  # type: IPRoute

    def __init__(self, subnet_id, log_dir):
        # type: (str, str) -> None
        """
        Set names in order to create or delete resources.

        Args:
            subnet_id: Subnet ID.
            log_dir: Directory path to save log file.
        """
        self.netns_name = 'dhcp-{}'.format(subnet_id)  # type: str
        self.tap_name = 'tap-dhcp-{}'.format(subnet_id)  # type: str
        self.peer_name = 'eth-dhcp-{}'.format(subnet_id)  # type: str

        self.dirname = os.path.join('/var/lib/n0stack/', self.netns_name)  # type: str
        self.pid_filename = os.path.join(self.dirname, 'pid')  # type: str
        self.dhcp_hostsfilename = os.path.join(self.dirname, 'hosts')  # type: str
        self.dhcp_leasefilename = os.path.join(self.dirname, 'lease')  # type: str
        self.dhcp_optsfilename = os.path.join(self.dirname, 'opts')  # type: str
        self.log_path = os.path.join(log_dir, 'dnsmasq-{}.log'.format(self.netns_name))  # type: str

{'fraction_non_alphanumeric': 0.12716026923776605, 'fraction_numerical': 0.03310896852828816, 'mean_word_length': 2.709851551956815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2203653', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1893, 'n_words': 447}