#!/usr/bin/env python """ Prettify qstat scheduler allocations. Example: $ echo foo/1+foo/2+foo/15+foo/16+blah/20+blah/21+blah/30+blah/31+blah/40 | prettify-qstat.py blah: 20-21, 30-31, 40, foo: 1-2, 15-16 """ import itertools import sys def merge_range(numbers): for key, group in itertools.groupby(enumerate(numbers), lambda (x, y): y - x): group = list(group) if group[0][1] == group[-1][1]: yield str(group[0][1]) else: yield "%i-%i" % (group[0][1], group[-1][1]) def prettify(qstat): pretty = [] allocation_raw = [_.split("/") for _ in qstat.split("+")] allocation_dict = {} for host, proc in allocation_raw: if not allocation_dict.has_key(host): allocation_dict[host] = [] allocation_dict[host].append(int(proc)) allocation_dict[host].sort() for host in allocation_dict: pretty.append("%s: %s" % (host, str.join(", ", merge_range(allocation_dict[host])))) return str.join(", ", pretty) if __name__ == "__main__": for line in sys.stdin: print prettify(line)