]> khamidou — GitHub mirrors - cagibi.git/commitdiff
WIP - using base64 to transmit binary data
authorkarim hamidou <khamidou@gmail.com>
Mon, 23 Jul 2012 08:09:48 +0000 (10:09 +0200)
committerkarim hamidou <khamidou@gmail.com>
Mon, 23 Jul 2012 08:09:48 +0000 (10:09 +0200)
cagibi/cagibi.py
cagibi/rsync.py
cagibi/server.py

index f9c99ad145e1b19591aa3292d8535682d63e1d07..1bdf92e0f526f8fe5827890665bcb2329a73ca72 100644 (file)
@@ -18,6 +18,7 @@ import json
 import time
 import os
 import threading
+import base64
 
 
 client_config = {}
@@ -51,11 +52,12 @@ def upload_local_changes():
     while not mqueue["modified"].empty():
         file = mqueue["modified"].get()
         print "Detect %s modified" % file
+
         url = "%s/files/%s/hashes" % (server_url, file)
         fd = urllib2.urlopen(url)
         hashes = json.load(fd)
         patchedfile = open(secure_path(cagibi_folder, file), "rb")
-        deltas = list(rsyncdelta(patchedfile, hashes))
+        deltas = encode_deltas(rsyncdelta(patchedfile, hashes))
         patchedfile.close()
 
         # Send the deltas to the server.
@@ -74,6 +76,8 @@ def upload_local_changes():
 
     while not mqueue["added"].empty():
         file = mqueue["added"].get()
+        print "Detected %s added" % file
+
         put_data = {}
         fd = open(secure_path(cagibi_folder, file), "r")
         put_data["contents"] = fd.read()
@@ -88,6 +92,8 @@ def upload_local_changes():
 
     while not mqueue["removed"].empty():
         file = mqueue["removed"].get()
+        print "Detect %s removed" % file
+
         url = "%s/files/%s" % (server_url, file)
         request = urllib2.Request(url)
         request.get_method = lambda: 'DELETE'
@@ -117,7 +123,7 @@ def checkout_upstream_changes():
             if server_files[file]["rev"] > local_files[file]["rev"]:
                 # Get it too, but using the rsync algo
 
-                unpatched = open(secure_path(cagibi_folder, file).encode('ascii', 'ignore'), "rb")
+                unpatched = open(secure_path(cagibi_folder, file), "rb")
                 hashes = list(blockchecksums(unpatched))
                 json_hashes = json.dumps(hashes)
                 post_data = {}
@@ -126,7 +132,7 @@ def checkout_upstream_changes():
 
                 url = "%s/files/%s/deltas" % (server_url, file) 
                 fd = urllib2.urlopen(url, post_string)
-                json_response = json.load(fd)
+                json_response = decode_deltas(json.load(fd))
 
                 unpatched.seek(0)
                 save_to = os.tmpfile()
@@ -136,7 +142,7 @@ def checkout_upstream_changes():
                 save_to.seek(0)
                 
                 # FIXME: rename instead of copying ?
-                file_copy = open(secure_path(cagibi_folder, file).encode('ascii', 'ignore'), "w+b")
+                file_copy = open(secure_path(cagibi_folder, file), "w+b")
                 file_copy.write(save_to.read())
                 file_copy.close()
                 save_to.close()
index 0ef4fb0c4ef53b73b6546eb029880998791c70b5..fbcd1d3824659c4036b53a42c5bd90621d85938d 100644 (file)
@@ -26,6 +26,7 @@ http://samba.anu.edu.au/rsync/.
 
 import collections
 import hashlib
+import base64
 
 if not(hasattr(__builtins__, "bytes")) or str is bytes:
     # Python 2.x compatibility
@@ -36,7 +37,7 @@ if not(hasattr(__builtins__, "bytes")) or str is bytes:
             return map(ord, var)
 
 __all__ = ["rollingchecksum", "weakchecksum", "patchstream", "rsyncdelta",
-    "blockchecksums"]
+    "blockchecksums", "encode_deltas", "decode_deltas"]
 
 
 def rsyncdelta(datastream, remotesignatures, blocksize=4096):
@@ -174,3 +175,27 @@ def weakchecksum(data):
         b += (l - i)*data[i]
 
     return (b << 16) | a, a, b
+
+def encode_deltas(deltas):
+    """
+    Returns base64-encoded rsync deltas
+    """
+    def discriminator(element):
+        if isinstance(element, unicode):
+            return base64.b64encode(element)
+        else:
+            return element
+
+    return map(discriminator, deltas)
+
+def decode_deltas(deltas):
+    """
+    Return normal unicode strings in base64-encoded rsync deltas
+    """
+    def discriminator(element):
+        if isinstance(element, str):
+            return base64.b64decode(element)
+        else:
+            return element
+
+    return map(discriminator, deltas)
index 21c8a728484c2c0a2a404e95f3fbfa02b4395c9d..f138241e0be78c1be54058157c55cccac53d19c4 100644 (file)
@@ -2,8 +2,9 @@
 from bottle import Bottle, route, run, post, get, put, delete, request, abort, static_file
 import os, time
 import json
+import base64
 from config import load_config, save_config
-from rsync import rsyncdelta, blockchecksums, patchstream
+from rsync import rsyncdelta, blockchecksums, patchstream, encode_deltas, decode_deltas
 from util import secure_path
 
 cagibi_folder = "."
@@ -87,7 +88,8 @@ def file_hashes(filename):
 @post('/files/<filename>/hashes')
 def update_file_hashes(filename):
     """Updates a file using the deltas received by a client"""
-    deltas = json.loads(request.forms.get("deltas"))
+    deltas = decode_deltas(json.loads(request.forms.get("deltas")))
+    print deltas
 
     unpatched = open(secure_path(cagibi_folder, filename), "rb")
     save_to = os.tmpfile()
@@ -97,7 +99,7 @@ def update_file_hashes(filename):
     save_to.seek(0)
 
     # FIXME: rename instead of copying ?
-    file_copy = open(secure_path(cagibi_folder, filename).encode('ascii', 'ignore'), "w+b")
+    file_copy = open(secure_path(cagibi_folder, filename), "w+b")
     file_copy.write(save_to.read())
     file_copy.close()
     save_to.close()
@@ -112,7 +114,7 @@ def return_deltas(filename):
     """return the deltas corresponding to the file. The client must send in its request the hashes of the file"""  
     hashes = json.loads(request.forms.get("hashes"))
     patchedfile = open(secure_path(cagibi_folder, filename), "rb")
-    deltas = rsyncdelta(patchedfile, hashes)
+    deltas = encode_deltas(rsyncdelta(patchedfile, hashes))
     patchedfile.close()
 
     return json.dumps(deltas)