File: replace_write6.py

package info (click to toggle)
abinit 9.10.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 518,712 kB
  • sloc: xml: 877,568; f90: 577,240; python: 80,760; perl: 7,019; ansic: 4,585; sh: 1,925; javascript: 601; fortran: 557; cpp: 454; objc: 323; makefile: 77; csh: 42; pascal: 31
file content (213 lines) | stat: -rwxr-xr-x 6,956 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# ======================================================================
# == Python script replacing "forbidden" access to standard output    ==
# == or standard error in ABINIT source files by statements using     ==
# == "std_out" or "std_err" global variables.                         ==
# ==                                                                  ==
# ==  Unallowed access: access to standard output via an unit         ==
# ==                    number different from "std_out".              ==
# ==                    access to main output via an unit             ==
# ==                    number different from "ab_out".               ==
# ==                    access to standard output via an unit         ==
# ==                    number different from "std_err".              ==
# ==                                                                  ==
# ==  Usage: simply launch the script from the ABINIT root folder     ==
# ==                                                                  ==
# ==                                           M. Torrent - June 2011 ==
# ======================================================================
from __future__ import print_function

import os
import re

#The following statements (access to standard output)...
write6_wrong = [  # Note: use only lowercases
"print*,",
"write(*,",
"write(6,",
"write(06,",
"write(006,",
"wrtout(6,",
"wrtout(06,",
"wrtout(006,"
]
#... have to be replaced by these ones:
write6_correct = [
"write(std_out,*) ",
"write(std_out,",
"write(std_out,",
"write(std_out,",
"write(std_out,",
"wrtout(std_out,",
"wrtout(std_out,",
"wrtout(std_out,"
]

#The following statements (access to main output)...
write7_wrong = [  # Note: use only lowercases
"write(7,",
"write(07,",
"write(007,",
"wrtout(7,",
"wrtout(07,",
"wrtout(007,"
]
#... have to be replaced by these ones:
write7_correct = [
"write(ab_out,",
"write(ab_out,",
"write(ab_out,",
"wrtout(ab_out,",
"wrtout(ab_out,",
"wrtout(ab_out,"
]

#The following statements (access to standard error)...
write0_wrong = [  # Note: use only lowercases
"write(0,",
"write(00,",
"write(000,",
"wrtout(0,",
"wrtout(00,",
"wrtout(000,"
]
#... have to be replaced by these ones:
write0_correct = [
"write(std_err,",
"write(std_err,",
"write(std_err,",
"wrtout(std_err,",
"wrtout(std_err,",
"wrtout(std_err,"
]

#Pattern to be search for each member of write6_wrong
#(case insensitive, eventual whitespaces between characters)
pattern_write6_wrong=[]
for i in range(len(write6_wrong)):
  strg=re.sub(r'(?<!^)x*','\s*',write6_wrong[i])
  strg=re.sub(r'\*\*','*\*',strg)
  strg=re.sub(r'\(','\\\(',strg)
  strg='(?i)'+strg
  re.compile(strg)
  pattern_write6_wrong.append(strg)

#Pattern to be search for each member of write7_wrong
#(case insensitive, eventual whitespaces between characters)
pattern_write7_wrong=[]
for i in range(len(write7_wrong)):
  strg=re.sub(r'(?<!^)x*','\s*',write7_wrong[i])
  strg=re.sub(r'\*\*','*\*',strg)
  strg=re.sub(r'\(','\\\(',strg)
  strg='(?i)'+strg
  re.compile(strg)
  pattern_write7_wrong.append(strg)

#Pattern to be search for each member of write0_wrong
#(case insensitive, eventual whitespaces between characters)
pattern_write0_wrong=[]
for i in range(len(write0_wrong)):
  strg=re.sub(r'(?<!^)x*','\s*',write0_wrong[i])
  strg=re.sub(r'\*\*','*\*',strg)
  strg=re.sub(r'\(','\\\(',strg)
  strg='(?i)'+strg
  re.compile(strg)
  pattern_write0_wrong.append(strg)

re_srcfile = re.compile("\.([Ff]|[Ff]90|h)$")

print( )
print( '---------------------------------------------------')
print( 'Replacing forbidden access to standard output/error')
print( 'in ABINIT src files')
print( '---------------------------------------------------')

#Initialize counters
file_total_count=0
file_write_wrong_count=0

#Loop over files in src folder
#-----------------------------
for (root, dirs, files) in os.walk("src"):
  for src in files:
    if ( re_srcfile.search(src) ):
      file_total_count +=1
      filename=os.path.join(root,src)
      with open(filename, "r") as fh: 
        src_data = fh.readlines()

#     1- look for wrong write6/7/0 statements in the file
#     ---------------------------------------------------
#     Loop over lines in the file
      found_write6_wrong=False
      found_write7_wrong=False
      found_write0_wrong=False
      for line in src_data:
#       Transform line to lower case + eliminate whitespaces
        line=line.lower()
        line = re.sub(" ","",line)
#       Look for wrong statements
        for strg in write6_wrong:
          if line.find(strg) != -1:
            found_write6_wrong=True
            break
        for strg in write7_wrong:
          if line.find(strg) != -1:
            found_write7_wrong=True
            break
        for strg in write0_wrong:
          if line.find(strg) != -1:
            found_write0_wrong=True
            break
        if found_write6_wrong: break
        if found_write7_wrong: break
        if found_write0_wrong: break

#     2- if wrong statements found, replace them
#     ------------------------------------------
      if found_write6_wrong or found_write7_wrong or found_write0_wrong:
        ErrorEncountered=False
#       Write message
        print ('File %s: found wrong statements !' % (filename))
#       Open a temporary file for writing
        filenametmp=filename+'.tmp'
        try:
          filout=open(filenametmp,'w')
        except:
          print ('File %s, error: could not open tmp file !' % (filename))
          ErrorEncountered=True
        if not ErrorEncountered:
#         Loop over lines in the file
          for line in src_data:
            newline=line
#           Replace wrong statements
            for i in range(len(write6_wrong)):
              newline=re.sub(pattern_write6_wrong[i],write6_correct[i],newline)
            for i in range(len(write7_wrong)):
              newline=re.sub(pattern_write7_wrong[i],write7_correct[i],newline)
            for i in range(len(write0_wrong)):
              newline=re.sub(pattern_write0_wrong[i],write0_correct[i],newline)
#           Write the modified line
            try:
              filout.write(newline)
            except:
              print ('File %s, error: could not write into tmp file !' % (filename))
              ErrorEncountered=True
              break
#         Close the temporary file
          filout.close()
#       Replace current file by temporary file
        if not ErrorEncountered:
          try:
            os.system('mv -f '+filenametmp+' '+filename)
            file_write_wrong_count +=1
          except:
            print ('File %s, error: could not move tmp file !' % (filename))

#Print final message
#-------------------
if file_write_wrong_count==0:
  print ('No file modified !')
else:
  print ('---------------------------------------------------')
  print ('%d/%d file(s) modified !' %  (file_write_wrong_count,file_total_count))