[PATCH] Add features to copyfile module.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



        This patch is a combination of my two previous patches with a little 
code duplication removed.  Also, the base directory for the patch is now the 
top level of the source tree, not the directory above that.

         Applying this patch to the current Func source (pulled from the git 
repo) adds the following features:

* If the source and target file are identical then the target will not be 
overwritten.

* Adds a --force option to overwrite the target file even if it is identical 
to the source file.

* Adds a --backup option to make a timestamped backup of the target file 
before it is overwritten.

--
Marcus
diff -rupN func/minion/modules/copyfile.py func.new/minion/modules/copyfile.py
--- func/minion/modules/copyfile.py	2011-01-01 11:45:52.000000000 -0500
+++ func.new/minion/modules/copyfile.py	2011-01-01 11:48:11.000000000 -0500
@@ -26,6 +26,7 @@ import time
 import shutil
 
 import func_module
+from func import utils as func_utils
 
 
 class CopyFile(func_module.FuncModule):
@@ -40,28 +41,39 @@ class CopyFile(func_module.FuncModule):
         return thissum.hexdigest()
 
     def checksum(self, thing):
-
         CHUNK=2**16
         thissum = hashlib.new('sha1')
         if os.path.exists(thing):
-            fo = open(thing, 'r', CHUNK)
-            chunk = fo.read
-            while chunk:
+            fo = open(thing, 'r')
+            while True:
                 chunk = fo.read(CHUNK)
+                if not chunk:
+                    break
                 thissum.update(chunk)
             fo.close()
             del fo
         else:
-            # assuming it's a string of some kind
+	    # assuming it's a string of some kind
             thissum.update(thing)
 
-        return thissum.hexdigest()
+        hexdig = thissum.hexdigest()
+        return hexdig
 
-    def open(self, filepath, mode=None, uid=-1, gid=-1):
+    def open(self, filepath, remote_sum, mode=None, uid=-1, gid=-1, backup=False, force=False):
         dirpath = os.path.dirname(filepath)
         if not os.path.exists(dirpath):
             os.makedirs(dirpath)
 
+        if os.path.exists(filepath):
+	    if not force:
+                local_sum = self.checksum(filepath)
+                if remote_sum == local_sum:
+                    return 0
+
+	    if backup:
+                if not self._backuplocal(filepath):
+                    return -1
+
         # Create empty file
         try:
             fo = open(filepath, 'w')
@@ -82,7 +94,14 @@ class CopyFile(func_module.FuncModule):
 
         return filepath
 
-    def append(self, filepath, filebuf):
+    def append(self, open_result, filebuf):
+        hostname = func_utils.get_hostname_by_route()
+        filepath = open_result[hostname]
+
+        # If the result of the open function was 0 (or -1), do not append, return the same value.
+	if type(filepath) == type(0):
+	    return filepath
+
         if not os.path.exists(filepath):
             # file disaperead
             return -1
diff -rupN func/overlord/cmd_modules/copyfile.py func.new/overlord/cmd_modules/copyfile.py
--- func/overlord/cmd_modules/copyfile.py	2011-01-01 11:45:52.000000000 -0500
+++ func.new/overlord/cmd_modules/copyfile.py	2011-01-01 11:48:11.000000000 -0500
@@ -34,6 +34,8 @@ class CopyFile(base_command.BaseCommand)
                                action="store")
         self.parser.add_option("", "--remotepath", dest="remotepath",
                                 action="store")
+        self.parser.add_option("", "--backup", dest="backup",
+                               action="store_true")
         self.parser.add_option("", "--force", dest="force",
                                action="store_true")
         self.parser.add_option("-v", "--verbose", dest="verbose",
@@ -50,4 +52,4 @@ class CopyFile(base_command.BaseCommand)
         self.server_spec = self.parentCommand.server_spec
         self.getOverlord()
 
-        return self.overlord_obj.local.copyfile.send(self.options.filename, self.options.remotepath)
+        return self.overlord_obj.local.copyfile.send(self.options.filename, self.options.remotepath, self.options.backup, self.options.force)
diff -rupN func/overlord/modules/copyfile.py func.new/overlord/modules/copyfile.py
--- func/overlord/modules/copyfile.py	2011-01-01 12:08:48.000000000 -0500
+++ func.new/overlord/modules/copyfile.py	2011-01-01 12:10:49.000000000 -0500
@@ -7,7 +7,7 @@ import xmlrpclib
 from func.overlord import overlord_module
 
 class copyfile(overlord_module.BaseModule):
-    def send(self, localpath, remotepath, bufsize=60000):
+    def send(self, localpath, remotepath, backup=None, force=None, bufsize=60000):
         try:
             f = open(localpath, "r")
         except IOError, e:
@@ -19,12 +19,19 @@ class copyfile(overlord_module.BaseModul
         uid = st.st_uid
         gid = st.st_gid
 
-        self.parent.run("copyfile", "open", [remotepath, mode, uid, gid])
+	if force:
+            local_sum = -1
+        else:
+            import func.minion.modules.copyfile as CopyFile
+            cf = CopyFile.CopyFile()
+            local_sum = cf.checksum(localpath)
+
+        open_result = self.parent.run("copyfile", "open", [remotepath, local_sum, mode, uid, gid, backup, force])
 
         while True:
             data=f.read(bufsize)
             if data:
-                self.parent.run("copyfile", "append", [remotepath, xmlrpclib.Binary(data)])
+                self.parent.run("copyfile", "append", [open_result, xmlrpclib.Binary(data)])
             else:
                 break
 
_______________________________________________
Func-list mailing list
Func-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/func-list

[Index of Archives]     [Fedora Users]     [Linux Networking]     [Fedora Legacy List]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux