def safe_chdir(pth: str): "safely change directory to pth, and then go back" currdir = os.getcwd() try: if not pth: pth = currdir os.chdir(pth) __logger__.info("changing directory to %s", pth) yield pth finally: os.chdir(currdir) __logger__.info("changing directory back to %s", currdir)
def web_download_to_folder(url: str, folder: str, filename: str = None): if not filename: split = urlsplit(url) filename = split.path.split("/")[-1] filename = os.path.join(folder, filename) os.makedirs(folder, exist_ok=True) try: urlretrieve(url, filename) __logger__.info('download from %s to %s', url, filename) return filename except Exception as e: __logger__.error("failed to download", exc_info=True)
def update_default(key: str, value: str = None, is_global: bool = False, to_delete: bool = False): filename = __global_default_file__ if is_global else __local_default_file__ dic = get_global_defaults() if is_global else get_per_folder_defaults() if to_delete: if key not in dic: __logger__.warn("key %s not found in %s, ignored", key, filename) return del dic[key] else: __logger__.info("key %s updated to %s in %s", key, value, filename) dic[key] = value to_file(dic, filename)
def submit(self, cluster_alias: str): __logger__.info("submit job %s to cluster %s", self.name, cluster_alias) self.validate() client = ClusterList().load().get_client(cluster_alias) # upload for f in self.protocol.get("extras", {}).get("__sources__", []): if not f: continue local_path, remote_path = f, '{}/source/{}'.format( self.protocol["secrets"]["work_directory"], f) client.get_storage().upload(local_path=local_path, remote_path=remote_path, overwrite=True) client.get_token().rest_api_submit(self.get_config()) job_link = client.get_job_link(self.name) browser_open(job_link) return job_link
def browser_open(url: str): __logger__.info("open in browser: %s", url) try: open_new_tab(url) except Exception as e: __logger__.warn("failed to open %s due to %s", url, e)
def do_action_delete(self, args): if self.__clusters__.delete(args.cluster_alias): __logger__.info("cluster %s deleted" % args.cluster_alias) return None