<commit_before># Copyright 2015 Red Hat Inc., Durham, North Carolina.
# All Rights Reserved.
#
# openscap-daemon 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.
#
# openscap-daemon 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 openscap-daemon.  If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
#   <NAME> <<EMAIL>>


from openscap_daemon import et_helpers
from openscap_daemon import oscap_helpers

from xml.etree import cElementTree as ElementTree
from datetime import datetime, timedelta
import os.path
import shutil
import threading
import logging
import tempfile


class SCAPInput(object):
    """Encapsulates all sorts of SCAP input, either embedded in the task spec
    itself or separate in a file installed via RPM or other means.

    This does not include tailoring! That is handled separately,
    see SCAPTailoring class.
    """

    def __init__(self):
        self.file_path = None
        self.temp_file = None
        self.datastream_id = None
        self.xccdf_id = None

    def is_valid(self):
        return self.file_path is not None

    def is_equivalent_to(self, other):
        # TODO input_file and tailoring_file may have the same contents and
        # different paths and the tasks wouldn't end up being equivalent.

        return \
            self.file_path == other.file_path and \
            self.datastream_id == other.datastream_id and \
            self.xccdf_id == other.xccdf_id

    def set_file_path(self, file_path):
        """Sets given file_path to be the input file. If you use this method
        no temporary files will be allocated, the file_path will be passed to
        `oscap` as is, in its absolute form.
        """

        self.temp_file = None
        if file_path is not None:
            self.file_path = \
                os.path.abspath(file_path) if file_path is not None else None

        else:
            self.file_path = None

    def set_contents(self, input_contents):
        """Sets given input_contents XML to be the input source. This method
        allocates a temporary file that exists for the lifetime of this
        instance.
        """

        if input_contents is not None:
            self.temp_file = tempfile.NamedTemporaryFile()
            self.temp_file.write(input_contents.encode("utf-8"))
            self.temp_file.flush()

            self.file_path = os.path.abspath(self.temp_file.name)

        else:
            self.file_path = None

    def load_from_xml_element(self, element):
        input_file = element.get("href")
        if input_file is not None:
            self.set_file_path(input_file)

        else:
            input_file_contents = element.text
            self.set_contents(input_file_contents)

        self.datastream_id = element.get("datastream_id")
        self.xccdf_id = element.get("xccdf_id")

    def to_xml_element(self):
        if self.file_path is None:
            return None

        ret = ElementTree.Element("input")
        if self.temp_file is None:
            ret.set("href", self.file_path)
        else:
            with open(self.temp_file.name, "r") as f:
                ret.text = f.read().decode("utf-8")

        if self.datastream_id is not None:
            ret.set("datastream_id", self.datastream_id)
        if self.xccdf_id is not None:
            ret.set("xccdf_id", self.xccdf_id)

        return ret


class SCAPTailoring(object):
    """Encapsulates SCAP tailoring. At this point we only support separate files
    as tailorings, not the input SCAP file. The tailoring has to be plain
    XCCDF tailoring, no datastreams!
    """

    def __init__(self):
        self.file_path = None
        self.temp_file = None

    def is_equivalent_to(self, other):
        # TODO input_file and tailoring_file may have the same contents and
        # different paths and the tasks wouldn't end up being equivalent.

        return \
            self.file_path == other.file_path

    def set_file_path(self, file_path):
        """Sets given file_path to be the input file. If you use this method
        no temporary files will be allocated, the file_path will be passed to
        `oscap` as is, in its absolute form.
        """

        self.temp_file = None
        if file_path is not None:
            self.file_path = \
                os.path.abspath(file_path) if file_path is not None else None

        else:
            self.file_path = None<commit_msg>Compare XML contents of input_ and tailoring instead of the file paths<commit_after># Copyright 2015 Red Hat Inc., Durham, North Carolina.
