a5ec78440d94 — cedricbonhomme 13 years ago
Initial commit. Let's have fun
2 files changed, 142 insertions(+), 0 deletions(-)

A => gitsyn.py
A => watcher.py
A => gitsyn.py +79 -0
@@ 0,0 1,79 @@ 
+
+
+import git
+
+
+REPOSITORIES = ["/home/cedric/test-repo-1"]
+
+
+from subprocess import *
+
+def execute_command(cmd):
+    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()
+
+
+class GitSync(object):
+    """
+    """
+    def __init__(self, repository_url, local_url):
+        """
+        """
+        self.repository_url = repository_url
+        self.local_url = local_url
+    
+    def initialize(self):
+        """
+        """
+        
+        self.repo = git.Repo.init(self.repository)
+        assert self.repo.bare == False
+
+    def clone(self):
+        """
+        """
+        execute_command("hg clone " + self.repository_url)
+
+    def get_commits(self, nb=5):
+        """
+        """
+        return self.repo.iter_commits('master', max_count=nb)
+
+    def untracked_files(self):
+        """
+        """
+        return self.untracked_files
+
+    def add(self, new_file):
+        """
+        """
+        execute_command("cd test-balloon; hg add " + new_file)
+        execute_command("cd test-balloon; hg commit -u cedricbonhomme -m 'Added " + new_file + "'")
+        execute_command("cd test-balloon; hg push")
+    
+    def modify(self, the_file):
+        """
+        """
+        execute_command("cd test-balloon; hg commit -u cedricbonhomme -m 'Updated " + the_file + "'")
+        execute_command("cd test-balloon; hg push")
+    
+    def remove(self, file_to_remove):
+        """
+        """
+        execute_command("cd test-balloon; hg remove " + file_to_remove)
+        execute_command("cd test-balloon; hg commit -u cedricbonhomme -m 'Removed " + file_to_remove + "'")
+        execute_command("cd test-balloon; hg push")
+    
+
+
+
+
+
+if __name__ == "__main__":
+    repo = GitSync("https://cedricbonhomme@bitbucket.org/cedricbonhomme/test-balloon", ".")
+    #repo.initialize()
+    repo.clone()
+    
+    
  No newline at end of file

          
A => watcher.py +63 -0
@@ 0,0 1,63 @@ 
+
+
+
+import os
+import pyinotify
+
+import gitsyn
+
+
+
+
+
+wm = pyinotify.WatchManager()
+mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY | pyinotify.IN_MOVED_TO
+
+class PTmp(pyinotify.ProcessEvent):
+    def __init__(self):
+        """
+        """
+        self.repo = gitsyn. GitSync("https://cedricbonhomme:***@bitbucket.org/cedricbonhomme/test-balloon", ".")
+        self.repo.clone()
+    
+    def process_IN_CREATE(self, event):
+        if ".hg" not in event.path and "hg-" not in event.path:
+            print "Create: %s " % os.path.join(event.path, event.name)
+            self.repo.add(os.path.join(event.path, event.name))
+        
+    def process_IN_DELETE(self, event):
+        if ".hg" not in event.path and "hg-" not in event.path:
+            print "Delete: %s " % os.path.join(event.path, event.name)
+            self.repo.remove(os.path.join(event.path, event.name))
+        
+        
+    def process_IN_MODIFY(self, event):
+        if ".hg" not in event.path and "hg-" not in event.path:
+            print "Modify: %s " % os.path.join(event.path, event.name)
+            self.repo.modify(os.path.join(event.path, event.name))
+        
+        
+    def process_IN_MOVED_TO(self, event):
+        if ".hg" not in event.path and "hg-" not in event.path:
+            print "Renamed: %s " % os.path.join(event.path, event.name)
+
+
+
+
+
+
+notifier = pyinotify.Notifier(wm, PTmp())
+
+
+
+wdd = wm.add_watch('/home/cedric/balloon/test-balloon', mask, rec=True)
+
+
+while True:
+    try:
+        notifier.process_events()
+        if notifier.check_events():
+            notifier.read_events()
+    except KeyboardInterrupt:
+        notifier.stop()
+        break
  No newline at end of file