<commit_before>#
# Copyright (c) 2012 rPath, Inc.
#
from xml.etree import cElementTree as etree
from services import ServiceInfo
from services import ServiceScanner
from packages import PackageScanner
from packages import IDFactory
from configurators import RunConfigurators
from configurators import valuesXmlPath
from preview import Preview
from descriptors import Descriptors

import time
import os


class SurveyScanner(object):
    def __init__(self):
        self._serviceScanner = ServiceScanner(ServiceInfo)
        self._packageScanner = PackageScanner(IDFactory())

    def _getServices(self):
        return self._serviceScanner.getServices()

    def _getPackages(self):
        return self._packageScanner.scan()

    def getServicesXML(self):
        services = self._getServices()
        rpm_pkgs, conary_pkgs = self._getPackages()
        services_xml = etree.Element('services')
        srv_id = 100
        for srv in services:
            if srv.conary_pkg:
                troves = [ conary_pkgs.get(x) for x in srv.conary_pkg 
                            if x in conary_pkgs ]
                for trove in troves:
                    srv.conary_pkg_uri = self._packageScanner._idfactory.getId(
                                            trove)
            if srv.rpm_pkg:
                rpms = [ rpm_pkgs.get(x) for x in srv.rpm_pkg 
                            if x in rpm_pkgs]
                for rpm in rpms:
                    srv.rpm_pkg_uri = self._packageScanner._idfactory.getId(rpm)
            services_xml.append(srv.toxml(str(srv_id)))
            srv_id += 1
        return services_xml

    def getPackagesXML(self):
        rpm_xml, conary_xml = self._packageScanner.toxml()
        rpm_pkgs_xml = etree.Element('rpm_packages')
        for result in rpm_xml:
            rpm_pkgs_xml.append(result)
        conary_pkgs_xml = etree.Element('conary_packages')
        for result in conary_xml:
            conary_pkgs_xml.append(result)
        return rpm_pkgs_xml, conary_pkgs_xml

    def getValuesXML(self):
        values_xml = etree.Element('config_properties')
        if os.path.exists(valuesXmlPath):
            values_xml.append(etree.parse(valuesXmlPath).getroot())
        return values_xml

    def getPreviewXML(self, sources=None):
        preview = Preview()
        raw_preview_xml = preview.preview(sources)
        if raw_preview_xml:
            try:
                preview_xml = etree.fromstring(raw_preview_xml)
            except SyntaxError:
                pass
        return preview_xml

    def getConfigurators(self):
        # Adds observed_values, discovered_values, and validation_report
        # from configurators.
        configurators = RunConfigurators()
        configurators_xml = configurators.toxml()
        configurator_nodes = []
        for nodes in configurators_xml.getchildren():
            configurator_nodes.append(nodes)
        return configurator_nodes

    def getDescriptors(self):
        descriptors_xml = etree.Element('config_properties_descriptor')<commit_msg>Adding top level status to Validation_Report<commit_after>#
# Copyright (c) 2012 rPath, Inc.
#
from xml.etree import cElementTree as etree
from services import ServiceInfo
from services import ServiceScanner
from packages import PackageScanner
from packages import IDFactory
from configurators import RunConfigurators
from configurators import valuesXmlPath
from preview import Preview
from descriptors import Descriptors

import sys 
import time
import os


class SurveyScanner(object):
    def __init__(self):
        self._serviceScanner = ServiceScanner(ServiceInfo)
        self._packageScanner = PackageScanner(IDFactory())

    def _getServices(self):
        return self._serviceScanner.getServices()

    def _getPackages(self):
        return self._packageScanner.scan()

    def getServicesXML(self):
        services = self._getServices()
        rpm_pkgs, conary_pkgs = self._getPackages()
        services_xml = etree.Element('services')
        srv_id = 100
        for srv in services:
            if srv.conary_pkg:
                troves = [ conary_pkgs.get(x) for x in srv.conary_pkg 
                            if x in conary_pkgs ]
                for trove in troves:
                    srv.conary_pkg_uri = self._packageScanner._idfactory.getId(
                                            trove)
            if srv.rpm_pkg:
                rpms = [ rpm_pkgs.get(x) for x in srv.rpm_pkg 
                            if x in rpm_pkgs]
                for rpm in rpms:
                    srv.rpm_pkg_uri = self._packageScanner._idfactory.getId(rpm)
            services_xml.append(srv.toxml(str(srv_id)))
            srv_id += 1
        return services_xml

    def getPackagesXML(self):
        rpm_xml, conary_xml = self._packageScanner.toxml()
        rpm_pkgs_xml = etree.Element('rpm_packages')
        for result in rpm_xml:
            rpm_pkgs_xml.append(result)
        conary_pkgs_xml = etree.Element('conary_packages')
        for result in conary_xml:
            conary_pkgs_xml.append(result)
        return rpm_pkgs_xml, conary_pkgs_xml
        
    def getValuesXML(self):
        values_xml = etree.Element('config_properties')
        if os.path.exists(valuesXmlPath):
            values_xml.append(etree.parse(valuesXmlPath).getroot())
        return values_xml
 
    def getPreviewXML(self, sources=None):
        preview = Preview()
        raw_preview_xml = preview.preview(sources)
        if raw_preview_xml:
            try:
                preview_xml = etree.fromstring(raw_preview_xml)
            except SyntaxError:
                pass
        return preview_xml

    def getConfigurators(self):
        # Adds observed_values, discovered_values, and validation_report
        # from configurators.
        configurators = RunConfigurators()
        configurators_xml = configurators.toxml()
        configurator_nodes = []
        for node in configurators_xml.getchildren():
            if node.tag == 'validation_report':
                validate = 'pass'
                status_node = etree.SubElement(node, 'status')
                results = node.findall('.//success')
                results.append(node.findall('.//status'))
                if 'false' or 'fail' in results.lower():
                    validate = 'fail'
                status_node.text = validate
                configurator_nodes.append(status_node)
            configurator_nodes.append(node)
        return configurator_nodes

    def getDescriptors(self):
        descriptors_xml = etree.Element('config_properties_descriptor')
{'fraction_non_alphanumeric': 0.07155797101449275, 'fraction_numerical': 0.0024154589371980675, 'mean_word_length': 2.23486328125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 56}, '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': '1544587', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1848, 'n_words': 416}