def get_configfile_user(): """return the full path for the user configuration file. If doesn't exist, create it for the user. """ from helpme.defaults import HELPME_CLIENT_SECRETS # The inital file has a funny username if not os.path.exists(HELPME_CLIENT_SECRETS): bot.debug("Generating settings file at %s" % HELPME_CLIENT_SECRETS) config_dir = os.path.dirname(HELPME_CLIENT_SECRETS) # The configuration directory might be needed for different clients if not os.path.exists(config_dir): mkdir_p(config_dir) name = RobotNamer().generate() # Generate the user config config = configparser.ConfigParser() config["DEFAULT"] = {"Alias": name} write_config(HELPME_CLIENT_SECRETS, config) return HELPME_CLIENT_SECRETS
def get(url, headers=None, token=None, data=None, return_json=True): """get will use requests to get a particular url """ bot.debug("GET %s" % url) return call( url, headers=headers, func=requests.get, data=data, return_json=return_json )
def post(url, data=None, return_json=True): """post will use requests to get a particular url """ bot.debug("POST %s" % url) return call( url, headers=headers, func=requests.post, data=data, return_json=return_json )
def delete(self, url, headers=None, return_json=True, default_headers=True): """delete request, use with caution """ bot.debug("DELETE %s" % url) return self._call( url, headers=headers, func=requests.delete, return_json=return_json, default_headers=default_headers, )
def stream(url, headers, stream_to=None, retry=True): """stream is a get that will stream to file_name. Since this is a worker task, it differs from the client provided version in that it requires headers. """ bot.debug("GET %s" % url) if DISABLE_SSL_CHECK is True: bot.warning("Verify of certificates disabled! ::TESTING USE ONLY::") # Ensure headers are present, update if not response = requests.get( url, headers=headers, verify=not DISABLE_SSL_CHECK, stream=True ) # Deal with token if necessary if response.status_code == 401 and retry is True: headers = update_token(response, headers) return stream(url, headers, stream_to, retry=False) if response.status_code == 200: # Keep user updated with Progress Bar content_size = None if "Content-Length" in response.headers: progress = 0 content_size = int(response.headers["Content-Length"]) bot.show_progress(progress, content_size, length=35) chunk_size = 1 << 20 with open(stream_to, "wb") as filey: for chunk in response.iter_content(chunk_size=chunk_size): filey.write(chunk) if content_size is not None: progress += chunk_size bot.show_progress( iteration=progress, total=content_size, length=35, carriage_return=False, ) # Newline to finish download sys.stdout.write("\n") return stream_to bot.error("Problem with stream, response %s" % (response.status_code)) sys.exit(1)
def update_headers(self, fields=None): """update headers with a token & other fields """ do_reset = True if hasattr(self, "headers"): if self.headers is not None: do_reset = False if do_reset is True: self._reset_headers() if fields is not None: for key, value in fields.items(): self.headers[key] = value header_names = ",".join(list(self.headers.keys())) bot.debug("Headers found: %s" % header_names)
def put(self, url, headers=None, data=None, return_json=True, default_headers=True): """put request """ bot.debug("PUT %s" % url) return self._call( url, headers=headers, func=requests.put, data=data, return_json=return_json, default_headers=default_headers, )
def stream(self, url, headers=None, stream_to=None, retry=True, default_headers=True): """ stream is a get that will stream to file_name. This stream is intended to take a url and (optionally) a set of headers and file to stream to, and will generate a response with requests.get. Parameters ========== url: the url to do a requests.get to headers: any updated headers to use for the requets stream_to: the file to stream to retry: should the client retry? (intended for use after token refresh) by default we retry once after token refresh, then fail. """ bot.debug("GET %s" % url) # Ensure headers are present, update if not if headers == None: if self.headers is None: self._reset_headers() headers = self.headers.copy() response = requests.get(url, headers=headers, verify=self._verify(), stream=True) # Deal with token if necessary if response.status_code == 401 and retry is True: if hasattr(self, "_update_token"): self._update_token(response) return self.stream(url, headers, stream_to, retry=False) if response.status_code == 200: return self._stream(response, stream_to=stream_to) bot.error("Problem with stream, response %s" % (response.status_code)) sys.exit(1)
def post(self, url, headers=None, data=None, return_json=True, default_headers=True): """post will use requests to get a particular url """ bot.debug("POST %s" % url) return self._call( url, headers=headers, func=requests.post, data=data, return_json=return_json, default_headers=default_headers, )
def get( self, url, headers=None, token=None, data=None, return_json=True, default_headers=True, quiet=False, ): """get will use requests to get a particular url """ bot.debug("GET %s" % url) return self._call( url, headers=headers, func=requests.get, data=data, return_json=return_json, default_headers=default_headers, quiet=quiet, )
def collect(self, step, content): """given a name of a configuration key and the provided content, collect the required metadata from the user. Parameters ========== headless: run the collection headless (no prompts) step: the key in the configuration. Can be one of: user_message_<name> runtime_arg_<name> record_asciinema record_environment record_system user_prompt_<name> content: the default value or boolean to indicate doing the step. """ # Option 1: The step is just a message to print to the user if step.startswith("user_message"): print(content) # Option 2: The step is to collect a user prompt (if not at runtime) elif step.startswith("user_prompt"): self.collect_argument(step, content) # Option 3: The step is to record an asciinema! elif step == "record_asciinema": self.record_asciinema() # Option 4: Record the user environment elif step == "record_environment": self.record_environment() elif step == "record_system": self.record_system() bot.debug(self.data)
def end(self): self.end_time = time.time() self.runtime = self.runtime = self.end_time - self.start_time bot.debug("Ending multiprocess, runtime: %s sec" % (self.runtime))
def start(self): bot.debug("Starting multiprocess") self.start_time = time.time()
def __init__(self, workers=None): if workers is None: workers = HELPME_WORKERS self.workers = workers bot.debug("Using %s workers for multiprocess." % (self.workers))
def head(self, url): """head request, typically used for status code retrieval, etc. """ bot.debug("HEAD %s" % url) return self._call(url, func=requests.head)