root / prepareQMX / prepareQMX.py @ 14
Historique | Voir | Annoter | Télécharger (5,4 ko)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
# command line interface for setup of QMX calculations
|
4 |
#
|
5 |
# Torsten Kerber, ENS LYON: 2011, 07, 11
|
6 |
#
|
7 |
# This work is supported by Award No. UK-C0017, made by King Abdullah
|
8 |
# University of Science and Technology (KAUST)
|
9 |
|
10 |
#---------------------------------------------------------------------------------------------------
|
11 |
import os, sys |
12 |
from copy import deepcopy |
13 |
from optparse import OptionParser |
14 |
|
15 |
#---------------------------------------------------------------------------------------------------
|
16 |
from qmxEMBED import embedDefinitions |
17 |
from qmxJOB import jobDefinitions |
18 |
from qmxSTR import strDefinitions |
19 |
from qmxCALC import calcDefinitions, QmxCalcDefinition |
20 |
|
21 |
from qmxWRITE import writeData |
22 |
|
23 |
#---------------------------------------------------------------------------------------------------
|
24 |
#---------------------------------------------------------------------------------------------------
|
25 |
#---------------------------------------------------------------------------------------------------
|
26 |
def getline(section, subsection, lines): |
27 |
nline = -1
|
28 |
for iline in xrange(len(lines)): |
29 |
if lines[iline][0].strip() == subsection: |
30 |
if nline != -1: |
31 |
sys.stderr.write("section: <"+section+separator+subsection+"> has been definied more than once\n") |
32 |
sys.exit(1)
|
33 |
nline = iline |
34 |
if nline == -1: |
35 |
return None |
36 |
return lines[nline][1].strip() |
37 |
|
38 |
#---------------------------------------------------------------------------------------------------
|
39 |
def analyzeSection(definitions, section, lines): |
40 |
line = getline(section, "program", lines)
|
41 |
if line is None: |
42 |
print "section <" + section + "> does not contain program" |
43 |
sys.exit(1)
|
44 |
|
45 |
myDefinition = None
|
46 |
for definition in definitions: |
47 |
if definition.name.lower() == line.lower():
|
48 |
myDefinition = deepcopy(definition) |
49 |
break
|
50 |
|
51 |
if myDefinition is None: |
52 |
print "the program <" + line + "> is not defined for the section <" + section + ">" |
53 |
sys.exit(1)
|
54 |
|
55 |
myDefinition.system = section |
56 |
|
57 |
subsections = [] |
58 |
for line in lines: |
59 |
subsections.append(line[0].strip().lower())
|
60 |
|
61 |
for subsection in subsections: |
62 |
line = getline(section, subsection, lines) |
63 |
if line is None: |
64 |
print "input error in <"+section+"."+subsection+">" |
65 |
sys.exit(1)
|
66 |
if subsection == "program": |
67 |
continue
|
68 |
if subsection == "options": |
69 |
subsection = 'class.options'
|
70 |
myDefinition.keywords[subsection] = line |
71 |
|
72 |
return myDefinition
|
73 |
|
74 |
#---------------------------------------------------------------------------------------------------
|
75 |
#--- setting default values ------------------------------------------------------------------------
|
76 |
#---------------------------------------------------------------------------------------------------
|
77 |
|
78 |
#define separators
|
79 |
separator=":"
|
80 |
subseparator="."
|
81 |
lineseparator="\n"
|
82 |
|
83 |
#--- parse arguments -------------------------------------------------------------------------------
|
84 |
#parse arguments (python 2.6)
|
85 |
parser = OptionParser() |
86 |
parser.add_option("-i", "--infile", dest="inputFile", help="specifies the input file", metavar="FILE", default="qmx.in") |
87 |
parser.add_option("-o", "--outfile", dest="outputFile", help="specifies the output file", metavar="FILE") |
88 |
parser.add_option("-f", action="store_true", dest="overwrite") |
89 |
(options, args) = parser.parse_args() |
90 |
|
91 |
#--- check wether output file exists ---------------------------------------------------------------
|
92 |
if options.outputFile is not None and os.path.exists(options.outputFile) and not options.overwrite: |
93 |
sys.stderr.write(lineseparator+"the output file <"+options.outputFile+"> already exists"+lineseparator) |
94 |
sys.exit(1)
|
95 |
|
96 |
#--- read intput file ------------------------------------------------------------------------------
|
97 |
file=open(options.inputFile, "r") |
98 |
lines = file.readlines()
|
99 |
file.close()
|
100 |
|
101 |
#--- remove comments -------------------------------------------------------------------------------
|
102 |
for iline in xrange(len(lines)): |
103 |
lines[iline] = lines[iline].split("#")[0] |
104 |
|
105 |
#--- collect data in class -------------------------------------------------------------------------
|
106 |
definitions=[] |
107 |
definitions.append(QmxCalcDefinition()) |
108 |
|
109 |
#--- analyze the input file ------------------------------------------------------------------------
|
110 |
for section in "high-level", "low-level", "cluster", "system", "job", "embed": |
111 |
#search lines for section
|
112 |
lines_section=[] |
113 |
for line in lines: |
114 |
line = line.split(separator) |
115 |
if line[0].find(section) >= 0: |
116 |
line[0] = line[0].replace(section+subseparator, "") |
117 |
lines_section.append(line) |
118 |
|
119 |
#calculators
|
120 |
if section == "high-level" or section == "low-level": |
121 |
definitions.append(analyzeSection(calcDefinitions, section, lines_section)) |
122 |
|
123 |
#structures (atoms)
|
124 |
if section == "cluster" or section == "system": |
125 |
definitions.append(analyzeSection(strDefinitions, section, lines_section)) |
126 |
|
127 |
#job
|
128 |
if section == "job": |
129 |
definitions.append(analyzeSection(jobDefinitions, section, lines_section)) |
130 |
|
131 |
#embed
|
132 |
if section == "embed": |
133 |
definitions.append(analyzeSection(embedDefinitions, section, lines_section)) |
134 |
|
135 |
output = None
|
136 |
if options.outputFile is None: |
137 |
output=sys.stdout |
138 |
else:
|
139 |
output=open(options.outputFile, "w") |
140 |
|
141 |
writeData(output, definitions) |
142 |
output.close() |
143 |
|