Example #1
0
def fetch_element(element, conf, verbose):
    url = "%s%s" % (conf['host'], element.url)
    if bool_conf_value(conf.get('trailing_slash',"")):
        url = "%s/" % url
    if verbose:
        print "-" * 70
        print "Connecting to host: %s" % url
        print "Using key: %s" % conf['key']
        print "Using certificate: %s" % conf['cert']
        print "-" * 70
    server_response = utils.send_document(url, conf['key'], conf['cert'])
    if server_response is False:
        sys.stderr.write("Unable to fetch data from \"%s\".\n" %
                            url)
        sys.stderr.write("See log for more information.\n")
    else:
        if verbose:
            print "%s\nRecieved data:\n%s" % ("-" * 70, "-" * 70)
            print server_response
        data = xmlutils.element_from_string(server_response)
        if data is False:
            sys.stderr.write(("Got response from server at url \"%s\", "
                            "but unable to parse. \nError message: %s\n") % 
                            (url, e))
        else:
            # Check for valid according to DTD:
            utils.check_version(data.attrib.get("version"))
            dtd_validation = xmlutils.dtd_validate(data)
            if len(dtd_validation) == 0:
                logging.debug(("Data returned from \"%s\" validated to "
                                "DTD.") % url)
                found_elements = data.findall(
                                element.xml_tag_name
                                )
                sub_elements = []
                for found_element in found_elements:
                    sub_elements.append(MetaElement.from_xml_element(
                                        found_element, element)
                                        )
                element.update_handler(sub_elements).process()
            else:
                logging.error(("XML recieved for \"%s\" did not "
                            "contain valid XML according to DTD.") % 
                            element.xml_tag_name)
                sys.stderr.write(("Received XML document from server "
                            "on url \"%s\", but did not validate "
                            "against DTD.\n") % url)
                sys.stderr.write("Logging with \"debug\" will show "
                            "validation errors.\n")
                dtd_errors = ""
                for error in dtd_validation:
                    dtd_errors = "%s\n%s" % (dtd_errors, error)
                logging.debug("XML DTD errors: %s" % dtd_errors)
Example #2
0
def send_element(element, conf, send_cache, dryrun, verbose, cache_only):
    """Attempts to gather and send information defined by element to server.

    If send_cache is True or dryrun is False, cache will be ignored.
    If cache_only is True, only cache is checked and no new data is gathered.


    @param element: Element class that should be fetched.
    @type element: L{MetaElement} sub class.
    @param conf: Configuration dictionary.
    @type conf: dict
    @param send_cache: Indicates whether cache should be included.
    @type send_cache: bool
    @param dryrun: Indicates whether the script is doing a dry run.
    @type dryrun: bool
    @param verbose: Indicates whether or not the script runs in verbose mode.
    @type verbose: bool
    @param cache_only: Indicates whether to send only cache for this element.
    @type cache_only: bool

    """
    if dryrun and cache_only:
        # We're doing a dry run and we've reached the cached items
        return
    m = MetaDoc(conf.get("site_name"))
    element_processor = get_element_processor(element, send_cache, 
                                                verbose, dryrun)
    if not cache_only:
        # If we're doing cache only, we've reached the possible_send_elements
        # loop and do not want or need to remove elements from it anymore.
        site_element = element.site_handler()
        site_element.populate()
        element_processor.add_elements(site_element.fetch())
    m.reg_meta_element(element_processor)
    # Build URL
    url = "%s%s" % (conf['host'], element.url)
    if bool_conf_value(conf.get('trailing_slash',"")):
        url = "%s/" % url

    # Check to see if there is any content to transfer in the MetaDoc.
    if m.has_content():
        if verbose:
            print "-" * 70
            print "Connecting to host: %s" % url
            print "Using key: %s" % conf['key']
            print "Using certificate: %s" % conf['cert']
            print "-" * 70
            print "Sent data:\n%s" % ("-" * 70)
            print m.get_xml(pretty=True)

        if not dryrun:
            server_response = utils.send_document(url, 
                conf['key'], 
                conf['cert'],
                m)
            if server_response is False:
                if not send_cache:
                    logging.warning("Could not send data to server, "
                        "but running with --no-cache, so data was not cached.")
                else:
                    Cacher(element.xml_tag_name, m)
            else:
                if verbose:
                    print "%s\nRecieved data:\n%s" % ("-" * 70, "-" * 70)
                    print server_response
                    print "-" * 70
                utils.check_response(element.xml_tag_name, 
                                        m, 
                                        server_response,
                                        send_cache)
    else:
        if verbose and not cache_only:
            print "No data to send for \"%s\"." % element.xml_tag_name
        logging.info(("No data to send for \"%s\".") % element.xml_tag_name)