--- a/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py Tue Apr 29 18:02:02 2014 +0200 +++ b/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py Tue Apr 29 18:22:44 2014 +0200 @@ -48,7 +48,7 @@ Class implementing a fixer for certain code style issues. """ def __init__(self, filename, sourceLines, fixCodes, noFixCodes, - maxLineLength, inPlace, eol): + maxLineLength, inPlace, eol, backup=False): """ Constructor @@ -62,6 +62,8 @@ @param maxLineLength maximum allowed line length (integer) @param inPlace flag indicating to modify the file in place (boolean) @param eol end of line character(s) (string) + @param backup flag indicating to create a backup before fixing + anything (boolean) """ super(CodeStyleFixer, self).__init__() @@ -78,14 +80,13 @@ self.__indentWord = self.__getIndentWord() if inPlace: - # TODO: Do a backup before any changes depending on - # 'CreateBackupFile' editor config setting. - pass + self.__createBackup = backup else: self.__origName = self.__filename self.__filename = os.path.join( os.path.dirname(self.__filename), "fixed_" + os.path.basename(self.__filename)) + self.__createBackup = False self.__eol = eol self.__fixes = { @@ -188,6 +189,23 @@ # no need to write return + if self.__createBackup: + # create a backup file before writing any changes + if os.path.islink(self.__filename): + bfn = '{0}~'.format(os.path.realpath(self.__filename)) + else: + bfn = '{0}~'.format(self.__filename) + try: + os.remove(bfn) + except EnvironmentError: + # if there was an error, ignore it + pass + try: + os.rename(self.__filename, bfn) + except EnvironmentError: + # if there was an error, ignore it + pass + txt = "".join(self.__source) try: if sys.version_info[0] == 3: