Skip to content

Commit

Permalink
Add options for disabling logger and progress bars
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasjacobsen committed Oct 24, 2023
1 parent 92040d3 commit bc34173
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/ramble/ramble/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
'config': {
'debug': False,
'disable_passthrough': False,
'disable_progress_bar': False,
'connect_timeout': 10,
'verify_ssl': True,
'checksum': True,
Expand Down
15 changes: 15 additions & 0 deletions lib/ramble/ramble/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ def make_argument_parser(**kwargs):
parser.add_argument(
'--disable-passthrough', action='store_true',
help="disable passthrough of expansion variables for debugging")
parser.add_argument(
'-N', '--disable-logger', action='store_true',
help="disable the ramble logger. All output will be printed to stdout.")
parser.add_argument(
'-P', '--disable-progress-bar', action='store_true',
help="disable the progress bars while setting up experiments.")

parser.add_argument(
'--timestamp', action='store_true',
help="Add a timestamp to tty output")
Expand Down Expand Up @@ -515,6 +522,14 @@ def setup_main_options(args):
if args.disable_passthrough:
ramble.config.set('config:disable_passthrough', True, scope='command_line')

if args.disable_logger:
ramble.config.set('config:disable_logger', True, scope='command_line')

ramble.util.logger.logger.enabled = not ramble.config.get('config:disable_logger', False)

if args.disable_progress_bar:
ramble.config.set('config:disable_progress_bar', True, scope='command_line')

if args.mock:
import spack.util.spack_yaml as syaml

Expand Down
31 changes: 19 additions & 12 deletions lib/ramble/ramble/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
import spack.util.spack_json as sjson
from spack.util.executable import which

try:
import tqdm
except ModuleNotFoundError:
ramble.util.logger.logger.die(
'Module `tqdm` is not found. Ensure requirements.txt are installed.'
)
if not ramble.config.get('config:disable_progress_bar', False):
try:
import tqdm
except ModuleNotFoundError:
ramble.util.logger.logger.die(
'Module `tqdm` is not found. Ensure requirements.txt are installed.'
)


class Pipeline(object):
Expand Down Expand Up @@ -147,17 +148,23 @@ def _execute(self):

phase_list = app_inst.get_pipeline_phases(self.name, self.filters.phases)

progress = tqdm.tqdm(total=len(phase_list), leave=False)
disable_progress = ramble.config.get('config:disable_progress_bar', False)
if not disable_progress:
progress = tqdm.tqdm(total=len(phase_list), leave=True,
ascii=' >-', bar_format='{l_bar}{bar}{elapsed_s}')
for phase in phase_list:
progress.set_description(f'Processing phase {phase}')
if not disable_progress:
progress.set_description(f'Processing phase {phase}')
app_inst.run_phase(phase, self.workspace)
progress.update()
progress.close()
if not disable_progress:
progress.update()
if not disable_progress:
progress.set_description('Experiment complete')
progress.close()

ramble.util.logger.logger.remove_log()
ramble.util.logger.logger.all_msg(
' Experiment log file closed.'
f'Returning to log file: {ramble.util.logger.logger.active_log()}'
f' Returning to log file: {ramble.util.logger.logger.active_log()}'
)
count += 1

Expand Down
16 changes: 16 additions & 0 deletions lib/ramble/ramble/schema/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@
}
}

properties['config']['disable_passthrough'] = {
'type': 'boolean',
'default': False
}

properties['config']['disable_progress_bar'] = {
'type': 'boolean',
'default': False
}

properties['config']['disable_logger'] = {
'type': 'boolean',
'default': False
}


#: Full schema with metadata
schema = {
'$schema': 'http://json-schema.org/schema#',
Expand Down
18 changes: 14 additions & 4 deletions lib/ramble/ramble/util/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ class Logger(object):
printed to all log files instead of only one.
"""
def __init__(self):
"""Construct a a logger instance
A logger instance consists of a stack of logs (self.log_stack) and an
enabled flag.
If the enabled flag is set to False, the logger will only print to
screen instead of to underlying files.
"""
self.log_stack = []
self.enabled = True

def add_log(self, path):
"""Add a log to the current log stack
Expand All @@ -29,18 +38,19 @@ def add_log(self, path):
Args:
path: File path for the new log file
"""
if isinstance(path, str):
if isinstance(path, str) and self.enabled:
stream = None
stream = open(path, 'w+')
stream = open(path, 'a+')
self.log_stack.append((path, stream))

def remove_log(self):
"""Remove the active stack
Pop the active log from the log stack, and close the log stream.
"""
last_log = self.log_stack.pop()
last_log[1].close()
if self.enabled:
last_log = self.log_stack.pop()
last_log[1].close()

def active_log(self):
"""Return the path for the active log
Expand Down

0 comments on commit bc34173

Please sign in to comment.