Esempio n. 1
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)
Esempio n. 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 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 conf.get('trailing_slash',"").lower() == 'true'\
        or conf.get('trailing_slash',"").lower() == 'yes':
        url = "%s/" % url

    # Check to see if there is any content to transfer in the MetaDoc.
    if m.has_content():
        # Do not want to send any data if we're doing a dry run.
        if not dryrun:
            cli = metahttp.XMLClient(url, conf['key'], conf['cert'])

        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)

        # Will not recieve any data on a dry run.
        if not dryrun:
            try:
                res = cli.send(m.get_xml())
            except (urllib2.HTTPError, urllib2.URLError) as httperror:
                logging.error(("Unable to connect to server address \"%s\". "
                    "Error: %s") % (url, httperror))
                # Since we're unable to send the document to the server, we will 
                # cache it so that we can send it at a later date.
                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 res:
                    xml_data = res.read()
                    if verbose:
                        print "%s\nRecieved data:\n%s" % ("-" * 70, "-" * 70)
                        print xml_data
                        print "-" * 70
                    utils.check_response(element.xml_tag_name, 
                                            m, 
                                            xml_data,
                                            send_cache)
                else:
                    logging.error(("Server returned an empty response when "
                        "attempting to send \"%s\". Caching data.") % 
                        element.xml_tag_name)
                    # Since we recieved an empty response from the server we have 
                    # not recieved any reciept for any elements and must cache
                    # them.
                    if not send_cache:
                        logging.warning("No data returned from server, but "
                            "running with --no-cache, so data was not cached.")
                    else:
                        Cacher(element.xml_tag_name, m)
    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)