Commits

Cédric Bonhomme  committed 9e6e1be

Major improvement. Modifications are now pushed every n commits (but are pushed immediately when renaming a file). The goal is to avoid a heavy use of the network.

  • Participants
  • Parent commits 3112b0a

Comments (0)

Files changed (2)

 __copyright__ = "Copyright (c) Cedric Bonhomme"
 __license__ = "GPLv3"
 
+import time
+import threading
 
 from subprocess import *
 
     try:
         p = Popen(cmd, shell=True, bufsize=1024, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
         (child_stdin, child_stdout, child_stderr) = (p.stdin, p.stdout, p.stderr)
+        print child_stderr.read()
+        print child_stdout.read()
     except Exception, e:
         print e
-    print child_stderr.read()
-    print child_stdout.read()
 
 
 class HgSync(object):
         self.repository_url = repository_url
         self.local_url = local_url
         self.username = username
+        
+        self.nb_commit = 0
+        thread = threading.Thread(None, self.push_loop, None)
+        thread.start()
     
     def initialize(self):
         """
         """
         execute_command("hg clone " + self.repository_url)
 
+    def commit(self, message):
+        """
+        """
+        execute_command("cd test-balloon; hg commit -u " + self.username + " -m '" + message + "'")
+        self.nb_commit += 1 
+
     def add(self, new_file):
         """
         Add new_file to the list of tracked files.
         """
         execute_command("cd test-balloon; hg add " + new_file)
-        execute_command("cd test-balloon; hg commit -u " + self.username + " -m 'Added " + new_file + "'")
-        execute_command("cd test-balloon; hg push")
+        self.commit("Added " + new_file)
     
     def modify(self, the_file):
         """
         Modify a file.
         """
         execute_command("cd test-balloon; hg commit -u " + self.username + " -m 'Updated " + the_file + "'")
-        execute_command("cd test-balloon; hg push")
+        self.commit("Updated " + the_file)
     
     def remove(self, file_to_remove):
         """
         Remove a file.
         """
         execute_command("cd test-balloon; hg remove " + file_to_remove)
-        execute_command("cd test-balloon; hg commit -u " + self.username + " -m 'Removed " + file_to_remove + "'")
-        execute_command("cd test-balloon; hg push")
+        self.commit("Removed " + file_to_remove)
 
     def rename(self, file_from, file_to):
         """
         """
         self.remove(file_from)
         self.add(file_to)
-    
+        self.push()
+
+    def push_loop(self):
+        """
+        Push changes every n commits in order to avoid to avoid a heavy use of the network.
+        """
+        while True:
+            time.sleep(1)
+            if self.nb_commit == 5:
+                execute_command("cd test-balloon; hg push")
+                self.nb_commit = 0
+
+    def push(self):
+        """
+        """
+        execute_command("cd test-balloon; hg push")
+        self.nb_commit = 0
+
 
 if __name__ == "__main__":
     # Point of entry in execution mode.
         Called when a file has been created.
         """
         if "hg-check" not in event.name:
-            print "Create: %s " % os.path.join(event.path, event.name)
+            print "Created: %s " % os.path.join(event.path, event.name)
             self.repo.add(os.path.join(event.path, event.name))
         
     def process_IN_DELETE(self, event):
         Called when a file has been deleted.
         """
         if "hg-check" not in event.name:
-            print "Delete: %s " % os.path.join(event.path, event.name)
+            print "Deleted: %s " % os.path.join(event.path, event.name)
             self.repo.remove(os.path.join(event.path, event.name))
         
     def process_IN_MODIFY(self, event):
         Called when a file has been modified.
         """
         if "hg-check" not in event.name:
-            print "Modify: %s " % os.path.join(event.path, event.name)
+            print "Modified: %s " % os.path.join(event.path, event.name)
             self.repo.modify(os.path.join(event.path, event.name))
         
     def process_IN_MOVED_FROM(self, event):