from watch import FileWatcher
from rsync import *
-from config import load_config
+from config import load_config, save_config
import urllib, urllib2
import json
import os
url = "%s/files/%s/data" % (server_url, file)
urllib.urlretrieve(url, os.path.join(cagibi_folder, file))
print "Retrieved %s" % file
+ modified = True
+
else:
if server_files[file]["rev"] > local_files[file]["rev"]:
# Get it too, but using the rsync algo
- pass
+
+ unpatched = open(os.path.join(cagibi_folder, file).encode('ascii', 'ignore'), "rb")
+ hashes = blockchecksums(unpatched)
+ json_hashes = json.dumps(hashes)
+ post_data = {}
+ post_data["hashes"] = json_hashes
+ post_string = urllib.urlencode(post_data)
+
+ url = "%s/files/%s/deltas" % (server_url, file)
+ fd = urllib2.urlopen(url, post_string)
+ json_response = json.load(fd)
+
+ unpatched.seek(0)
+ save_to = os.tmpfile()
+ patchstream(unpatched, save_to, json_response)
+ unpatched.close()
+ os.unlink(os.path.join(cagibi_folder, file))
+ save_to.seek(0)
+ file_copy = open(os.path.join(cagibi_folder, file).encode('ascii', 'ignore'), "w+b")
+ file_copy.write(save_to.read())
+ file_copy.close()
+ save_to.close()
+
+ local_files[file]["rev"] = server_files[file]["rev"]
+
+ if modified == True:
+ save_config(local_files, filename="files.json")
if __name__ == "__main__":
client_config = load_config("cagibi.json")
import os, time
import json
from config import load_config, save_config
+from rsync import rsyncdelta, blockchecksums, patchstream
cagibi_folder = "."
server_config = {}
@post('/files/<filename>/deltas')
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.load(request.forms.get("hashes"))
+ hashes = json.loads(request.forms.get("hashes"))
patchedfile = open(os.path.join(cagibi_folder, filename), "rb")
deltas = rsyncdelta(patchedfile, hashes)
patchedfile.close()