<commit_before># Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Authors: <NAME> <<EMAIL>>
# -*- coding: utf-8 -*-

from ServiceProvider import ServiceProvider
from LMI_DiskPartitionConfigurationSetting import LMI_DiskPartitionConfigurationSetting
import pywbem
import pyanaconda.storage.formats
import util.partitioning
import parted

class LMI_DiskPartitionConfigurationService(ServiceProvider):
    """
        LMI_DiskPartitionConfigurationService provider implementation.
    """
    def __init__(self, *args, **kwargs):
        super(LMI_DiskPartitionConfigurationService, self).__init__(
                "LMI_DiskPartitionConfigurationService", *args, **kwargs)

    def get_instance(self, env, model):
        model = super(LMI_DiskPartitionConfigurationService, self).get_instance(
                env, model)
        model['PartitioningSchemes'] = self.Values.PartitioningSchemes.Volumes_may_be_partitioned_or_treated_as_whole
        return model

    def cim_method_setpartitionstyle(self, env, object_name,
                                     param_extent=None,
                                     param_partitionstyle=None):
        """
            Implements LMI_DiskPartitionConfigurationService.SetPartitionStyle()

            This method installs a partition table on an extent of the
            specified partition style; creating an association between the
            extent and that capabilities instances referenced as method
            parameters. As a side effect, the consumable block size of the
            underlying extent is reduced by the block size of the metadata
            reserved by the partition table and associated metadata. This size
            is in the PartitionTableSize property of the associated
            DiskPartitionConfigurationCapabilities instance.
        """
        logger = env.get_logger()
        logger.log_debug('Entering %s.cim_method_setpartitionstyle()' \
                % self.__class__.__name__)

        # check parameters here, the real work is done in _setpartitionstyle
        self.check_instance(object_name)

        if not param_extent:
            raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER,
                    "Parameter Extent is mandatory.")

        # check the device
        device = self.provider_manager.get_device_for_name(param_extent)
        if not device:
            raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                    "Cannot find the Extent.")
        if isinstance(device, pyanaconda.storage.devices.PartitionDevice):
            raise pywbem.CIMError(pywbem.CIM_ERR_NOT_SUPPORTED,
                    "Creation of extended partitions is not supported.")
        if self.storage.deviceDeps(device):
            raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                    "The Extent is used.")

        # check the capabilities
        capabilities_provider = self.provider_manager.get_capabilities_provider_for_class('LMI_DiskPartitionConfigurationCapabilities')
        if not capabilities_provider:
            raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                    "Cannot find capabilities provider.")
        if param_partitionstyle:
            capabilities = capabilities_provider.get_capabilities_for_id(
                    param_partitionstyle['InstanceID'])
            if not capabilities:
                raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER,
                        "Cannot find capabilities for given PartitionStyle.")
        else:
            # find the default capabilities
            capabilities = capabilities_provider.get_default_capabilities()
            raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER,
                    "Parameter PartitionStyle is mandatory, there is no default PartitionStyle.")

        if capabilities['PartitionStyle'] == capabilities_provider.Values.PartitionStyle.EMBR:
            raise pywbem.CIMError(pywbem.CIM_ERR_NOT_SUPPORTED,
                    "Creation of extended partitions is not supported.")

        retval = self._setpartitionstyle(device, capabilities, capabilities_provider)
        return (retval, [])

    def _setpartitionstyle(self, device, capabilities, capabilities_provider):
        """
            Really set the partition style, all parameters were successfully
            checked.
        """
        if capabilities['PartitionStyle'] == capabilities_provider.Values.PartitionStyle.MBR:
            label = "msdos"
        elif capabilities['PartitionStyle'] == capabilities_provider.Values.PartitionStyle.GPT:
            label = "gpt"
        else:
            raise pywbem.CIMError(pywbem.CIM_ERR_NOT_SUPPORTED,
                    "Unsupported PartitionStyle:"
                    + str(capabilities['PartitionStyle']) + ".")

        fmt = pyanaconda.storage.formats.getFormat('disklabel', labelType=label)
        action = pyanaconda.storage.deviceaction.ActionCreateFormat(device, fmt)
        self.storage.devicetree.registerAction(action)
        action.execute()
        return self.Values.SetPartitionStyle.Success
<commit_msg>Fixed getting default partition capabilities.<commit_after># Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Authors: <NAME> <<EMAIL>>
# -*- coding: utf-8 -*-

