1 | #!/usr/bin/env python |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | # grabdata |
---|
4 | # |
---|
5 | # script to grab dx files from a run.xml file |
---|
6 | # |
---|
7 | # RUN AS FOLLOWS: |
---|
8 | # grabdata [-h|-t] <runFile> |
---|
9 | # |
---|
10 | # -h | --help - print the help menu |
---|
11 | # -t | --type - type of data to grab |
---|
12 | # |
---|
13 | # <runFile> - the run.xml file containing dx files |
---|
14 | # |
---|
15 | # ====================================================================== |
---|
16 | # AUTHOR: Derrick Kearney, Purdue University |
---|
17 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
18 | # |
---|
19 | # See the file "license.terms" for information on usage and |
---|
20 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
21 | # ====================================================================== |
---|
22 | # |
---|
23 | # |
---|
24 | |
---|
25 | import re |
---|
26 | import os |
---|
27 | import sys |
---|
28 | import getopt |
---|
29 | import Rappture |
---|
30 | |
---|
31 | def help(argv=sys.argv): |
---|
32 | return """%(prog)s [-h|-t] runFile |
---|
33 | Grab DX, PDB, or other files from a run.xml file |
---|
34 | |
---|
35 | -h | --help - print the help menu |
---|
36 | -t | --type - type of data to grab |
---|
37 | |
---|
38 | <runFile> - the run.xml file containing dx, pdb or other files |
---|
39 | |
---|
40 | Examples: |
---|
41 | %(prog)s run1234.xml |
---|
42 | %(prog)s -t dx run1234.xml |
---|
43 | %(prog)s -t pdb run1234.xml |
---|
44 | """ % {'prog':os.path.basename(argv[0])} |
---|
45 | |
---|
46 | def main(argv=None): |
---|
47 | |
---|
48 | if argv is None: |
---|
49 | argv = sys.argv |
---|
50 | |
---|
51 | if len(argv) < 2: |
---|
52 | msg = "%(prog)s requires at least 1 arguments" % {'prog':argv[0]} |
---|
53 | print >>sys.stderr, msg |
---|
54 | print >>sys.stderr, help() |
---|
55 | return 2 |
---|
56 | |
---|
57 | longOpts = ["type=","help"] |
---|
58 | shortOpts = "t:h" |
---|
59 | try: |
---|
60 | opts, args = getopt.getopt(argv[1:], shortOpts, longOpts) |
---|
61 | except getopt.GetoptError, msg: |
---|
62 | print >>sys.stderr, msg |
---|
63 | print >>sys.stderr, help() |
---|
64 | return 2 |
---|
65 | |
---|
66 | tag = 'dx' |
---|
67 | |
---|
68 | # match options |
---|
69 | for o, v in opts: |
---|
70 | if o in ("-t", "--type"): |
---|
71 | tag = v |
---|
72 | elif o in ("-h", "--help"): |
---|
73 | print >>sys.stderr, help() |
---|
74 | return 2 |
---|
75 | |
---|
76 | runFile = args[0] |
---|
77 | |
---|
78 | # look for the text <tag> (any characters non-greedy) </tag> |
---|
79 | regexp = r'<%(tag)s>(?P<data>.*?)</%(tag)s>' % {'tag':tag} |
---|
80 | re_command = re.compile(regexp,re.I|re.DOTALL) |
---|
81 | |
---|
82 | try: |
---|
83 | xmlfile = open(runFile, "rb").read() |
---|
84 | except IOError, msg: |
---|
85 | print >>sys.stderr, msg |
---|
86 | return 2 |
---|
87 | |
---|
88 | cnt = 0 |
---|
89 | runFileBase = os.path.basename(runFile) |
---|
90 | prefix,ext = os.path.splitext(runFileBase) |
---|
91 | |
---|
92 | outTextIter = re_command.finditer(xmlfile) |
---|
93 | for match in outTextIter: |
---|
94 | if match: |
---|
95 | encoded_data = match.group('data') |
---|
96 | # This needs to be a python static variable: |
---|
97 | # RPENC_HDR == 4 |
---|
98 | unencoded_data = Rappture.encoding.decode(encoded_data,4) |
---|
99 | newDXFile = open('%s_%i_.%s'%(prefix,cnt,tag),'wb') |
---|
100 | newDXFile.write(unencoded_data) |
---|
101 | newDXFile.close() |
---|
102 | cnt += 1 |
---|
103 | |
---|
104 | if __name__ == '__main__' : |
---|
105 | # call main and exit gracefully |
---|
106 | sys.exit(main()) |
---|