# -*- coding: utf-8 -*-# vim: tabstop=4 shiftwidth=4 softtabstop=4## Copyright (C) 2017-2023 GEM Foundation## OpenQuake is free software: you can redistribute it and/or modify it# under the terms of the GNU Affero General Public License as published# by the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## OpenQuake is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.importosimportsysimporttimeimportsignalimportgetpassfromopenquake.baselibimportconfigfromopenquake.commonlibimportlogsfromopenquake.engine.utilsimportconfirmfromopenquake.serverimportdbserverfromopenquake.commands.purgeimportpurge_one,purge_all
[docs]defmain(yes=False):""" Remove all the datastores and the database of the current user """ok=yesorconfirm('Do you really want to destroy all your data? (y/n) ')ifnotok:returndbpath=os.path.realpath(os.path.expanduser(config.dbserver.file))ifnotos.path.isfile(dbpath):sys.exit('%s does not exist'%dbpath)else:dbserver.ensure_on()# start the dbserver in a subprocessuser=getpass.getuser()forcalc_idinlogs.dbcmd('get_calc_ids',user):purge_one(calc_id,user,force=True)ifos.access(dbpath,os.W_OK):# single user modepurge_all(user)# calculations in oqdata not in the dbifconfig.dbserver.host!='local':# stop the dbserver firstpid=logs.dbcmd('getpid')os.kill(pid,signal.SIGTERM)time.sleep(.5)# give time to stopassertdbserver.get_status()=='not-running'print('dbserver stopped')# remove the databaseos.remove(dbpath)print('Removed %s'%dbpath)