8000 Merge pull request #48 from scijava/log-bootstrap · scijava/jgo@20553f3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 20553f3

Browse files
authored
Merge pull request #48 from scijava/log-bootstrap
Log first-time start up and warn about potentially slow start-up time
2 parents 7788370 + 2f147d2 commit 20553f3

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

jgo.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# Define some useful functions.
1515

16+
notice() { test $quiet || echo "$@"; }
1617
info() { test $verbose && echo "[INFO] $@"; }
1718
err() { echo "$@" 1>&2; }
1819
die() { err "$@"; exit 1; }
@@ -180,6 +181,9 @@ do
180181
updateMaven=1
181182
updateCache=1
182183
;;
184+
-q)
185+
quiet=1
186+
;;
183187
-*)
184188
jvm_args+=("$1")
185189
;;
@@ -244,12 +248,13 @@ do
244248
v="RELEASE"
245249
;;
246250
*)
247-
echo "Usage: jgo [-v] [-u] [-U] [-m] <jvm-args> <endpoint> <main-args>"
251+
echo "Usage: jgo [-v] [-u] [-U] [-m] [-q] <jvm-args> <endpoint> <main-args>"
248252
echo
249253
echo " -v : verbose mode flag"
250254
echo " -u : update/regenerate cached environment"
251255
echo " -U : force update from remote Maven repositories (implies -u)"
252256
echo " -m : use endpoints for dependency management (see README)"
257+
echo " -q : quiet mode flag to suppress regular output"
253258
echo " <jvm-args> : any list of arguments to the JVM"
254259
echo " <endpoint> : the artifact(s) + main class to execute"
255260
echo " <main-args> : any list of arguments to the main class"
@@ -310,6 +315,8 @@ then
310315
exit $?
311316
fi
312317

318+
notice 'First time start-up may be slow. Downloaded dependencies will be cached for shorter start-up times in subsequent executions.'
319+
313320
# Synthesize a dummy Maven project.
314321

315322
for repository in "${repositories[@]}"

jgo/jgo.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ def find_endpoint(argv, shortcuts={}):
235235
indices.append(index)
236236
return -1 if len(indices) == 0 else indices[-1]
237237

238-
def jgo_parser():
238+
_default_log_levels = ('NOTSET', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL', 'FATAL', 'TRACE')
239+
240+
def jgo_parser(log_levels = _default_log_levels):
239241

240242
epilog='''
241243
The endpoint should have one of the following formats:
@@ -254,7 +256,7 @@ def jgo_parser():
254256

255257
parser = argparse.ArgumentParser(
256258
description = 'Run Java main class from maven coordinates.',
257-
usage = '%(prog)s [-v] [-u] [-U] [-m] [--ignore-jgorc] [--link-type type] [--additional-jars jar [jar ...]] [--additional-endpoints endpoint [endpoint ...]] [JVM_OPTIONS [JVM_OPTIONS ...]] <endpoint> [main-args]',
259+
usage = '%(prog)s [-v] [-u] [-U] [-m] [-q] [--log-level] [--ignore-jgorc] [--link-type type] [--additional-jars jar [jar ...]] [--additional-endpoints endpoint [endpoint ...]] [JVM_OPTIONS [JVM_OPTIONS ...]] <endpoint> [main-args]',
258260
epilog = epilog,
259261
formatter_class = argparse.RawTextHelpFormatter
260262
)
@@ -264,19 +266,23 @@ def jgo_parser():
264266
parser.add_argument('-m', '--manage-dependencies', action='store_true', help='use endpoints for dependency management (see "Details" below)')
265267
parser.add_argument('-r', '--repository', nargs='+', help='Add additional maven repository (key=url format)', default=[], required=False)
266268
parser.add_argument('-a', '--additional-jars', nargs='+', help='Add additional jars to classpath', default=[], required=False)
269+
parser.add_argument('-q', '--quiet', action='store_true', required=False, help='Suppress jgo output, including logging')
267270
parser.add_argument( '--additional-endpoints', nargs='+', help='Add additional endpoints', default=[], required=False)
268271
parser.add_argument('--ignore-jgorc', action='store_true', help='Ignore ~/.jgorc')
269272
parser.add_argument('--link-type', default=None, type=str, help='How to link from local maven repository into jgo cache. Defaults to the `links\' setting in ~/.jgorc or \'auto\' if not specified.', choices=('hard', 'soft', 'copy', 'auto'))
273+
parser.add_argument('--log-level', default=None, type=str, help='Set log level', choices=log_levels)
270274

271275
return parser
272276

273277
def _jgo_main(argv=sys.argv[1:], stdout=None, stderr=None):
274278

275279
LOG_FORMAT = '%(levelname)s %(asctime)s: %(message)s'
276-
logging.basicConfig(
277-
level = logging.INFO,
278-
# datefmt = '%Y-%m-%d - %H:%M:%S',
279-
format = LOG_FORMAT)
280+
281+
if not ('-q' in argv or '--quiet' in argv):
282+
logging.basicConfig(
283+
level = logging.INFO,
284+
# datefmt = '%Y-%m-%d - %H:%M:%S',
285+
format = LOG_FORMAT)
280286

281287
parser = jgo_parser()
282288

@@ -390,8 +396,7 @@ def resolve_dependencies(
390396
manage_dependencies=False,
391397
repositories={},
392398
shortcuts={},
393-
verbose=0
394-
):
399+
verbose=0):
395400

396401

397402
endpoint_strings = split_endpoint_string(endpoint_string)
@@ -410,6 +415,10 @@ def resolve_dependencies(
410415
if not update_cache:
411416
return primary_endpoint, workspace
412417

418+
_logger.info('First time start-up may be slow. '
419+
'Downloaded dependencies will be cached '
420+
'for shorter start-up times in subsequent executions.')
421+
413422
if update_cache:
414423
shutil.rmtree(workspace, True)
415424

@@ -530,6 +539,7 @@ def run(parser, argv=sys.argv[1:], stdout=None, stderr=None):
530539
args, unknown = parser.parse_known_args(argv[:endpoint_index])
531540
jvm_args = unknown if unknown else []
532541
program_args = [] if endpoint_index == -1 else argv[endpoint_index+1:]
542+
if args.log_level: logging.getLogger().setLevel(logging.getLevelName(args.log_level))
533543

534544
if args.additional_jars is not None and len(args.additional_jars) > 0:
535545
_logger.warning('The -a, --additional-jars option has been deprecated and will be removed in the future. '
@@ -570,7 +580,6 @@ def run(parser, argv=sys.argv[1:], stdout=None, stderr=None):
570580
link_type = link_type)
571581

572582
main_class_file = os.path.join(workspace, primary_endpoint.main_class, 'mainClass') if primary_endpoint.main_class else os.path.join(workspace, 'mainClass')
573-
574583
try:
575584
with open(main_class_file, 'r') as f:
576585
main_class = f.readline()

0 commit comments

Comments
 (0)
0