Skip to content

Commit

Permalink
Add -q/--quiet and --log-level flags to python jgo
Browse files Browse the repository at this point in the history
  • Loading branch information
hanslovsky committed Aug 13, 2019
1 parent fd3cb9c commit 24c6def
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions jgo/jgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,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 @@ -258,7 +260,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 @@ -268,19 +270,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')

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 @@ -379,8 +385,7 @@ def resolve_dependencies(
manage_dependencies=False,
repositories={},
shortcuts={},
verbose=0
):
verbose=0):


endpoint_strings = split_endpoint_string(endpoint_string)
Expand All @@ -399,6 +404,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 @@ -519,6 +528,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.verbose > 0:
_logger.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -554,17 +564,13 @@ 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()
return launch_java(workspace, jvm_args, main_class, *program_args, additional_jars=args.additional_jars, stdout=stdout, stderr=stderr, check=False)
except FileNotFoundError:
pass

_logger.info('First time start-up may be slow. '
'Downloaded dependencies will be cached '
'for shorter start-up times in subsequent executions.')
if not primary_endpoint.main_class:
jar_path = glob.glob(os.path.join(workspace, primary_endpoint.jar_name()).replace(Endpoint.VERSION_RELEASE, '*').replace(Endpoint.VERSION_LATEST, '*'))[0]
with zipfile.ZipFile(jar_path) as jar_file:
Expand Down

0 comments on commit 24c6def

Please sign in to comment.