# -*- coding: utf-8 -*-# vim: tabstop=4 shiftwidth=4 softtabstop=4## Copyright (C) 2016-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/>.importosimportsysimportsubprocessimportwebbrowserfromopenquake.baselibimportconfig,generalfromopenquake.serverimportdbserver,dbfromopenquake.server.utilsimportcheck_webserver_runningcommands=['start','migrate','createsuperuser','collectstatic']
[docs]defrundjango(subcmd,hostport=None,skip_browser=False):args=[sys.executable,'-m','openquake.server.manage',subcmd]ifsubcmd=='runserver':# the reload functionality of the Django development server interferes# with SIGCHLD and causes zombies, thus it is disabledargs.append('--noreload')ifhostport:args.append(hostport)p=subprocess.Popen(args)ifsubcmd=='runserver'andnotskip_browser:url='http://'+hostportifcheck_webserver_running(url):webbrowser.open(url)p.wait()ifp.returncode!=0:sys.exit(p.returncode)
[docs]defmain(cmd,hostport='127.0.0.1:8800',skip_browser:bool=False):""" start the webui server in foreground or perform other operation on the django application """dbpath=os.path.realpath(os.path.expanduser(config.dbserver.file))ifos.path.isfile(dbpath)andnotos.access(dbpath,os.W_OK):sys.exit('This command must be run by the proper user: ''see the documentation for details')ifcmd=='start':general.check_dependencies()dbserver.ensure_on()# start the dbserver in a subprocess# reset any computation left in the 'executing' statedb.actions.reset_is_running(dbserver.db)print('Starting, using version %s'%general.engine_version())rundjango('runserver',hostport,skip_browser)elifcmdincommands:rundjango(cmd)
main.cmd=dict(help='webui command',choices=commands)main.hostport='a string of the form <hostname:port>'main.skip_browser='do not automatically open the browser'