<commit_before>from nailgun.provision import ProvisionException
from nailgun.provision import ProvisionAlreadyExists, ProvisionDoesNotExist
from nailgun.provision import Provision
import logging
import xmlrpclib


class Cobbler(Provision):
    def __init__(self, config):
        self.logger = logging.getLogger('provision.cobbler')
        try:
            self.url = config.url
            self.user = config.user
            self.password = <PASSWORD>
        except AttributeError as e:
            self.logger.error(
                'Provision configuration error.' \
                ' Not all necessary attributes are set properly.'
            )
            raise e

        self.logger.debug(
            'Cobbler config: url="%s", user="%s", password="%s"' \
            % (self.url, self.user, self.password)
        )

        try:
            self.server = xmlrpclib.Server(self.url)
            self.token = self.server.login(self.user, self.password)
        except ProvisionException as e:
            self.logger.error(
                'Error occured while connecting to provision server.'
            )
            raise e

    def _get_any_profile(self):
        profiles = self.server.get_profiles(self.token)
        if profiles:
            return profiles[0]
        raise ProvisionException("There is no available profiles")

    def system_by_name(self, name):
        systems = self.server.find_system({'name': name}, self.token)
        if systems:
            if len(systems) > 1:
                self.logger.error(
                    "There are more than one system found by pattern: %s" \
                    % name
                )
                raise ProvisionException(
                    "There are more than one system found by pattern: %s" \
                    % name
                )
            return systems[0]
        return None

    # FIXME
    # IT NEEDED TO BE IMPLEMENTED AS ONLY METHOD FOR ADD AND EDIT
    def add_system(self, name, mac, profile, kopts=""):
        if self.system_by_name(name):
            self.logger.error(
                "Trying to add system that already exists: %s" \
                % name
            )
            raise ProvisionAlreadyExists(
                "System with name %s already exists. Try to edit it." \
                % name
            )
        system_id = self.server.new_system(self.token)
        self.server.modify_system(
            system_id, 'name', name, self.token
        )
        self.server.modify_system(
            system_id, 'profile', profile.name, self.token
        )
        self.server.modify_system(
            system_id, 'kopts', kopts, self.token
        )
        self.server.modify_system(
            system_id, 'modify_interface', {
                "macaddress-eth0": mac,
            }, self.token<commit_msg>[nailgun/nailgun/provision/driver/cobbler.py] Fixed signature error in add_system method<commit_after>from nailgun.provision import ProvisionException
from nailgun.provision import ProvisionAlreadyExists, ProvisionDoesNotExist
from nailgun.provision import Provision
import logging
import xmlrpclib


class Cobbler(Provision):
    def __init__(self, config):
        self.logger = logging.getLogger('provision.cobbler')
        try:
            self.url = config.url
            self.user = config.user
            self.password = <PASSWORD>
        except AttributeError as e:
            self.logger.error(
                'Provision configuration error.' \
                ' Not all necessary attributes are set properly.'
            )
            raise e

        self.logger.debug(
            'Cobbler config: url="%s", user="%s", password="%s"' \
            % (self.url, self.user, self.password)
        )

        try:
            self.server = xmlrpclib.Server(self.url)
            self.token = self.server.login(self.user, self.password)
        except ProvisionException as e:
            self.logger.error(
                'Error occured while connecting to provision server.'
            )
            raise e

    def _get_any_profile(self):
        profiles = self.server.get_profiles(self.token)
        if profiles:
            return profiles[0]
        raise ProvisionException("There is no available profiles")

    def system_by_name(self, name):
        systems = self.server.find_system({'name': name}, self.token)
        if systems:
            if len(systems) > 1:
                self.logger.error(
                    "There are more than one system found by pattern: %s" \
                    % name
                )
                raise ProvisionException(
                    "There are more than one system found by pattern: %s" \
                    % name
                )
            return systems[0]
        return None

    # FIXME
    # IT NEEDED TO BE IMPLEMENTED AS ONLY METHOD FOR ADD AND EDIT
    def add_system(self, name, mac, power, profile, kopts=""):
        if self.system_by_name(name):
            self.logger.error(
                "Trying to add system that already exists: %s" \
                % name
            )
            raise ProvisionAlreadyExists(
                "System with name %s already exists. Try to edit it." \
                % name
            )
        system_id = self.server.new_system(self.token)
        self.server.modify_system(
            system_id, 'name', name, self.token
        )
        self.server.modify_system(
            system_id, 'profile', profile.name, self.token
        )
        self.server.modify_system(
            system_id, 'kopts', kopts, self.token
        )
        self.server.modify_system(
            system_id, 'modify_interface', {
                "macaddress-eth0": mac,
            }, self.token
{'fraction_non_alphanumeric': 0.08614102118791248, 'fraction_numerical': 0.0013893713094824591, 'mean_word_length': 1.650253106304648, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, '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': '402723', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1506, 'n_words': 404}