Skip to content

Commit

Permalink
Merge pull request #48 from scijava/log-bootstrap
Browse files Browse the repository at this point in the history
Log first-time start up and warn about potentially slow start-up time
  • Loading branch information
ctrueden authored Aug 13, 2019
2 parents 7788370 + 2f147d2 commit 20553f3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
9 changes: 8 additions & 1 deletion jgo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# Define some useful functions.

notice() { test $quiet || echo "$@"; }
info() { test $verbose && echo "[INFO] $@"; }
err() { echo "$@" 1>&2; }
die() { err "$@"; exit 1; }
Expand Down Expand Up @@ -180,6 +181,9 @@ do
updateMaven=1
updateCache=1
;;
-q)
quiet=1
;;
-*)
jvm_args+=("$1")
;;
Expand Down Expand Up @@ -244,12 +248,13 @@ do
v="RELEASE"
;;
*)
echo "Usage: jgo [-v] [-u] [-U] [-m] <jvm-args> <endpoint> <main-args>"
echo "Usage: jgo [-v] [-u] [-U] [-m] [-q] <jvm-args> <endpoint> <main-args>"
echo
echo " -v : verbose mode flag"
echo " -u : update/regenerate cached environment"
echo " -U : force update from remote Maven repositories (implies -u)"
echo " -m : use endpoints for dependency management (see README)"
echo " -q : quiet mode flag to suppress regular output"
echo " <jvm-args> : any list of arguments to the JVM"
echo " <endpoint> : the artifact(s) + main class to execute"
echo " <main-args> : any list of arguments to the main class"
Expand Down Expand Up @@ -310,6 +315,8 @@ then
exit $?
fi

notice 'First time start-up may be slow. Downloaded dependencies will be cached for shorter start-up times in subsequent executions.'

# Synthesize a dummy Maven project.

for repository in "${repositories[@]}"
Expand Down
27 changes: 18 additions & 9 deletions jgo/jgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ def find_endpoint(argv, shortcuts={}):
indices.append(index)
return -1 if len(indices) == 0 else indices[-1]

def jgo_parser():
_default_log_levels = ('NOTSET', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL', 'FATAL', 'TRACE')

def jgo_parser(log_levels = _default_log_levels):

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

parser = argparse.ArgumentParser(
description = 'Run Java main class from maven coordinates.',
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]',
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]',
epilog = epilog,
formatter_class = argparse.RawTextHelpFormatter
)
Expand All @@ -264,19 +266,23 @@ def jgo_parser():
parser.add_argument('-m', '--manage-dependencies', action='store_true', help='use endpoints for dependency management (see "Details" below)')
parser.add_argument('-r', '--repository', nargs='+', help='Add additional maven repository (key=url format)', default=[], required=False)
parser.add_argument('-a', '--additional-jars', nargs='+', help='Add additional jars to classpath', default=[], required=False)
parser.add_argument('-q', '--quiet', action='store_true', required=False, help='Suppress jgo output, including logging')
parser.add_argument( '--additional-endpoints', nargs='+', help='Add additional endpoints', default=[], required=False)
parser.add_argument('--ignore-jgorc', action='store_true', help='Ignore ~/.jgorc')
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'))
parser.add_argument('--log-level', default=None, type=str, help='Set log level', choices=log_levels)

return parser

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

LOG_FORMAT = '%(levelname)s %(asctime)s: %(message)s'
logging.basicConfig(
level = logging.INFO,
# datefmt = '%Y-%m-%d - %H:%M:%S',
format = LOG_FORMAT)

if not ('-q' in argv or '--quiet' in argv):
logging.basicConfig(
level = logging.INFO,
# datefmt = '%Y-%m-%d - %H:%M:%S',
format = LOG_FORMAT)

parser = jgo_parser()

Expand Down Expand Up @@ -390,8 +396,7 @@ def resolve_dependencies(
manage_dependencies=False,
repositories={},
shortcuts={},
verbose=0
):
verbose=0):


endpoint_strings = split_endpoint_string(endpoint_string)
Expand All @@ -410,6 +415,10 @@ def resolve_dependencies(
if not update_cache:
return primary_endpoint, workspace

_logger.info('First time start-up may be slow. '
'Downloaded dependencies will be cached '
'for shorter start-up times in subsequent executions.')

if update_cache:
shutil.rmtree(workspace, True)

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

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

main_class_file = os.path.join(workspace, primary_endpoint.main_class, 'mainClass') if primary_endpoint.main_class else os.path.join(workspace, 'mainClass')

try:
with open(main_class_file, 'r') as f:
main_class = f.readline()
Expand Down

0 comments on commit 20553f3

Please sign in to comment.