# All Rights Reserved.
#
# openscap-daemon 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.
#
# openscap-daemon 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 openscap-daemon.  If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
#   <NAME> <<EMAIL>>


from openscap_daemon import et_helpers
from openscap_daemon import oscap_helpers

from xml.etree import cElementTree as ElementTree
from datetime import datetime, timedelta
import os.path
import shutil
import threading
import logging
import tempfile


class SCAPInput(object):
    """Encapsulates all sorts of SCAP input, either embedded in the task spec
    itself or separate in a file installed via RPM or other means.

    This does not include tailoring! That is handled separately,
    see SCAPTailoring class.
    """

    def __init__(self):
        self.file_path = None
        self.temp_file = None
        self.datastream_id = None
        self.xccdf_id = None

    def is_valid(self):
        return self.file_path is not None

    def get_xml_source(self):
        if self.file_path is None:
            return None

        with open(self.file_path, "r") as f:
            return f.read().decode("utf-8")

    def is_equivalent_to(self, other):
        return \
            self.get_xml_source() == other.get_xml_source() and \
            self.datastream_id == other.datastream_id and \
            self.xccdf_id == other.xccdf_id

    def set_file_path(self, file_path):
        """Sets given file_path to be the input file. If you use this method
        no temporary files will be allocated, the file_path will be passed to
        `oscap` as is, in its absolute form.
        """

        self.temp_file = None
        if file_path is not None:
            self.file_path = \
                os.path.abspath(file_path) if file_path is not None else None

        else:
            self.file_path = None

    def set_contents(self, input_contents):
        """Sets given input_contents XML to be the input source. This method
        allocates a temporary file that exists for the lifetime of this
        instance.
        """

        if input_contents is not None:
            self.temp_file = tempfile.NamedTemporaryFile()
            self.temp_file.write(input_contents.encode("utf-8"))
            self.temp_file.flush()

            self.file_path = os.path.abspath(self.temp_file.name)

        else:
            self.file_path = None

    def load_from_xml_element(self, element):
        input_file = element.get("href")
        if input_file is not None:
            self.set_file_path(input_file)

        else:
            input_file_contents = element.text
            self.set_contents(input_file_contents)

        self.datastream_id = element.get("datastream_id")
        self.xccdf_id = element.get("xccdf_id")

    def to_xml_element(self):
        if self.file_path is None:
            return None

        ret = ElementTree.Element("input")
        if self.temp_file is None:
            ret.set("href", self.file_path)
        else:
            with open(self.temp_file.name, "r") as f:
                ret.text = f.read().decode("utf-8")

        if self.datastream_id is not None:
            ret.set("datastream_id", self.datastream_id)
        if self.xccdf_id is not None:
            ret.set("xccdf_id", self.xccdf_id)

        return ret


class SCAPTailoring(object):
    """Encapsulates SCAP tailoring. At this point we only support separate files
    as tailorings, not the input SCAP file. The tailoring has to be plain
    XCCDF tailoring, no datastreams!
    """

    def __init__(self):
        self.file_path = None
        self.temp_file = None

    def get_xml_source(self):
        if self.file_path is None:
            return None

        with open(self.file_path, "r") as f:
            return f.read().decode("utf-8")

    def is_equivalent_to(self, other):
        return \
            self.get_xml_source() == other.get_xml_source()

    def set_file_path(self, file_path):
        """Sets given file_path to be the input file. If you use this method
        no temporary files will be allocated, the file_path will be passed to
        `oscap` as is, in its absolute form.
        """

        self.temp_file = None
        if file_path is not None:
            self.file_path = \
                os.path.abspath(file_path) if file_path is not None else None

        else:
            self.file_path = None
{'fraction_non_alphanumeric': 0.07285901177417732, 'fraction_numerical': 0.0018114118949381101, 'mean_word_length': 2.3987688098495212, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 12}, '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': '1037809', 'n_tokens_mistral': 2898, 'n_tokens_neox': 2815, 'n_words': 998}