source: fedd/extract_state_data.py @ f1f9aec

Last change on this file since f1f9aec was 2e46f35, checked in by mikeryan <mikeryan@…>, 13 years ago

switch to /usr/bin/env python to run python

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#!/usr/bin/env python
2
3import sys
4import pickle
5from optparse import OptionParser
6
7from string import join
8
9class 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
17parser = Parser()
18opts, args = parser.parse_args()
19
20if len(args) == 1:
21    fn = args[0]
22    a = None
23elif len(args) == 2:
24    fn, a = args
25else:
26    parser.print_help()
27    sys.exit(1)
28
29try:
30    f = open(fn, 'r')
31    d = pickle.load(f)
32    f.close()
33except EnvironmentError, e:
34    sys.exit("Can't read %s: %s" % (e.filename or '!?', e.strerror))
35except pickle.PickleError, e:
36    f.close()
37    sys.exit("Unpickling error: %s" %e)
38
39if '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
54else:
55    sys.exit('No allocation in state')
Note: See TracBrowser for help on using the repository browser.