Prettify: using python logging instead of custom logFile

svn-origin-rev: 17335
This commit is contained in:
Patrick Seewald 2016-08-30 19:38:19 +00:00
parent 61fd73fe44
commit 55fe699c25
4 changed files with 63 additions and 62 deletions

View file

@ -3,6 +3,8 @@ import re
import string
from sys import argv
from collections import deque
import logging
try:
from cStringIO import StringIO
except ImportError:
@ -217,9 +219,9 @@ def parseRoutine(inFile):
subF.close()
except:
import traceback
sys.stderr.write(
logging.warning(
"error trying to follow include " + m.group('file') + '\n')
sys.stderr.write(
logging.warning(
"warning this might lead to the removal of used variables\n")
traceback.print_exc()
if jline:
@ -359,9 +361,9 @@ def parseRoutine(inFile):
subF.close()
except:
import traceback
sys.stderr.write(
logging.warning(
"error trying to follow include " + m.group('file') + '\n')
sys.stderr.write(
logging.warning(
"warning this might lead to the removal of used variables\n")
traceback.print_exc()
(jline, _, lines) = stream.nextFortranLine()
@ -600,14 +602,14 @@ def writeDeclarations(parsedDeclarations, file):
writeExtendedDeclaration(d, file)
def cleanDeclarations(routine, logFile=sys.stderr):
def cleanDeclarations(routine):
"""cleans up the declaration part of the given parsed routine
removes unused variables"""
global R_VAR
containsRe = re.compile(r" *contains *$", re.IGNORECASE)
if routine['core']:
if containsRe.match(routine['core'][-1]):
logFile.write("*** routine %s contains other routines ***\n*** declarations not cleaned ***\n" %
logging.warning("*** routine %s contains other routines ***\n*** declarations not cleaned ***\n" %
(routine['name']))
return
commentToRemoveRe = re.compile(
@ -619,13 +621,13 @@ def cleanDeclarations(routine, logFile=sys.stderr):
return
if (routine['core']):
if re.match(" *type *[a-zA-Z_]+ *$", routine['core'][0], re.IGNORECASE):
logFile.write("*** routine %s contains local types, not fully cleaned ***\n" %
logging.warning("*** routine %s contains local types, not fully cleaned ***\n" %
(routine['name']))
if re.match(" *import+ *$", routine['core'][0], re.IGNORECASE):
logFile.write("*** routine %s contains import, not fully cleaned ***\n" %
logging.warning("*** routine %s contains import, not fully cleaned ***\n" %
(routine['name']))
if re.search("^#", "".join(routine['declarations']), re.MULTILINE):
logFile.write("*** routine %s declarations contain preprocessor directives ***\n*** declarations not cleaned ***\n" % (
logging.warning("*** routine %s declarations contain preprocessor directives ***\n*** declarations not cleaned ***\n" % (
routine['name']))
return
try:
@ -702,7 +704,7 @@ def cleanDeclarations(routine, logFile=sys.stderr):
raise SyntaxError(
"could not remove nullify of " + lowerV +
" as expected, routine=" + routine['name'])
logFile.write("removed var %s in routine %s\n" %
logging.info("removed var %s in routine %s\n" %
(lowerV, routine['name']))
R_VAR += 1
if (len(localD['vars'])):
@ -712,7 +714,7 @@ def cleanDeclarations(routine, logFile=sys.stderr):
if argDeclDict.has_key(arg):
argDecl.append(argDeclDict[arg])
else:
sys.stderr.write("warning, implicitly typed argument '" +
logging.warning("warning, implicitly typed argument '" +
arg + "' in routine " + routine['name'] + '\n')
if routine['kind'].lower() == 'function':
aDecl = argDecl[:-1]
@ -765,9 +767,9 @@ def cleanDeclarations(routine, logFile=sys.stderr):
routine['declarations'] = [newDecl.getvalue()]
except:
if routine.has_key('name'):
logFile.write("**** exception cleaning routine " +
logging.critical("**** exception cleaning routine " +
routine['name'] + " ****")
logFile.write("parsedDeclartions=" +
logging.critical("parsedDeclartions=" +
str(routine['parsedDeclarations']))
raise
@ -1003,7 +1005,7 @@ def prepareImplicitUses(modules):
return mods
def cleanUse(modulesDict, rest, implicitUses=None, logFile=sys.stderr):
def cleanUse(modulesDict, rest, implicitUses=None):
"""Removes the unneded modules (the ones that are not used in rest)"""
global R_USE
exceptions = {}
@ -1016,7 +1018,7 @@ def cleanUse(modulesDict, rest, implicitUses=None, logFile=sys.stderr):
m_att = implicitUses[m_name]
if m_att.has_key('_WHOLE_') and m_att['_WHOLE_']:
R_USE += 1
logFile.write("removed USE of module " + m_name + "\n")
logging.info("removed USE of module " + m_name + "\n")
del modules[i]
elif modules[i].has_key("only"):
els = modules[i]['only']
@ -1028,13 +1030,13 @@ def cleanUse(modulesDict, rest, implicitUses=None, logFile=sys.stderr):
impAtt = m.group('localName').lower()
if m_att.has_key(impAtt):
R_USE += 1
logFile.write("removed USE " + m_name +
logging.info("removed USE " + m_name +
", only: " + repr(els[j]) + "\n")
del els[j]
elif not exceptions.has_key(impAtt):
if findWord(impAtt, rest) == -1:
R_USE += 1
logFile.write("removed USE " + m_name +
logging.info("removed USE " + m_name +
", only: " + repr(els[j]) + "\n")
del els[j]
if len(modules[i]['only']) == 0:
@ -1055,7 +1057,7 @@ def resetModuleN(moduleName, lines):
lines[i])
def rewriteFortranFile(inFile, outFile, indent, decl_linelength, decl_offset, logFile=sys.stderr, orig_filename=None):
def rewriteFortranFile(inFile, outFile, indent, decl_linelength, decl_offset, orig_filename=None):
"""rewrites the use statements and declarations of inFile to outFile.
It sorts them and removes the repetitions."""
import os.path
@ -1095,7 +1097,7 @@ def rewriteFortranFile(inFile, outFile, indent, decl_linelength, decl_offset, lo
while routine['kind']:
routine = parseRoutine(inFile)
routines.append(routine)
map(lambda x: cleanDeclarations(x, logFile), routines)
map(lambda x: cleanDeclarations(x), routines)
for routine in routines:
coreLines.extend(routine['declarations'])
coreLines.extend(routine['strippedCore'])
@ -1103,10 +1105,10 @@ def rewriteFortranFile(inFile, outFile, indent, decl_linelength, decl_offset, lo
nonStPrep = 0
for line in modulesDict['origLines']:
if (re.search('^#', line) and not commonUsesRe.match(line)):
sys.stderr.write('noMatch ' + repr(line) + '\n')
logging.warning('noMatch ' + repr(line) + '\n') # what does it mean?
nonStPrep = 1
if nonStPrep:
logFile.write(
logging.warning(
"*** use statements contains preprocessor directives, not cleaning ***\n")
outFile.writelines(modulesDict['origLines'])
else:
@ -1123,11 +1125,11 @@ def rewriteFortranFile(inFile, outFile, indent, decl_linelength, decl_offset, lo
implicitUses = prepareImplicitUses(
implicitUsesRaw['modules'])
except:
sys.stderr.write(
logging.critical(
"ERROR trying to parse use statements contained in common uses precompiler file " + inc_absfn + '\n')
raise
cleanUse(modulesDict, rest,
implicitUses=implicitUses, logFile=logFile)
implicitUses=implicitUses)
normalizeModules(modulesDict['modules'])
outFile.writelines(modulesDict['preComments'])
writeUses(modulesDict['modules'], outFile)
@ -1139,11 +1141,10 @@ def rewriteFortranFile(inFile, outFile, indent, decl_linelength, decl_offset, lo
writeRoutine(routine, outFile)
except:
import traceback
logFile.write('-' * 60 + "\n")
traceback.print_exc(file=logFile)
logFile.write('-' * 60 + "\n")
logFile.write("Processing file '" + orig_filename + "'\n")
logging.critical('-' * 60 + "\n")
traceback.print_exc(file=sys.stderr)
logging.critical('-' * 60 + "\n")
logging.critical("Processing file '" + orig_filename + "'\n")
raise
# EOF

View file

@ -40,6 +40,7 @@ import re
import sys
import os
from formatting.normalizeFortranFile import USE_PARSE_RE, VAR_DECL_RE, InputStream, CharFilter, OMP_RE, OMP_DIR_RE
import logging
#=========================================================================
# constants, mostly regular expressions
@ -171,17 +172,13 @@ class F90Indenter(object):
is_new = False
valid_new = False
debug = False
for new_n, newre in enumerate(NEW_SCOPE_RE):
if newre.search(f_line) and not END_SCOPE_RE[new_n].search(f_line):
what_new = new_n
is_new = True
valid_new = True
scopes.append(what_new)
if debug:
sys.stderr.write(f_line + '\n')
break
logging.debug(f_line + '\n')
# check statements that continue scope
is_con = False
@ -194,9 +191,7 @@ class F90Indenter(object):
what = scopes[-1]
if what == what_con:
valid_con = True
if debug:
sys.stderr.write(f_line + '\n')
break
logging.debug(f_line + '\n')
# check statements that end scope
is_end = False
@ -209,9 +204,7 @@ class F90Indenter(object):
what = scopes.pop()
if what == what_end:
valid_end = True
if debug:
sys.stderr.write(f_line + '\n')
break
logging.debug(f_line + '\n')
# deal with line breaks
if not manual_lines_indent:
@ -669,11 +662,10 @@ def format_single_fline(f_line, whitespace, linebreak_pos, ampersand_sep, filena
#=========================================================================
def reformat_ffile(infile, outfile, logFile=sys.stderr, indent_size=2, whitespace=2, orig_filename=None):
def reformat_ffile(infile, outfile, indent_size=2, whitespace=2, orig_filename=None):
"""
main method to be invoked for formatting a Fortran file.
"""
debug = False
# don't change original indentation if rel-indents set to 0
adopt_indents = indent_size <= 0
@ -689,9 +681,9 @@ def reformat_ffile(infile, outfile, logFile=sys.stderr, indent_size=2, whitespac
infile.seek(0)
if not is_f90:
logFile.write("*** " + orig_filename +
logging.error("*** " + orig_filename +
": formatter can not handle f77 constructs. ***\n")
outfile.write(infile.read()) # does not handle f77 constructs
outfile.write(infile.read())
return
nfl = 0 # fortran line counter
@ -877,14 +869,13 @@ def reformat_ffile(infile, outfile, logFile=sys.stderr, indent_size=2, whitespac
(133 - 2 * is_omp_conditional -
len(line.lstrip(' '))) + line.lstrip(' '))
if not VAR_DECL_RE.match(f_line):
logFile.write("*** " + orig_filename + ":" + str(stream.line_nr) +
logging.warning("*** " + orig_filename + ":" + str(stream.line_nr) +
": auto indentation failed due to 132 chars limit, line should be splitted. ***\n")
else:
outfile.write(orig_line)
logFile.write("*** " + orig_filename + ":" + str(stream.line_nr) +
logging.warning("*** " + orig_filename + ":" + str(stream.line_nr) +
(": auto indentation and whitespace formatting failed due to 132 chars limit, line should be splitted. ***\n"))
if debug:
sys.stderr.write(' ' * ind_use + line + '\n')
logging.debug(' ' * ind_use + line + '\n')
# no indentation of semicolon separated lines
if re.search(r";\s*$", f_line, RE_FLAGS):

View file

@ -1,5 +1,6 @@
import re
import sys
import logging
repl = {
'routine_name': 'routineN',
@ -10,8 +11,7 @@ specialRepl = None
def replaceWords(infile, outfile, replacements=repl,
specialReplacements=specialRepl,
logFile=sys.stderr):
specialReplacements=specialRepl):
"""Replaces the words in infile writing the output to outfile.
replacements is a dictionary with the words to replace.

View file

@ -5,7 +5,7 @@ import re
import tempfile
import os
import os.path
import tempfile
import logging
try:
from hashlib import md5
@ -66,7 +66,7 @@ def upcaseOMP(line):
return TO_UPCASE_OMP_RE.sub(lambda match: match.group("toUpcase").upper(), line)
def upcaseKeywords(infile, outfile, upcase_omp, logFile=sys.stderr):
def upcaseKeywords(infile, outfile, upcase_omp):
"""Writes infile to outfile with all the fortran keywords upcased"""
while 1:
line = infile.readline()
@ -81,7 +81,7 @@ def upcaseKeywords(infile, outfile, upcase_omp, logFile=sys.stderr):
def prettifyFile(infile, filename, normalize_use, decl_linelength, decl_offset,
reformat, indent, whitespace, upcase_keywords,
upcase_omp, replace, logFile):
upcase_omp, replace):
"""prettifyes the fortran source in infile into a temporary file that is
returned. It can be the same as infile.
if normalize_use normalizes the use statements (defaults to true)
@ -104,7 +104,7 @@ def prettifyFile(infile, filename, normalize_use, decl_linelength, decl_offset,
try:
if replace:
tmpfile2 = tempfile.TemporaryFile(mode="w+")
replacer.replaceWords(ifile, tmpfile2, logFile=logFile)
replacer.replaceWords(ifile, tmpfile2)
tmpfile2.seek(0)
if tmpfile:
tmpfile.close()
@ -112,7 +112,7 @@ def prettifyFile(infile, filename, normalize_use, decl_linelength, decl_offset,
ifile = tmpfile
if reformat: # reformat needs to be done first
tmpfile2 = tempfile.TemporaryFile(mode="w+")
reformatFortranFile.reformat_ffile(ifile, tmpfile2, logFile=logFile,
reformatFortranFile.reformat_ffile(ifile, tmpfile2,
indent_size=indent, whitespace=whitespace,
orig_filename=orig_filename)
tmpfile2.seek(0)
@ -124,7 +124,7 @@ def prettifyFile(infile, filename, normalize_use, decl_linelength, decl_offset,
tmpfile2 = tempfile.TemporaryFile(mode="w+")
normalizeFortranFile.rewriteFortranFile(ifile, tmpfile2, indent,
decl_linelength, decl_offset,
logFile, orig_filename=orig_filename)
orig_filename=orig_filename)
tmpfile2.seek(0)
if tmpfile:
tmpfile.close()
@ -132,7 +132,7 @@ def prettifyFile(infile, filename, normalize_use, decl_linelength, decl_offset,
ifile = tmpfile
if upcase_keywords:
tmpfile2 = tempfile.TemporaryFile(mode="w+")
upcaseKeywords(ifile, tmpfile2, upcase_omp, logFile)
upcaseKeywords(ifile, tmpfile2, upcase_omp)
tmpfile2.seek(0)
if tmpfile:
tmpfile.close()
@ -147,7 +147,7 @@ def prettifyFile(infile, filename, normalize_use, decl_linelength, decl_offset,
raise RuntimeError(
"Prettify did not converge in", max_pretty_iter, "steps.")
except:
logFile.write("error processing file '" + infile.name + "'\n")
logging.critical("error processing file '" + infile.name + "'\n")
raise
@ -260,6 +260,8 @@ def main(argv=None):
""" + str(defaultsDict))
replace = None
debug=False
if "--help" in argv:
sys.stderr.write(usageDesc + '\n')
return(0)
@ -306,14 +308,21 @@ def main(argv=None):
sys.stderr.write("file " + fileName + " does not exists!\n")
else:
stdout = defaultsDict['stdout'] or fileName == 'stdin'
try:
if defaultsDict['report-errors']:
logFile = sys.stderr
if defaultsDict['report-errors']:
if debug:
level=logging.DEBUG
else:
logFile = open(os.devnull, "w")
level=logging.INFO
else:
level=logging.CRITICAL
logging.basicConfig(stream=sys.stderr, level=level)
try:
prettfyInplace(fileName, bkDir=bkDir,
stdout=stdout,
logFile=logFile,
normalize_use=defaultsDict[
'normalize-use'],
decl_linelength=defaultsDict[