def headers_handler(response): # retrieves the various information from # the response object to be used as diagnostics # information in the observer object status_code = response.status_code status_message = response.status_message content_length = response.headers_map.get("Content-Length") or 0 content_type = response.headers_map.get("Content-Type") or "N/A" content_length_integer = int(content_length) content_length_string = colony.size_round_unit(content_length_integer, space=True) # prints a series of message to the observer object colony.message(handlers_map, "Request sent, awaiting response... %d %s" % (status_code, status_message)) colony.message(handlers_map, "Received headers [%s] [%s]" % (content_length_string, content_type)) colony.message(handlers_map, "Receiving data... ") # updates the message length in the current context context["message_length"] = content_length_integer
def download_package(self, address, target_directory=None, handlers_map={}): """ Downloads a package from the given url address to a target directory. @type address: String @param address: The url address of the package to download. @type target_directory: String @param target_directory: The target directory of the download. @type handlers_map: Dictionary @param handlers_map: The map of handlers for the execution events. """ try: # retrieves the current time for the initial time initial_time = time.time() # sets the target directory target_directory = target_directory or self._get_default_target_directory() # retrieves the client http plugin client_http_plugin = self.plugin.client_http_plugin # notifies the handlers about the message colony.message(handlers_map, "Get %s" % address) # creates a new set of handlers map (for http client) for # the current context _handlers_map = self._create_handlers_map(handlers_map) # retrieves the file name from the url path file_name = self.get_file_name_url(address) # creates the http client http_client = client_http_plugin.create_client({}) # opens the http client http_client.open({}) try: # fetches the url retrieving the http response http_response = http_client.fetch_url(address, handlers_map=_handlers_map) # retrieves the status code from the http response status_code = http_response.status_code # in case the status code is not valid if not status_code in VALID_STATUS_CODES: # retrieves the status message from the http response status_message = http_response.status_message # raises the invalid status code exception raise exceptions.InvalidStatusCodeException("%i - %s" % (status_code, status_message)) # retrieves the file contents from the http response file_contents = http_response.received_message finally: # closes the http client http_client.close({}) # in case there is no directory if not os.path.isdir(target_directory): # creates the directory and intermediate directories os.makedirs(target_directory) # creates the file path by joining the target directory (path) # and the file name target_path = os.path.join(target_directory, file_name) # opens a new file and creates it if necessary file = open(target_path, "wb") try: # writes the contents to the file file.write(file_contents) finally: # closes the file file.close() # retrieves the current time for the final time # and calculates the delta time final_time = time.time() delta_time = final_time - initial_time # calculates the file contents length and then # uses it to calculate the speed of the download # measured in bytes file_contents_length = len(file_contents) speed = int(float(file_contents_length) / delta_time) # converts the speed value into a size string (speed string) # for simpler scale values speed_string = colony.size_round_unit(speed, space=True) # notifies the handlers about the message colony.message(handlers_map, "Saved data as %s [%s/s]. " % (file_name, speed_string)) except Exception as exception: # prints an info message self.plugin.info( "Problem while downloading file: " + address + ", error: " + colony.legacy.UNICODE(exception) )