def retrieve_object(obj_id, obj_type, sync_if_missing=True, verbose=False, force_update=False): """ Retrieve an object (Experiment, Simulation) in the local database based on its id. Can call a sync if missing if the flag is true :param obj_id: Id of the object to retrieve :param obj_type: Type of the object to retrieve :param sync_if_missing: Should we try to sync if not present? :return: The experiment found #TODO: SHould also support suites """ typename = obj_type.__name__ if not obj_id: raise ValueError("Trying to retrieve an object (%s) without providing an ID" % typename) # Try a hit in the DB if obj_type == Experiment: obj = DataStore.get_experiment(obj_id) elif obj_type == Simulation: obj = DataStore.get_simulation(obj_id) if obj: # If we have an experiment and we want to force the update -> delete it if not force_update: return obj else: if obj_type == Experiment: DataStore.delete_experiment(obj_id) elif obj_type == Simulation: DataStore.delete_simulation(obj_id) if not sync_if_missing: raise Exception('%s %s not found in the local database and sync disabled.' % (typename, obj_id)) logger.info('%s with id %s not found in local database, trying sync.' % (typename, obj_id)) with SetupParser.TemporarySetup(temporary_block='HPC') as sp: endpoint = sp.get('server_endpoint') if obj_type == Experiment: obj = COMPS_experiment_to_local_db(obj_id, endpoint, verbose) elif obj_type == Simulation: obj =None if obj: return obj raise Exception("%s '%s' could not be retrieved." % (typename, obj_id))
def retrieve_simulation(sim_id, sync_if_missing=True, verbose=False, force_update=False): """ Retrieve a simulation in the local database based on its id. Can call a sync if missing if the flag is true. :param sim_id: Id of the simulation to retrieve :param sync_if_missing: Should we try to sync if not present? :return: The simulation found """ from simtools.Utilities.COMPSUtilities import get_simulation_by_id if not sim_id: raise Exception("Trying to retrieve a simulation without providing an simulation ID") from uuid import UUID if isinstance(sim_id, UUID): sim_id = str(sim_id) # If we dont force the update -> look first in the DB sim = DataStore.get_simulation(sim_id) if sim: # If we have a simulation and we want to force the update -> delete it if not force_update: return sim else: DataStore.delete_simulation(sim) if not sync_if_missing: raise Exception('Simulation %s not found in the local database and sync disabled.' % sim_id) logger.info('Simulation with id %s not found in local database, trying sync.' % sim_id) csim = get_simulation_by_id(sim_id) if csim: with SetupParser.TemporarySetup(temporary_block='HPC') as sp: endpoint = sp.get('server_endpoint') COMPS_experiment_to_local_db(csim.experiment_id, endpoint, verbose) sim = DataStore.get_simulation(sim_id) if sim: return sim raise Exception("Simulation '%s' could not be retrieved." % sim_id)