def _write_last_processed(self, hist): """Write down the history tuple of the last context processed.""" log.verbose("Saving last processed:", repr(hist)) log.verbose("Storing last processed state at", repr(self.last_processed_path)) utils.ensure_dir_exists(self.last_processed_path) with open(self.last_processed_path, "w+") as last: last.write(str(hist))
def locate_dir(instrument, mode=None): """Locate the instrument specific directory for a reference file.""" if mode is None: mode = config.get_crds_ref_subdir_mode(observatory="hst") else: config.check_crds_ref_subdir_mode(mode) crds_refpath = config.get_crds_refpath("hst") prefix = get_env_prefix(instrument) if mode == "legacy": # Locate cached files at the appropriate CDBS-style iref$ locations try: rootdir = os.environ[prefix] except KeyError: try: rootdir = os.environ[prefix[:-1]] except KeyError: raise KeyError("Reference location not defined for " + repr(instrument) + ". Did you configure " + repr(prefix) + "?") elif mode == "instrument": # use simple names inside CRDS cache. rootdir = os.path.join(crds_refpath, instrument) refdir = os.path.join(crds_refpath, prefix[:-1]) if not os.path.exists(refdir): if config.writable_cache_or_verbose("Skipping making instrument directory link for", repr(instrument)): log.verbose("Creating legacy cache link", repr(refdir), "-->", repr(rootdir)) utils.ensure_dir_exists(rootdir + "/locate_dir.fits") os.symlink(rootdir, refdir) elif mode == "flat": # use original flat cache structure, all instruments in same directory. rootdir = crds_refpath else: raise ValueError("Unhandled reference file location mode " + repr(mode)) return rootdir
def locate_dir(instrument, mode=None): """Locate the instrument specific directory for a reference file.""" if mode is None: mode = config.get_crds_ref_subdir_mode(observatory="hst") else: config.check_crds_ref_subdir_mode(mode) crds_refpath = config.get_crds_refpath("hst") prefix = get_env_prefix(instrument) if mode == "legacy": # Locate cached files at the appropriate CDBS-style iref$ locations try: rootdir = os.environ[prefix] except KeyError: try: rootdir = os.environ[prefix[:-1]] except KeyError: raise KeyError("Reference location not defined for " + repr(instrument) + ". Did you configure " + repr(prefix) + "?") elif mode == "instrument": # use simple names inside CRDS cache. rootdir = os.path.join(crds_refpath, instrument) refdir = os.path.join(crds_refpath, prefix[:-1]) if not os.path.exists(refdir): if config.writable_cache_or_verbose( "Skipping making instrument directory link for", repr(instrument)): log.verbose("Creating legacy cache link", repr(refdir), "-->", repr(rootdir)) utils.ensure_dir_exists(rootdir + "/locate_dir.fits") os.symlink(rootdir, refdir) elif mode == "flat": # use original flat cache structure, all instruments in same directory. rootdir = crds_refpath else: raise ValueError("Unhandled reference file location mode " + repr(mode)) return rootdir
def get_sqlite_db(observatory): """Download the CRDS database as a SQLite database.""" assert not config.get_cache_readonly(), "Readonly cache, updating the SQLite database cannot be done.""" encoded_compressed_data = S.get_sqlite_db(observatory) data = zlib.decompress(base64.b64decode(encoded_compressed_data)) path = config.get_sqlite3_db_path(observatory) utils.ensure_dir_exists(path) with open(path, "wb+") as db_out: db_out.write(data) return path
def locate_dir(instrument, mode=None): """Locate the instrument specific directory for a reference file.""" if mode is None: mode = config.get_crds_ref_subdir_mode(observatory="jwst") else: config.check_crds_ref_subdir_mode(mode) crds_refpath = config.get_crds_refpath("jwst") if mode == "instrument": # use simple names inside CRDS cache. rootdir = os.path.join(crds_refpath, instrument.lower()) if not os.path.exists(rootdir): utils.ensure_dir_exists(rootdir + "/locate_dir.fits") elif mode == "flat": # use original flat cache structure, all instruments in same directory. rootdir = crds_refpath else: raise ValueError("Unhandled reference file location mode " + repr(mode)) return rootdir
def download(self, name, localpath): """Download a single file.""" # This code is complicated by the desire to blow away failed downloads. For the specific # case of KeyboardInterrupt, the file needs to be blown away, but the interrupt should not # be re-characterized so it is still un-trapped elsewhere under normal idioms which try *not* # to trap KeyboardInterrupt. assert not config.get_cache_readonly(), "Readonly cache, cannot download files " + repr(name) try: utils.ensure_dir_exists(localpath) return proxy.apply_with_retries(self.download_core, name, localpath) except Exception as exc: self.remove_file(localpath) raise CrdsDownloadError("Error fetching data for " + srepr(name) + " from context " + srepr(self.pipeline_context) + " at CRDS server " + srepr(get_crds_server()) + " with mode " + srepr(config.get_download_mode()) + " : " + str(exc)) except: # mainly for control-c, catch it and throw it. self.remove_file(localpath) raise