<commit_before>#
# Copyright (c) SAS Institute Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


"""
Edit product definition
"""
import os
import tempfile
import subprocess
import StringIO
from lxml import etree

from rbuild import pluginapi

class EditCommand(pluginapi.command.CommandWithSubCommands):
    """
    Edit product definition
    """
    commands = ['edit']
    help = 'Edit product definition'

class EditProductCommand(pluginapi.command.BaseCommand):
    """
    Edit product definition XML
    """
    help = 'Edit product definition XML'
    docs = {
            'message' : 'Text describing why the commit was performed',
    }

    def addLocalParameters(self, argDef):
        argDef['message'] = '-m', pluginapi.command.ONE_PARAM

    def runCommand(self, handle, argSet, _):
        """
        Process the command line provided for this plugin
        @param handle: context handle
        @type handle: rbuild.handle.RbuildHandle
        """
        message = argSet.pop('message', 'rbuild commit')
        handle.Edit.editProductDefinition(message)

class Edit(pluginapi.Plugin):
    """
    Edit plugin
    """
    name = 'edit'

    def registerCommands(self):
        """
        Register the command-line handling portion of the update plugin.
        """
        self.handle.Commands.registerCommand(EditCommand)
        self.handle.Commands.getCommandClass('edit').registerSubCommand(
                                             'product', EditProductCommand)

    def editProductDefinition(self, message):
        handle = self.handle
        tmpf = self._makeTemporaryFile()
        handle.product.serialize(tmpf)
        tmpf.flush()

        try:
            checkModified = True
            while 1:
                ret = self._editProductDefinitionFile(handle, tmpf,
                        checkModified=checkModified)
                checkModified = False
                if ret < 0:
                    # -1 means no change, so return 0 in that case
                    return int(ret != -1)
                if ret == 0:
                    break
                while 1:
                    retry = handle.ui.input("Do you want to retry? (Y/n) ")
                    retry = retry.strip().lower()
                    if not retry:
                        retry = 'y'
                    if retry == 'n':
                        return ret
                    if retry == 'y':
                        break
        finally:
            # Cleanup here
            pass
        # We now have a changed product definition loaded
        # Serialize it
        handle.product.serialize(
                file(handle.productStore.getProductDefinitionXmlPath(), "w"))
        # And commit
        handle.productStore.commit(message=message)<commit_msg>Exception handling for (RCE-1992)<commit_after>#
# Copyright (c) SAS Institute Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


"""
Edit product definition
"""
import os
import tempfile
import subprocess
import StringIO
from lxml import etree

from rbuild import pluginapi

class EditCommand(pluginapi.command.CommandWithSubCommands):
    """
    Edit product definition
    """
    commands = ['edit']
    help = 'Edit product definition'

class EditProductCommand(pluginapi.command.BaseCommand):
    """
    Edit product definition XML
    """
    help = 'Edit product definition XML'
    docs = {
            'message' : 'Text describing why the commit was performed',
    }

    def addLocalParameters(self, argDef):
        argDef['message'] = '-m', pluginapi.command.ONE_PARAM

    def runCommand(self, handle, argSet, _):
        """
        Process the command line provided for this plugin
        @param handle: context handle
        @type handle: rbuild.handle.RbuildHandle
        """
        message = argSet.pop('message', 'rbuild commit')
        handle.Edit.editProductDefinition(message)

class Edit(pluginapi.Plugin):
    """
    Edit plugin
    """
    name = 'edit'

    def registerCommands(self):
        """
        Register the command-line handling portion of the update plugin.
        """
        self.handle.Commands.registerCommand(EditCommand)
        self.handle.Commands.getCommandClass('edit').registerSubCommand(
                                             'product', EditProductCommand)

    def editProductDefinition(self, message):
        handle = self.handle
        tmpf = self._makeTemporaryFile()
        
        try:
            handle.product.serialize(tmpf)
        except Exception:
            handle.ui.writeError("Command run outside of expected context, "
                                 "make sure you're in a checkout directory.")
            return 1
        tmpf.flush()
        try:
            checkModified = True
            while 1:
                ret = self._editProductDefinitionFile(handle, tmpf,
                        checkModified=checkModified)
                checkModified = False
                if ret < 0:
                    # -1 means no change, so return 0 in that case
                    return int(ret != -1)
                if ret == 0:
                    break
                while 1:
                    retry = handle.ui.input("Do you want to retry? (Y/n) ")
                    retry = retry.strip().lower()
                    if not retry:
                        retry = 'y'
                    if retry == 'n':
                        return ret
                    if retry == 'y':
                        break
        finally:
            # Cleanup here
            pass
        # We now have a changed product definition loaded
        # Serialize it
        handle.product.serialize(
                file(handle.productStore.getProductDefinitionXmlPath(), "w"))
        # And commit
        handle.productStore.commit(message=message)
{'fraction_non_alphanumeric': 0.08373590982286634, 'fraction_numerical': 0.003952569169960474, 'mean_word_length': 1.9072340425531915, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2305952', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1633, 'n_words': 560}