def find_file(self, filename): """ Try to find file in search_path if it was specified. Helps finding files in non-CLI environments or relative to config path :param filename: file basename to find """ filename = os.path.expanduser(filename) if os.path.isfile(filename): return filename elif filename.lower().startswith("http://") or filename.lower().startswith("https://"): parsed_url = parse.urlparse(filename) downloader = request.FancyURLopener() self.log.info("Downloading %s", filename) tmp_f_name, http_msg = downloader.retrieve(filename) cd_header = http_msg.get('Content-Disposition', '') dest = cd_header.split('filename=')[-1] if cd_header and 'filename=' in cd_header else '' if not dest: dest = os.path.basename(parsed_url.path) fname, ext = os.path.splitext(dest) if dest else (parsed_url.hostname.replace(".", "_"), '.file') dest = self.create_artifact(fname, ext) self.log.debug("Moving %s to %s", tmp_f_name, dest) shutil.move(tmp_f_name, dest) return dest elif self.file_search_paths: for dirname in self.file_search_paths: location = os.path.join(dirname, os.path.basename(filename)) if os.path.isfile(location): self.log.warning("Guessed location from search paths for file %s: %s", filename, location) return location self.log.warning("Could not find file at path: %s", filename) return filename
def install(self): dest = os.path.dirname( os.path.dirname(os.path.expanduser(self.tool_path))) dest = os.path.abspath(dest) self.log.info("Will try to install Gatling into %s", dest) # download gatling downloader = request.FancyURLopener() gatling_zip_file = tempfile.NamedTemporaryFile(suffix=".zip", delete=False) self.download_link = self.download_link.format(version=self.version) self.log.info("Downloading %s", self.download_link) # TODO: check archive checksum/hash before unzip and run with ProgressBarContext() as pbar: try: downloader.retrieve(self.download_link, gatling_zip_file.name, pbar.download_callback) except BaseException as exc: self.log.error("Error while downloading %s", self.download_link) raise exc self.log.info("Unzipping %s", gatling_zip_file.name) unzip(gatling_zip_file.name, dest, 'gatling-charts-highcharts-bundle-' + self.version) gatling_zip_file.close() os.remove(gatling_zip_file.name) os.chmod(os.path.expanduser(self.tool_path), 0o755) self.log.info("Installed Gatling successfully") if not self.check_if_installed(): raise RuntimeError("Unable to run %s after installation!" % self.tool_name)
def mirrors(self): self.log.debug("Retrieving mirrors from page: %s", self.base_link) downloader = request.FancyURLopener() try: tmp_file = downloader.retrieve(self.base_link)[0] with open(tmp_file) as fds: self.page_source = fds.read() except BaseException: self.log.error("Can't fetch %s", self.base_link) mirrors = self._parse_mirrors() return (mirror for mirror in mirrors)
def install(self): with ProgressBarContext() as pbar: try: if not os.path.exists(os.path.dirname(self.tool_path)): os.makedirs(os.path.dirname(self.tool_path)) downloader = request.FancyURLopener() downloader.retrieve(self.download_link, self.tool_path, pbar.download_callback) if self.check_if_installed(): return self.tool_path else: raise RuntimeError("Unable to run %s after installation!" % self.tool_name) except BaseException as exc: raise exc
def install_with_mirrors(self, dest, suffix): self.log.info("Will try to install %s into %s", self.tool_name, dest) downloader = request.FancyURLopener() tool_dist = tempfile.NamedTemporaryFile(suffix=suffix, delete=False) # delete=False because of Windows mirrors = self.mirror_manager.mirrors() sock_timeout = socket.getdefaulttimeout() for mirror in mirrors: with ProgressBarContext() as pbar: try: socket.setdefaulttimeout(5) self.log.debug("Downloading: %s", mirror) downloader.retrieve(mirror, tool_dist.name, pbar.download_callback) return tool_dist except BaseException: self.log.error("Error while downloading %s", mirror) continue finally: socket.setdefaulttimeout(sock_timeout) raise RuntimeError("%s download failed: No more mirrors to try", self.tool_name)
def download_archive(self, links, suffix): downloader = request.FancyURLopener() tool_dist = tempfile.NamedTemporaryFile( suffix=suffix, delete=False) # delete=False because of Windows sock_timeout = socket.getdefaulttimeout() for link in links: self.log.info("Downloading: %s", link) with ProgressBarContext() as pbar: try: socket.setdefaulttimeout(5) downloader.retrieve(link, tool_dist.name, pbar.download_callback) return tool_dist except KeyboardInterrupt: raise except BaseException: self.log.error("Error while downloading %s", link) continue finally: socket.setdefaulttimeout(sock_timeout) raise RuntimeError("%s download failed: No more links to try" % self.tool_name)