forked from thomas-robinson/mkmf2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparseShort.py
More file actions
executable file
·340 lines (261 loc) · 9.34 KB
/
Copy pathparseShort.py
File metadata and controls
executable file
·340 lines (261 loc) · 9.34 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""mkmf2
Resolves dependencies between Fortran modules.
"""
'''
!***********************************************************************
!* GNU Lesser General Public License
!*
!* This file is part of the GFDL Flexible Modeling System (FMS).
!*
!* mkmf2 is free software: you can redistribute it and/or modify it under
!* the terms of the GNU Lesser General Public License as published by
!* the Free Software Foundation, either version 3 of the License, or (at
!* your option) any later version.
!*
!* mkmf2 is distributed in the hope that it will be useful, but WITHOUT
!* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
!* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
!* for more details.
!*
!* You should have received a copy of the GNU Lesser General Public
!* License along with FMS. If not, see <http://www.gnu.org/licenses/>.
!*
!* Author: Diyor Zakirov
!***********************************************************************
'''
import re;
import os;
def getModules(fileName, verbose = False, vv = False):
"""Parses Fortran file and returns module dependencies.
This function takes in a file to parse through.
Using regex to pattern match, populates a module list, cleans up, and returns without duplicates.
Requires a name of the file to be parsed.
"""
MODS = []
fileContents = open(fileName, encoding = 'latin-1').read()
if verbose or vv:
print("Open " + fileName + " and read contents")
if vv:
print("Regex pattern to find matches in the file. Checks for USE and possible & on the same or next line, until module name is found signified by the '?'. Ignores cases and tries to match on all lines")
print("Regex patter: ^ *USE[ &\n]+.*?[ &\n]*,")
patternMatch = re.compile('^ *USE[ &\n]+.*?[ &\n]*,', re.IGNORECASE | re.M)
if verbose or vv:
print("Parsing the file, finding all possible matches using regex")
"""Finds all possible matches in the file.
Puts all possible matches in a list.
"""
matches = re.findall(patternMatch, fileContents)
"""Grooms and populates return list of modules.
Removes spaces, characters from pattern matching, and checks for duplicates.
"""
for match in matches:
if verbose or vv:
print("Match found: " + match)
match = match.lower().strip()
badChars = ["use", "&", '\n', ' ', ',']
for char in badChars:
match = match.replace(char, '')
if verbose or vv:
print("Cleaning up the match: " + match +"\n")
if not match in MODS:
MODS.append(match)
if verbose or vv:
print("The module dependencies are:")
for m in MODS:
print(m)
print("\n")
return MODS
def getFileModuleName(fileName):
"""Returns the module name of a file.
Requires name of file to be parsed.
"""
badChars = ["module", "module.", "modules"]
fileContents = open(fileName, encoding = 'latin-1').read()
moduleNameMatch = re.compile('^ *MODULE+.*', re.IGNORECASE | re.M)
matches = re.findall(moduleNameMatch, fileContents)
if not matches or matches[0] in badChars:
matches = ' '
return matches
else:
result = matches[0]
for char in ['module', 'MODULE', ' ']:
result = result.replace(char, '')
return result
def getPathModuleNameList(path):
"""Returns a list of modules in given path.
Requires a path to be parsed.
"""
moduleList = []
fileList = os.listdir(path)
for file in fileList:
if os.path.isfile(file) and file != "Makefile.am":
moduleList.append(getFileModuleName(file))
return moduleList
def getSubdirModuleNameList(path):
"""Returns a list of modules from sub directories of a path given.
Requires a path to be parsed.
"""
subdirModuleList = []
fileList = os.listdir(path)
for file in fileList:
if os.path.isfile(path + "/" + file) and file != "Makefile.am":
subdirModuleList.append(getFileModuleName(path + "/" + file))
return subdirModuleList
"""Global value for APCPPFLAGS dictionary"""
amcppDic = {}
def getAMCPP(path):
fortranMatch = re.compile('.*F90', re.IGNORECASE)
fileList = os.listdir(path)
for file in fileList:
if not os.path.isfile(path + "/" + file):
getAMCPP(path + "/" + file)
elif fortranMatch.match(file):
amcppDic[getFileModuleName(path + "/" + file)] = path.split('/')[len(path.split('/'))-1]
return amcppDic
def writeModules(path, verbose = False, vv = False, recursive = False, mainDir = False):
"""Creates a Makefile.am
Creates a Makefile.am in the path provided, resolving all possible dependencies.
Requires a path to a folder with Fortran modules.
"""
if mainDir:
getAMCPP(path)
if verbose or vv:
print("Creating AMCPPFLAGS dictionary...")
print(amcppDic)
fortranMatch = re.compile('.*F90', re.IGNORECASE)
"""Runs recursively"""
if recursive:
if verbose or vv:
print("Running recursively...")
for file in os.listdir(path):
if file == ".git":
pass
else:
if not os.path.isfile(path + "/" + file):
writeModules(path + "/" + file, verbose, vv, recursive, mainDir = False)
os.chdir(path)
folder = path.split('/')[len(path.split('/'))-1]
makefile = open('Makefile.am', 'w',)
fileList = os.listdir(path)
subDirModules = []
DONE = False
AMCPPFLAGS_str = '\t-I$[top_scrdir]/include \\\n' + '\t-I${builddir}/.mods \\\n'
SUBDIRS_str = ''
SUBDIRSLIB_str = ''
NOINSTLTL_str = ''
SOURCES_str = ''
MODULESINIT_str = ''
DEPENDENCIES_str = ''
MODFILES_str = ''
if verbose or vv:
print("Setting work directory to " + path + "\n")
print("Files to parse:\n")
for f in fileList:
print(f)
"""Populating the subDirModules if there are any"""
if recursive:
for file in fileList:
if file == ".git":
break;
if not os.path.isfile(path + "/" + file):
subDirModules += (getSubdirModuleNameList(path + "/" + file))
if verbose or vv:
print("Creating list of sub directory modules...")
if vv:
print(subDirModules)
for file in fileList:
"""List all possible sub directories"""
if not os.path.isfile(path + "/" + file) and file != ".git":
if verbose or vv:
print("Found sub directory: " + file)
SUBDIRS_str += ("\t" + file + " \\\n")
SUBDIRSLIB_str += "\tlib" + file + ".la \\\n"
"""List Fortran file sources"""
if fortranMatch.match(file):
if verbose or vv:
print("Found source file: " + file)
NOINSTLTL_str += ("\tlib" + file.split('.')[0] + ".la \\\n")
if getFileModuleName(file) != ' ':
SOURCES_str += ("lib" + file.split('.')[0] + "_la_SOURCES = " + file + " " + getFileModuleName(file) + ".mod \n")
MODULESINIT_str += (file.split('.')[0] + ".$(OBJEXT): " + getFileModuleName(file) + ".mod \n")
"""List all modules files in built_sources"""
MODFILES_str += ("\t" + getFileModuleName(file) + '.mod \\\n')
"""List dependencies of each file"""
set1 = set(getModules(file, verbose,vv)).intersection(getPathModuleNameList(path))
set2 = set(getModules(file, verbose,vv)).intersection(subDirModules)
"""List AMCPPFLAGS for modules"""
for mod in getModules(file,vv):
if mod in amcppDic and not mod in set1 and not mod in set2:
if amcppDic[mod] in AMCPPFLAGS_str:
pass;
else:
if vv:
print("Found module needing AMCPPFLAG: " + mod)
AMCPPFLAGS_str += "\t-I${top_buildir}/" + amcppDic[mod] + " \\\n"
if set1 or set2:
if verbose or vv:
print("Found dependencies for " + file)
print("Dependencies...\n")
print(set1)
print(set2)
DEPENDENCIES_str += (getFileModuleName(file) + ".mod: ")
for mod in set1:
DONE = True
DEPENDENCIES_str += (mod + '.mod ')
for mod in set2:
DONE = True
DEPENDENCIES_str += (mod + '.mod ')
if DONE:
DEPENDENCIES_str += "\n"
DONE = False
"""Write populated strings to the file"""
if AMCPPFLAGS_str != '':
if verbose or vv:
print("\nWriting AMCPPFLAGS... \n")
if vv:
print(AMCPPFLAGS_str)
makefile.write("AM_CPPFLAGS = \\\n" + AMCPPFLAGS_str[0:len(AMCPPFLAGS_str)-2] + '\n')
makefile.write("\n\n")
if SUBDIRS_str != '':
if verbose or vv:
print("\nWriting sub directories... \n")
if vv:
print(SUBDIRS_str)
makefile.write("SUBDIRS = \\\n" + SUBDIRS_str[0:len(SUBDIRS_str)-3] + '\n')
makefile.write('\n\n')
makefile.write("lib" + folder + "_la_LIBADD = \\\n" + SUBDIRSLIB_str[0:len(SUBDIRSLIB_str)-3] + '\n')
makefile.write("\n\n")
if SOURCES_str != '':
if verbose or vv:
print("\nWriting Fortran sources... \n")
if vv:
print(SOURCES_str)
makefile.write("noinst_LTLIBRARIES = \\\n" + NOINSTLTL_str[0:len(NOINSTLTL_str)-2] + '\n')
makefile.write("\n\n")
makefile.write(SOURCES_str + '\n')
makefile.write("\n\n")
if verbose or vv:
print("\nWriting module initializations... \n")
if vv:
print(MODULESINIT_str)
makefile.write(MODULESINIT_str)
makefile.write("\n\n")
if verbose or vv:
print("\nWriting module dependencies... \n")
if vv:
print(DEPENDENCIES_str)
makefile.write(DEPENDENCIES_str)
makefile.write("\n\n")
if verbose or vv:
print("\nWriting built_sources... \n")
if vv:
print(MODFILES_str)
makefile.write("MODFILES = \\\n" + MODFILES_str[0:len(MODFILES_str)-3] + '\n' +
"include_HEADERS = $(MODFILES)\n" +
"BUILT_SOURCES = $(MODFILES)\n")
makefile.write("\n\n")
makefile.write("include $(top_srcdir)/mkfmods.mk \n\n")
makefile.write("CLEANFILES = *.mod")
if __name__ == '__main__':
pass
#writeModules('/home/Diyor.Zakirov/atmos_param')