def show_config(file): from compmake.utils.visualization import colored ordered_sections = sorted(config_sections.values(), key=lambda section: section.order) max_len_name = 1 + max([len(s.name) for s in config_switches.values()]) max_len_val = 1 + max([len(str(compmake_config.__dict__[s.name])) for s in config_switches.values()]) for section in ordered_sections: file.write(" ---- %s ---- \n" % section.name) if section.desc: # XXX multiline file.write(" | %s \n" % section.desc) for name in section.switches: switch = config_switches[name] value = compmake_config.__dict__[name] changed = (value != switch.default_value) if changed: attrs = ['bold'] else: attrs = [] value = str(value) desc = str(switch.desc) file.write(" | %s %s %s\n" % (colored(rjust(name, max_len_name), attrs=['bold']), colored(rjust(value, max_len_val), attrs=attrs), desc))
def display_stats(job_list): states_order = [Cache.NOT_STARTED, Cache.IN_PROGRESS, Cache.MORE_REQUESTED, Cache.FAILED, Cache.DONE] # initialize counters to 0 states2count = dict(map(lambda x: (x, 0), states_order)) function2state2count = {} total = 0 for job_id in job_list: cache = get_job_cache(job_id) states2count[cache.state] += 1 total += 1 function_id = get_job(job_id).command_desc # initialize record if not present if not function_id in function2state2count: function2state2count[function_id] = dict(map(lambda x: (x, 0), states_order) + [('all', 0)]) # update function2state2count[function_id][cache.state] += 1 function2state2count[function_id]['all'] += 1 if total == 100: # XXX: use standard method info("Loading a large number of jobs...") print("Found %s jobs in total. Summary by state:" % total) for state in states_order: desc = "%30s" % Cache.state2desc[state] # colorize output desc = colored(desc, **state2color[state]) num = states2count[state] if num > 0: print("%s: %5d" % (desc, num)) print("Summary by function:") for function_id, function_stats in function2state2count.items(): ndone = function_stats[Cache.DONE] nfailed = function_stats[Cache.FAILED] nrest = function_stats['all'] - ndone - nfailed failed_s = "%5d failed" % nfailed if nfailed > 0: failed_s = colored(failed_s, color='red') s = "%5d done, %s, %5d to do." % (ndone, failed_s, nrest) print(" %30s(): %s" % (function_id, s))
def __init__(self, prefix, echo_stdout=True, echo_stderr=True): self.old_stdout = sys.stdout self.old_stderr = sys.stderr t1 = lambda s: "%s| %s" % (prefix, colored(s, "cyan", attrs=["dark"])) t2 = lambda s: pad_to_screen(t1(s)) dest = {True: sys.stdout, False: None}[echo_stdout] self.stdout_replacement = StreamCapture(transform=t2, dest=dest) sys.stdout = self.stdout_replacement t3 = lambda s: "%s| %s" % (prefix, colored(s, "red", attrs=["dark"])) t4 = lambda s: pad_to_screen(t3(s)) dest = {True: sys.stderr, False: None}[echo_stderr] self.stderr_replacement = StreamCapture(transform=t4, dest=dest) sys.stderr = self.stderr_replacement
def list_job_detail(job_id): #computation = get_computation(job_id) cache = get_job_cache(job_id) parents = direct_parents(job_id) children = direct_children(job_id) up, reason = up_to_date(job_id) red = lambda x: colored(x, 'red') bold = lambda x: colored(rjust(x + ' ', 15), attrs=['bold']) try: print bold('Job ID:') + '%s' % job_id print bold('Status:') + '%s' % Cache.state2desc[cache.state] print bold('Uptodate:') + '%s (%s)' % (up, reason) print bold('Children:') + '%s' % ', '.join(children) print bold('Parents:') + '%s' % ', '.join(parents) if cache.state == Cache.DONE and cache.done_iterations > 1: print bold('Iterations:') + '%s' % cache.done_iterations print bold('Wall Time:') + '%.4f s' % cache.walltime_used print bold('CPU Time:') + '%.4f s' % cache.cputime_used print bold('Host:') + '%s' % cache.host if cache.state == Cache.IN_PROGRESS: print bold('Progress:') + '%s/%s' % \ (cache.iterations_in_progress, cache.iterations_goal) if cache.state == Cache.FAILED: print red(cache.exception) print red(cache.backtrace) def display_with_prefix(buffer, prefix, transform=lambda x:x, out=sys.stdout): for line in buffer.split('\n'): out.write('%s%s\n' % (prefix, transform(line))) if cache.captured_stdout: print "-----> captured stdout <-----" display_with_prefix(cache.captured_stdout, prefix='|', transform=lambda x: colored(x, attrs=['dark'])) if cache.captured_stderr: print "-----> captured stderr <-----" display_with_prefix(cache.captured_stdout, prefix='|', transform=lambda x: colored(x, attrs=['dark'])) except AttributeError: pass
def list_jobs(job_list): for job_id in job_list: up, reason = up_to_date(job_id) s = job_id s += " " + (" " * (60 - len(s))) cache = get_job_cache(job_id) tag = Cache.state2desc[cache.state] #if not up: # tag += ' (needs update)' k = (cache.state, up) assert k in state2color, "I found strange state %s" % str(k) color_args = state2color[k] s += colored(tag, **color_args) if cache.state == Cache.DONE and cache.done_iterations > 1: s += ' %s iterations completed ' % cache.done_iterations if cache.state == Cache.IN_PROGRESS: s += ' (%s/%s iterations in progress) ' % \ (cache.iterations_in_progress, cache.iterations_goal) if up: when = duration_human(time() - cache.timestamp) s += " (%s ago)" % when else: if cache.state in [Cache.DONE, Cache.MORE_REQUESTED]: s += " (needs update: %s)" % reason print s
def console_starting(event): #@UnusedVariable # starting console print "%s %s -- ``%s,, -- %s " % ( colored('Compmake', attrs=['bold']), version, banner, banner2) print "Welcome to the compmake console. " + \ "(write 'help' for a list of commands)" njobs = len(list(all_jobs())) print("%d jobs loaded; using namespace '%s'." % (njobs, get_namespace()))
def list_commands_with_sections(file=sys.stdout): ordered_sections = sorted(sections.values(), key=lambda section: section.order) max_len = 1 + max([len(cmd.name) for cmd in commands.values()]) for section in ordered_sections: file.write(" ---- %s ---- \n" % section.name) if section.desc: # XXX multiline file.write(" | %s \n" % section.desc) for name in section.commands: cmd = commands[name] short_doc = cmd.doc.split('\n')[0].strip() file.write(" | %s %s\n" % (colored(ljust(name, max_len), attrs=['bold']), short_doc))
def handle_event(event): #@UnusedVariable # FIXME bug s = "Done %s/%s " % (len(tracker.done), len(tracker.all_targets)) if tracker.failed: s += colored("Failed %s " % len(tracker.failed), 'red') def get_string(level): X = [] for job_id, status in tracker.status.items(): x = [] if level >= 1: x += [job_id ] # + ':' if level <= 1 or not job_id in tracker.status_plus: x += [ status] elif job_id in tracker.status_plus: x += [] stack = tracker.status_plus[job_id] for i, frame in enumerate(stack): if level >= 3: x += [frame.name] if level >= 2: x += ["%s/%s" % (frame.iterations[0] + 1, frame.iterations[1])] if level >= 4 and frame.iteration_desc is not None: x += ["(" + frame.iteration_desc + ")"] if i < len(stack) - 1: x += ['>>'] X += ["[" + " ".join(x) + "]" ] return " ".join(X) cols, rows = getTerminalSize() #@UnusedVariable choice = '%d processing' % len(tracker.status) for level in [4, 3, 2, 1, 0]: x = s + get_string(level) if len(x) <= cols: choice = x break choice = string.ljust(choice, cols - 1) stream.write(choice) stream.write('\r')
def console_ending(event): #@UnusedVariable print "Thanks for using compmake. Problems? Suggestions? \ Praise? Go to %s" % colored(compmake_issues_url, attrs=['bold'])
def job_redefined(event): #@UnusedVariable #stream.write('\n') stream.write(colored('Redefined %s\r' % event.job_id, 'yellow', attrs=['bold'])) stream.write(colored(event.reason, 'yellow'))