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
|
#!/usr/bin/env python
#!/bin/env python
# $Id: adprocess.py,v 1.12 2001/08/24 18:26:15 bsmith Exp $
#
# change python to whatever is needed on your system to invoke python
#
# Processes .c or .f files looking for a particular function.
# Copies that function as well as an struct definitions
# into a new file to be processed with adiC
#
# Calling sequence:
# adprocess.py file1.[cf] functionname functionname2
#
#
# Bugs: Final line of C function must begin with } on first line
# Application context must be called AppCtx
# structs possible format is not completely general
#
import urllib
import os
import ftplib
import httplib
import re
from exceptions import *
from sys import *
from string import *
from parseargs import *
#
# Copies structs from filename to filename.tmp1
def setupfunctionC(filename,g = None):
import re
regtypedef = re.compile('typedef [ ]*struct')
reginclude = re.compile('#include [ ]*"([a-zA-Z_0-9]*.h)"')
regdefine = re.compile('#define')
regdefine__ = re.compile('#define [ ]*__FUNCT__')
regextern = re.compile('extern')
regEXTERN = re.compile('EXTERN')
regif = re.compile('#if')
regendif = re.compile('#endif')
f = open(filename)
if not g:
newfile = filename + ".tmp1"
g = open(newfile,"w")
g.write("#include <math.h>\n")
g.write("#define PetscMin(a,b) (((a)<(b)) ? (a) : (b))\n")
line = f.readline()
while line:
# line = lstrip(line)+" "
fl = regtypedef.search(line)
if fl:
struct = line
while line:
reg = re.compile('^[ ]*}')
fl = reg.search(line)
if fl:
break
line = f.readline()
struct = struct + line
#
# if this is the AppCtx then replace double and Scalar with passive
#
reg = re.compile('^[ ]*}[ ]*AppCtx[ ]*;')
fl = reg.search(line)
if fl:
print "Extracting structure AppCtx"
reg = re.compile('\n[ ]*PetscScalar ')
struct = reg.sub('\nPassiveScalar ',struct)
reg = re.compile('\n[ ]*double ')
struct = reg.sub('\nPassiveReal ',struct)
reg = re.compile('\n[ ]*PetscReal ')
struct = reg.sub('\nPassiveReal ',struct)
else:
reg = re.compile('^[ ]*}[ ]*')
line = reg.sub('',line)
reg = re.compile('[ ]*;\n')
line = reg.sub('',line)
print "Extracting structure "+line
g.write(struct)
# copy over all #define macros
fl = regdefine.search(line)
if fl:
fl = regdefine__.search(line)
if not fl:
g.write(line)
# copy over extern statements
if regextern.search(line) or regEXTERN.search(line):
g.write(line)
if reginclude.search(line):
fname = reginclude.match(line).group(1)
if os.path.exists(fname):
setupfunctionC(fname,g)
fl = regif.search(line)
if fl:
g.write(line)
fl = regendif.search(line)
if fl:
g.write(line)
line = f.readline()
f.close()
return g
#
# Appends function functionname from filename to filename.tmp1
def getfunctionC(g,filename,functionname):
import re
f = open(filename)
g.write("/* Function "+functionname+"*/\n\n")
line = f.readline()
while line:
for i in split('int double PetscReal PetscScalar PassiveReal PassiveScalar PetscErrorCode'," "):
reg = re.compile('^[ ]*'+i+'[ ]*'+functionname+'[ ]*\(')
fl = reg.search(line)
if fl:
print 'Extracting function', functionname
while line:
g.write(line)
# this is dangerous, have no way to find end of function
if line[0] == '}':
break
line = f.readline()
line = f.readline()
continue
line = f.readline()
f.close()
def getfunctionF(filename,functionname):
functionname = lower(functionname)
newfile = filename + ".f"
f = open(filename)
g = open(newfile,"w")
line = f.readline()
line = lower(line)
while line:
sline = lstrip(line)
if sline:
if len(sline) >= 11 + len(functionname):
if sline[0:11+len(functionname)] == "subroutine "+functionname:
while line:
sline = lstrip(line)
if sline:
g.write(line)
if sline[0:4] == "end\n":
break
line = f.readline()
line = lower(line)
line = f.readline()
line = lower(line)
f.close()
g.close()
def main():
arg_len = len(argv)
if arg_len < 2:
print 'Error! Insufficient arguments.'
print 'Usage:', argv[0], 'file.[cf] functionname1 functionname2 ...'
sys.exit()
ext = split(argv[1],'.')[-1]
if ext == "c":
g = setupfunctionC(argv[1])
for i in range(2,arg_len):
getfunctionC(g,argv[1],argv[i])
g.close()
else:
getfunctionF(argv[1],argv[2])
#
# The classes in this file can also be used in other python-programs by using 'import'
#
if __name__ == '__main__':
main()
|