| Line | |
|---|
| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import sys |
|---|
| 4 | import pickle |
|---|
| 5 | from optparse import OptionParser |
|---|
| 6 | |
|---|
| 7 | from string import join |
|---|
| 8 | |
|---|
| 9 | class Parser(OptionParser): |
|---|
| 10 | def __init__(self): |
|---|
| 11 | OptionParser.__init__(self, |
|---|
| 12 | usage='%prog [options] state_file [allocid]') |
|---|
| 13 | self.add_option('--field', dest='field', default='manifest', |
|---|
| 14 | help='field to extract: deafult %default') |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | parser = Parser() |
|---|
| 18 | opts, args = parser.parse_args() |
|---|
| 19 | |
|---|
| 20 | if len(args) == 1: |
|---|
| 21 | fn = args[0] |
|---|
| 22 | a = None |
|---|
| 23 | elif len(args) == 2: |
|---|
| 24 | fn, a = args |
|---|
| 25 | else: |
|---|
| 26 | parser.print_help() |
|---|
| 27 | sys.exit(1) |
|---|
| 28 | |
|---|
| 29 | try: |
|---|
| 30 | f = open(fn, 'r') |
|---|
| 31 | d = pickle.load(f) |
|---|
| 32 | f.close() |
|---|
| 33 | except EnvironmentError, e: |
|---|
| 34 | sys.exit("Can't read %s: %s" % (e.filename or '!?', e.strerror)) |
|---|
| 35 | except pickle.PickleError, e: |
|---|
| 36 | f.close() |
|---|
| 37 | sys.exit("Unpickling error: %s" %e) |
|---|
| 38 | |
|---|
| 39 | if 'allocation' in d: |
|---|
| 40 | alloc = d['allocation'] |
|---|
| 41 | |
|---|
| 42 | if a: |
|---|
| 43 | if a in alloc: |
|---|
| 44 | if opts.field in alloc[a]: |
|---|
| 45 | print alloc[a][opts.field] |
|---|
| 46 | else: |
|---|
| 47 | sys.exit('No %s in %s' % (opts.field, a)) |
|---|
| 48 | else: |
|---|
| 49 | sys.exit('Allocation %s not in state' % a) |
|---|
| 50 | else: |
|---|
| 51 | for k in alloc.keys(): |
|---|
| 52 | if opts.field in alloc[k]: |
|---|
| 53 | print k |
|---|
| 54 | else: |
|---|
| 55 | sys.exit('No allocation in state') |
|---|