changeset 92509f1b24f7 in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=92509f1b24f7
description:
        style: Make the style fixers safe

        Adds a wrapper to the fix functions of the verifiers. This wrapper first
        copies the original file to a backup file, then performs the fix. If an
        error occurs, the backup file is used to restore the original file.

        Also fixed a line-length error in verifiers.py

diffstat:

 util/style/verifiers.py |  46 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 41 insertions(+), 5 deletions(-)

diffs (77 lines):

diff -r e9096175eb38 -r 92509f1b24f7 util/style/verifiers.py
--- a/util/style/verifiers.py   Tue Aug 02 13:35:47 2016 +0100
+++ b/util/style/verifiers.py   Wed Aug 03 11:10:46 2016 -0500
@@ -56,6 +56,40 @@
 from region import *
 from file_types import lang_type
 
+
+def safefix(fix_func):
+    """ Decorator for the fix functions of the Verifier class.
+        This function wraps the fix function and creates a backup file
+        just in case there is an error.
+    """
+    def safefix_wrapper(*args, **kwargs):
+        # Check to be sure that this is decorating a function we expect:
+        # a class method with filename as the first argument (after self)
+        assert(os.path.exists(args[1]))
+        self = args[0]
+        assert(is_verifier(self.__class__))
+        filename = args[1]
+
+        # Now, Let's make a backup file.
+        from shutil import copyfile
+        backup_name = filename+'.bak'
+        copyfile(filename, backup_name)
+
+        # Try to apply the fix. If it fails, then we revert the file
+        # Either way, we need to clean up our backup file
+        try:
+            fix_func(*args, **kwargs)
+        except Exception as e:
+            # Restore the original file to the backup file
+            self.ui.write("Error! Restoring the original file.\n")
+            copyfile(backup_name, filename)
+            raise
+        finally:
+            # Clean up the backup file
+            os.remove(backup_name)
+
+    return safefix_wrapper
+
 def _modified_regions(old, new):
     try:
         m = SequenceMatcher(a=old, b=new, autojunk=False)
@@ -122,11 +156,11 @@
         return f
 
     def skip(self, filename):
-        # We never want to handle symlinks, so always skip them: If the 
location
-        # pointed to is a directory, skip it. If the location is a file inside
-        # the gem5 directory, it will be checked as a file, so symlink can be
-        # skipped. If the location is a file outside gem5, we don't want to
-        # check it anyway.
+        # We never want to handle symlinks, so always skip them: If the
+        # location pointed to is a directory, skip it. If the location is a
+        # file inside the gem5 directory, it will be checked as a file, so
+        # symlink can be skipped. If the location is a file outside gem5, we
+        # don't want to check it anyway.
         if os.path.islink(filename):
             return True
         return lang_type(filename) not in self.languages
@@ -202,6 +236,7 @@
         f.close()
         return errors
 
+    @safefix
     def fix(self, filename, regions=all_regions):
         f = self.open(filename, 'r+')
 
@@ -318,6 +353,7 @@
 
         return 0
 
+    @safefix
     def fix(self, filename, regions=all_regions):
         f = self.open(filename, 'r+')
 
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to