| 1 |
""" |
| 2 |
The main entry point to CLI usage of GAVO code. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
# The idea here is that you expose a CLI functionality by giving, as |
| 12 |
# strings, the module and function to call. |
| 13 |
# |
| 14 |
# We also give a little startup note if we're running on a tty. |
| 15 |
# While we do this, we import api; that should take care of most |
| 16 |
# of the real startup time. |
| 17 |
|
| 18 |
import os |
| 19 |
import sys |
| 20 |
import textwrap |
| 21 |
import traceback |
| 22 |
|
| 23 |
from gavo.user import common |
| 24 |
|
| 25 |
|
| 26 |
functions = [ |
| 27 |
("admin", ("user.admin", "main")), |
| 28 |
("adql", ("protocols.adqlglue", "localquery")), |
| 29 |
("config", ("base.config", "main")), |
| 30 |
("datapack", ("protocols.datapack", "main")), |
| 31 |
("drop", ("user.dropping", "dropRD")), |
| 32 |
("dlrun", ("protocols.dlasync", "main")), |
| 33 |
("dump", ("rsc.dumping", "main")), |
| 34 |
("gencol", ("user.mkrd", "gencol")), |
| 35 |
("gendoc", ("user.docgen", "main")), |
| 36 |
("import", ("user.importing", "main")), |
| 37 |
("info", ("user.info", "main")), |
| 38 |
("limits", ("user.limits", "main")), |
| 39 |
("mkboost", ("grammars.directgrammar", "main")), |
| 40 |
("mkrd", ("user.mkrd", "main")), |
| 41 |
("publish", ("registry.publication", "main")), |
| 42 |
("purge", ("user.dropping", "dropTable")), |
| 43 |
("raise", ("user.errhandle", "bailOut")), |
| 44 |
("serve", ("user.serve", "main")), |
| 45 |
("start", ("user.mkrd", "start")), |
| 46 |
("stc", ("stc.cli", "main")), |
| 47 |
("show", ("user.show", "main")), |
| 48 |
("test", ("rscdef.regtest", "main")), |
| 49 |
("taprun", ("protocols.taprunner", "main")), |
| 50 |
("totesturl", ("rscdef.regtest", "urlToURL")), |
| 51 |
("validate", ("user.validation", "main")), |
| 52 |
("upgrade", ("user.upgrade", "main")), |
| 53 |
("uwsrun", ("protocols.useruws", "main")), |
| 54 |
# init is special cased, but we want it in here for help generation |
| 55 |
("init", ("user.initdachs", "main")), |
| 56 |
] |
| 57 |
|
| 58 |
|
| 59 |
def _enablePDB(): |
| 60 |
# This can't be a callback to the --enable-pdb option since it needs |
| 61 |
# errhandle, and we only want to import this after the command line |
| 62 |
# is parsed |
| 63 |
import pdb |
| 64 |
def enterPdb(type, value, tb): |
| 65 |
traceback.print_exception(type, value, tb) |
| 66 |
pdb.pm() |
| 67 |
sys.excepthook = enterPdb |
| 68 |
from gavo.base import events |
| 69 |
events.PDB_ENABLED = True |
| 70 |
|
| 71 |
|
| 72 |
def _enableDebug(*args): |
| 73 |
from gavo import base |
| 74 |
base.DEBUG = True |
| 75 |
|
| 76 |
|
| 77 |
def _printVersion(*args): |
| 78 |
from gavo import base |
| 79 |
from gavo.user import upgrade |
| 80 |
print("Software (%s) Schema (%s/%s)"%( |
| 81 |
base.getVersion(), |
| 82 |
upgrade.CURRENT_SCHEMAVERSION, |
| 83 |
upgrade.getDBSchemaVersion())) |
| 84 |
sys.exit(0) |
| 85 |
|
| 86 |
|
| 87 |
def _parseCLArgs(): |
| 88 |
"""parses the command line and returns instructions on how to go on. |
| 89 |
|
| 90 |
As a side effect, sys.argv is manipulated such that the program |
| 91 |
called thinks it was execd in the first place. |
| 92 |
""" |
| 93 |
from optparse import OptionParser |
| 94 |
sels = [n for n,x in functions] |
| 95 |
sels.sort() |
| 96 |
parser = OptionParser(usage="%prog {<global option>} <func>" |
| 97 |
" {<func option>} {<func argument>}\n"+ |
| 98 |
textwrap.fill("<func> is a unique prefix into {%s}"%(", ".join(sels)), |
| 99 |
initial_indent='', subsequent_indent=' '), |
| 100 |
description="Try %prog <func> --help for function-specific help") |
| 101 |
parser.disable_interspersed_args() |
| 102 |
parser.add_option("--traceback", help="print a traceback on all errors.", |
| 103 |
action="store_true", dest="alwaysTracebacks") |
| 104 |
parser.add_option("--hints", help="if there are hints on an error, display" |
| 105 |
" them", action="store_true", dest="showHints") |
| 106 |
parser.add_option("--enable-pdb", help="run pdb on all errors.", |
| 107 |
action="store_true", dest="enablePDB") |
| 108 |
parser.add_option("--disable-spew", help='Ignored.', |
| 109 |
action="store_true", dest="disableSpew") |
| 110 |
parser.add_option("--profile-to", metavar="PROFILEPATH", |
| 111 |
help="enable profiling and write a profile to PROFILEPATH", |
| 112 |
action="store", dest="profilePath", default=None) |
| 113 |
parser.add_option("--suppress-log", help="Do not log exceptions and such" |
| 114 |
" to the dachs-specific log files", action="store_true", |
| 115 |
dest="suppressLog") |
| 116 |
parser.add_option("--debug", help="Produce debug info as appropirate.", |
| 117 |
action="callback", callback=_enableDebug) |
| 118 |
parser.add_option("--version", help="Write software version to stdout" |
| 119 |
" and exit", action="callback", callback=_printVersion) |
| 120 |
parser.add_option("-U", "--ui", help="use UI to show what is going on;" |
| 121 |
" try --ui=help to see available interfaces.", |
| 122 |
dest="uiName", action="store", type="str", default="module-dependent", |
| 123 |
metavar="UI") |
| 124 |
|
| 125 |
opts, args = parser.parse_args() |
| 126 |
if len(args)<1: |
| 127 |
parser.print_help(file=sys.stderr) |
| 128 |
sys.exit(2) |
| 129 |
|
| 130 |
module, funcName = common.getMatchingFunction(args[0], functions, parser) |
| 131 |
parser.destroy() |
| 132 |
args[0] = "dachs "+args[0] |
| 133 |
sys.argv = args |
| 134 |
return opts, module, funcName |
| 135 |
|
| 136 |
|
| 137 |
def main(): |
| 138 |
|
| 139 |
# we want to preserve group-writability in all our operations; hence |
| 140 |
# this prominent place for overriding a user decision... |
| 141 |
os.umask(0o02) |
| 142 |
|
| 143 |
# Special case: initial setup, no api working yet, and if things fail, |
| 144 |
# I cannot rely on normal DaCHS error reporting, which needs significant |
| 145 |
# parts of DaCHS. |
| 146 |
if len(sys.argv)>1 and sys.argv[1]=="init": |
| 147 |
try: |
| 148 |
del sys.argv[1] |
| 149 |
from gavo.user import initdachs |
| 150 |
sys.exit(initdachs.main()) |
| 151 |
except Exception as exc: |
| 152 |
traceback.print_exc() |
| 153 |
sys.stderr.write(f"Ouch -- Exception escaped from initdachs: {exc}\n") |
| 154 |
sys.exit(1) |
| 155 |
|
| 156 |
# Normal case: try executing what the user asked for and report |
| 157 |
# errors centrally. |
| 158 |
try: |
| 159 |
opts, module, funcName = _parseCLArgs() |
| 160 |
from gavo import base |
| 161 |
from gavo import utils |
| 162 |
from gavo.user import errhandle |
| 163 |
from gavo.user import plainui |
| 164 |
from gavo.user import useless |
| 165 |
|
| 166 |
interfaces = { |
| 167 |
"deluge": useless.DelugeUI, |
| 168 |
"null": useless.NullUI, |
| 169 |
"stingy": plainui.StingyPlainUI, |
| 170 |
"semistingy": plainui.SemiStingyPlainUI, |
| 171 |
"plain": plainui.PlainUI, |
| 172 |
} |
| 173 |
|
| 174 |
if not (opts.suppressLog or os.environ.get("GAVO_LOG")=="no"): |
| 175 |
from gavo.user import logui |
| 176 |
logui.LoggingUI(base.ui) |
| 177 |
|
| 178 |
if opts.uiName=="module-dependent": |
| 179 |
opts.uiName = {"registry.publication": "semistingy", |
| 180 |
"user.serve": "null", |
| 181 |
"user.validation": "null", |
| 182 |
}.get(module, "plain") |
| 183 |
if opts.uiName not in interfaces: |
| 184 |
raise base.ReportableError("UI %s does not exist. Choose one of" |
| 185 |
" %s"%(opts.uiName, ", ".join(interfaces))) |
| 186 |
interfaces[opts.uiName](base.ui) |
| 187 |
|
| 188 |
if opts.enablePDB: |
| 189 |
_enablePDB() |
| 190 |
funcToRun = utils.loadInternalObject(module, funcName) |
| 191 |
|
| 192 |
if opts.profilePath: |
| 193 |
import cProfile |
| 194 |
cProfile.runctx("funcToRun()", globals(), locals(), opts.profilePath) |
| 195 |
return |
| 196 |
except Exception: |
| 197 |
from gavo.user import errhandle |
| 198 |
sys.exit(errhandle.raiseAndCatch()) |
| 199 |
|
| 200 |
try: |
| 201 |
funcToRun() |
| 202 |
except Exception: |
| 203 |
if opts.alwaysTracebacks: |
| 204 |
traceback.print_exc() |
| 205 |
sys.exit(errhandle.raiseAndCatch(opts)) |
| 206 |
|
| 207 |
|
| 208 |
if __name__=="__main__": |
| 209 |
main() |