| 1 |
|
|---|
| 2 |
import difflib; |
|---|
| 3 |
import logging; |
|---|
| 4 |
import os; |
|---|
| 5 |
import os.path; |
|---|
| 6 |
import re; |
|---|
| 7 |
import time; |
|---|
| 8 |
|
|---|
| 9 |
from OptionHandler import OptionHandler; |
|---|
| 10 |
from OperationBundle import OperationBundle; |
|---|
| 11 |
|
|---|
| 12 |
class ConversionDriver: |
|---|
| 13 |
""" Driver for the operations |
|---|
| 14 |
""" |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
def __init__(self): |
|---|
| 27 |
self.fileRegEx = None; |
|---|
| 28 |
self.diffFile = None; |
|---|
| 29 |
self.opBundle = OperationBundle(); |
|---|
| 30 |
|
|---|
| 31 |
self.currFileName = None; |
|---|
| 32 |
self.currFilePath = None; |
|---|
| 33 |
self.currFileContent = None; |
|---|
| 34 |
self.currFileOrigContent = None; |
|---|
| 35 |
|
|---|
| 36 |
self.logName = "ConversionDriver"; |
|---|
| 37 |
|
|---|
| 38 |
consoleLogLevel = logging.ERROR; |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
if OptionHandler.getOptionActive("verbose"): |
|---|
| 42 |
if OptionHandler.getOptionArg("verbose") == 1: |
|---|
| 43 |
consoleLogLevel = logging.WARNING; |
|---|
| 44 |
elif OptionHandler.getOptionArg("verbose") == 2: |
|---|
| 45 |
consoleLogLevel = logging.INFO; |
|---|
| 46 |
elif OptionHandler.getOptionArg("verbose") >= 3: |
|---|
| 47 |
consoleLogLevel = logging.DEBUG; |
|---|
| 48 |
|
|---|
| 49 |
logging.basicConfig(level = consoleLogLevel); |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
if OptionHandler.getOptionActive("logFile"): |
|---|
| 53 |
fileLogName = OptionHandler.getOptionArg("logFile"); |
|---|
| 54 |
fileLogHandler = logging.FileHandler(fileLogName, "w"); |
|---|
| 55 |
fileLogFormatter = logging.Formatter( |
|---|
| 56 |
"%(asctime)s %(name)-12s %(levelname)-8s %(message)s"); |
|---|
| 57 |
|
|---|
| 58 |
fileLogHandler.setLevel(logging.DEBUG); |
|---|
| 59 |
fileLogHandler.setFormatter(fileLogFormatter); |
|---|
| 60 |
|
|---|
| 61 |
logging.getLogger("").addHandler(fileLogHandler); |
|---|
| 62 |
|
|---|
| 63 |
def getDiffFile(self): |
|---|
| 64 |
""" Return the file object for the diff file. |
|---|
| 65 |
""" |
|---|
| 66 |
return self.diffFile; |
|---|
| 67 |
|
|---|
| 68 |
def getCurrFilePath(self): |
|---|
| 69 |
""" Return the path to the current file. |
|---|
| 70 |
""" |
|---|
| 71 |
return self.currFilePath; |
|---|
| 72 |
|
|---|
| 73 |
def getCurrFileName(self): |
|---|
| 74 |
""" Return the name of the current file. |
|---|
| 75 |
""" |
|---|
| 76 |
return self.currFileName; |
|---|
| 77 |
|
|---|
| 78 |
def getCurrFileContent(self): |
|---|
| 79 |
""" Return a string containing the contents of the current file, |
|---|
| 80 |
including modificatins be alreay applied operations. |
|---|
| 81 |
""" |
|---|
| 82 |
return self.currFileContent; |
|---|
| 83 |
|
|---|
| 84 |
def setCurrFileContent(self, fileContent): |
|---|
| 85 |
""" Set the current files contents to the string fileContent. |
|---|
| 86 |
""" |
|---|
| 87 |
self.currFileContent = fileContent; |
|---|
| 88 |
|
|---|
| 89 |
def getCurrFileOrigContent(self): |
|---|
| 90 |
""" Return a string containing the original contetns of the current file. |
|---|
| 91 |
""" |
|---|
| 92 |
return self.currFileOrigContent; |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
def processFiles(self): |
|---|
| 96 |
log = logging.getLogger(self.logName); |
|---|
| 97 |
log.debug(">> process"); |
|---|
| 98 |
|
|---|
| 99 |
for path in OptionHandler.getArgList()[1:]: |
|---|
| 100 |
log.info("path: \"%s\"." % path); |
|---|
| 101 |
if not OptionHandler.getOptionActive("followSymlink") and os.path.islink(path): |
|---|
| 102 |
log.info("skipping path - is symlink and followSymlink is off."); |
|---|
| 103 |
continue; |
|---|
| 104 |
|
|---|
| 105 |
if os.path.isdir(path): |
|---|
| 106 |
for root, dirs, files in os.walk(path): |
|---|
| 107 |
if not OptionHandler.getOptionActive("followSymlink"): |
|---|
| 108 |
filter(lambda d: not os.path.islink(d), dirs); |
|---|
| 109 |
filter(lambda f: not os.path.islink(f), files); |
|---|
| 110 |
|
|---|
| 111 |
for file in files: |
|---|
| 112 |
self.processFile(root, file); |
|---|
| 113 |
|
|---|
| 114 |
elif os.path.isfile(path): |
|---|
| 115 |
self.processFile("./", path); |
|---|
| 116 |
log.debug("<< process"); |
|---|
| 117 |
|
|---|
| 118 |
def testFileRegEx(self, filePath, fileName): |
|---|
| 119 |
if self.fileRegEx != None: |
|---|
| 120 |
return self.fileRegEx.match(fileName); |
|---|
| 121 |
|
|---|
| 122 |
return True; |
|---|
| 123 |
|
|---|
| 124 |
def testFileAccess(self, filePath, fileName): |
|---|
| 125 |
fullFilePath = os.path.join(filePath, fileName); |
|---|
| 126 |
return os.access(fullFilePath, os.F_OK) and os.access(fullFilePath, os.R_OK | os.W_OK); |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
def processFile(self, filePath, fileName): |
|---|
| 130 |
log = logging.getLogger(self.logName); |
|---|
| 131 |
log.debug(">> processFile"); |
|---|
| 132 |
|
|---|
| 133 |
self.currFilePath = filePath; |
|---|
| 134 |
self.currFileName = fileName; |
|---|
| 135 |
fullFilePath = os.path.join(filePath, fileName); |
|---|
| 136 |
|
|---|
| 137 |
log.info(" File: \"%s\"." % fullFilePath); |
|---|
| 138 |
|
|---|
| 139 |
fileLastMTime = time.ctime(os.stat(fullFilePath).st_mtime); |
|---|
| 140 |
fileNewMTime = time.ctime(); |
|---|
| 141 |
|
|---|
| 142 |
if not self.testFileRegEx(filePath, fileName): |
|---|
| 143 |
log.info("File ignored - pattern does not match.") |
|---|
| 144 |
log.debug("<< processing file."); |
|---|
| 145 |
return; |
|---|
| 146 |
|
|---|
| 147 |
if not self.testFileAccess(filePath, fileName): |
|---|
| 148 |
log.warning("File ignored - insufficient file permissions."); |
|---|
| 149 |
log.debug("<< processing file."); |
|---|
| 150 |
return; |
|---|
| 151 |
|
|---|
| 152 |
log.info("reading file \"%s\"." % fullFilePath); |
|---|
| 153 |
currFile = open(fullFilePath, "r"); |
|---|
| 154 |
self.currFileContent = currFile.read(); |
|---|
| 155 |
self.currFileOrigContent = self.currFileContent[:]; |
|---|
| 156 |
currFile.close(); |
|---|
| 157 |
|
|---|
| 158 |
log.debug("applying bundle"); |
|---|
| 159 |
self.opBundle.applyBundle(self); |
|---|
| 160 |
|
|---|
| 161 |
udiff = difflib.unified_diff(self.currFileOrigContent.split("\n"), |
|---|
| 162 |
self.currFileContent.split("\n"), |
|---|
| 163 |
fullFilePath, fullFilePath, |
|---|
| 164 |
fileLastMTime, fileNewMTime, lineterm=""); |
|---|
| 165 |
|
|---|
| 166 |
if OptionHandler.getOptionActive("diffFile"): |
|---|
| 167 |
for line in udiff: |
|---|
| 168 |
log.info(line); |
|---|
| 169 |
self.diffFile.write(line); |
|---|
| 170 |
self.diffFile.write("\n"); |
|---|
| 171 |
else: |
|---|
| 172 |
for line in udiff: |
|---|
| 173 |
log.info(line); |
|---|
| 174 |
|
|---|
| 175 |
if OptionHandler.getOptionActive("noOp"): |
|---|
| 176 |
log.info("Changes NOT written."); |
|---|
| 177 |
log.debug("<< processing file."); |
|---|
| 178 |
return; |
|---|
| 179 |
|
|---|
| 180 |
log.info("writing modified file \"%s\"." % fullFilePath); |
|---|
| 181 |
currFile = open(fullFilePath, "w"); |
|---|
| 182 |
currFile.write(self.currFileContent); |
|---|
| 183 |
currFile.flush(); |
|---|
| 184 |
currFile.close(); |
|---|
| 185 |
log.debug("<< processing file."); |
|---|
| 186 |
|
|---|
| 187 |
def preProcess(self): |
|---|
| 188 |
log = logging.getLogger(self.logName); |
|---|
| 189 |
log.debug(">> preprocess"); |
|---|
| 190 |
if OptionHandler.getOptionActive("filePattern"): |
|---|
| 191 |
log.debug("Creating filePattern reg ex."); |
|---|
| 192 |
self.fileRegEx = re.compile(OptionHandler.getOptionArg("filePattern")); |
|---|
| 193 |
|
|---|
| 194 |
if OptionHandler.getOptionActive("diffFile"): |
|---|
| 195 |
diffFileName = OptionHandler.getOptionArg("diffFile"); |
|---|
| 196 |
log.debug("Opening diff file \"%s\"." % diffFileName); |
|---|
| 197 |
self.diffFile = open(diffFileName, "w+"); |
|---|
| 198 |
|
|---|
| 199 |
if OptionHandler.getOptionActive("operations"): |
|---|
| 200 |
log.debug("Populating operation bundle."); |
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
opNameArgList = OptionHandler.getOptionArg("operations"); |
|---|
| 205 |
for opNameArg in opNameArgList: |
|---|
| 206 |
opNameList = opNameArg.split(":"); |
|---|
| 207 |
|
|---|
| 208 |
for opName in opNameList: |
|---|
| 209 |
log.debug("adding operation \"%s\"." % opName); |
|---|
| 210 |
self.opBundle.addOp(opName); |
|---|
| 211 |
log.debug("<< preprocess"); |
|---|
| 212 |
|
|---|
| 213 |
def postProcess(self): |
|---|
| 214 |
log = logging.getLogger(self.logName); |
|---|
| 215 |
log.debug(">> postprocess"); |
|---|
| 216 |
if OptionHandler.getOptionActive("diffFile"): |
|---|
| 217 |
log.debug("Closing diff file \"%s\"." % self.diffFile.name); |
|---|
| 218 |
self.diffFile.flush(); |
|---|
| 219 |
self.diffFile.close(); |
|---|
| 220 |
log.debug("<< postprocess"); |
|---|
| 221 |
|
|---|
| 222 |
def run(self): |
|---|
| 223 |
self.preProcess(); |
|---|
| 224 |
self.processFiles(); |
|---|
| 225 |
self.postProcess(); |
|---|