from ServiceProvider import ServiceProvider
from LMI_DiskPartitionConfigurationSetting import LMI_DiskPartitionConfigurationSetting
import pywbem
import pyanaconda.storage.formats
import util.partitioning
import parted

class LMI_DiskPartitionConfigurationService(ServiceProvider):
    """
        LMI_DiskPartitionConfigurationService provider implementation.
    """
    def __init__(self, *args, **kwargs):
        super(LMI_DiskPartitionConfigurationService, self).__init__(
                "LMI_DiskPartitionConfigurationService", *args, **kwargs)

    def get_instance(self, env, model):
        model = super(LMI_DiskPartitionConfigurationService, self).get_instance(
                env, model)
        model['PartitioningSchemes'] = self.Values.PartitioningSchemes.Volumes_may_be_partitioned_or_treated_as_whole
        return model

    def cim_method_setpartitionstyle(self, env, object_name,
                                     param_extent=None,
                                     param_partitionstyle=None):
        """
            Implements LMI_DiskPartitionConfigurationService.SetPartitionStyle()

            This method installs a partition table on an extent of the
            specified partition style; creating an association between the
            extent and that capabilities instances referenced as method
            parameters. As a side effect, the consumable block size of the
            underlying extent is reduced by the block size of the metadata
            reserved by the partition table and associated metadata. This size
            is in the PartitionTableSize property of the associated
            DiskPartitionConfigurationCapabilities instance.
        """
        logger = env.get_logger()
        logger.log_debug('Entering %s.cim_method_setpartitionstyle()' \
                % self.__class__.__name__)

        # check parameters here, the real work is done in _setpartitionstyle
        self.check_instance(object_name)

        if not param_extent:
            raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER,
                    "Parameter Extent is mandatory.")

        # check the device
        device = self.provider_manager.get_device_for_name(param_extent)
        if not device:
            raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                    "Cannot find the Extent.")
        if isinstance(device, pyanaconda.storage.devices.PartitionDevice):
            raise pywbem.CIMError(pywbem.CIM_ERR_NOT_SUPPORTED,
                    "Creation of extended partitions is not supported.")
        if self.storage.deviceDeps(device):
            raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                    "The Extent is used.")

        # check the capabilities
        capabilities_provider = self.provider_manager.get_capabilities_provider_for_class('LMI_DiskPartitionConfigurationCapabilities')
        if not capabilities_provider:
            raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
                    "Cannot find capabilities provider.")
        if param_partitionstyle:
            capabilities = capabilities_provider.get_capabilities_for_id(
                    param_partitionstyle['InstanceID'])
            if not capabilities:
                raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER,
                        "Cannot find capabilities for given PartitionStyle.")
        else:
            # find the default capabilities
            capabilities = capabilities_provider.get_default_capabilities()
            if not capabilities:
                raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER,
                        "Parameter PartitionStyle is mandatory, there is no default PartitionStyle.")

        if capabilities['PartitionStyle'] == capabilities_provider.Values.PartitionStyle.EMBR:
            raise pywbem.CIMError(pywbem.CIM_ERR_NOT_SUPPORTED,
                    "Creation of extended partitions is not supported.")

        retval = self._setpartitionstyle(device, capabilities, capabilities_provider)
        return (retval, [])

    def _setpartitionstyle(self, device, capabilities, capabilities_provider):
        """
            Really set the partition style, all parameters were successfully
            checked.
        """
        if capabilities['PartitionStyle'] == capabilities_provider.Values.PartitionStyle.MBR:
            label = "msdos"
        elif capabilities['PartitionStyle'] == capabilities_provider.Values.PartitionStyle.GPT:
            label = "gpt"
        else:
            raise pywbem.CIMError(pywbem.CIM_ERR_NOT_SUPPORTED,
                    "Unsupported PartitionStyle:"
                    + str(capabilities['PartitionStyle']) + ".")

        fmt = pyanaconda.storage.formats.getFormat('disklabel', labelType=label)
        action = pyanaconda.storage.deviceaction.ActionCreateFormat(device, fmt)
        self.storage.devicetree.registerAction(action)
        action.execute()
        return self.Values.SetPartitionStyle.Success

{'fraction_non_alphanumeric': 0.0650932947090398, 'fraction_numerical': 0.0030672233108971626, 'mean_word_length': 2.7609740467798782, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2009144', 'n_tokens_mistral': 2918, 'n_tokens_neox': 2846, 'n_words': 847}