From 29a790167d698f307a45c6d2cbaa2ea151dfa3e1 Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Tue, 17 Jul 2012 10:56:08 +0200 Subject: [PATCH] WIP - fixing bugs --- cagibi/cagibi.py | 35 ++++++++++++++++++++++++++++++++--- cagibi/server.py | 10 +++++++--- cagibi/watch.py | 5 ++--- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/cagibi/cagibi.py b/cagibi/cagibi.py index d44dc92..0f1607e 100644 --- a/cagibi/cagibi.py +++ b/cagibi/cagibi.py @@ -15,10 +15,10 @@ cagibi_folder = "." def upload_local_changes(modified, added, removed): - pattern = "%s/files/%s/hashes" % server_url for file in modified: - url = pattern % file + 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") @@ -30,6 +30,35 @@ def upload_local_changes(modified, added, removed): post_data["deltas"] = json.dumps(deltas) post_string = urllib.urlencode(post_data) fd = urllib2.urlopen(url, post_string) + results = json.load(fd) + + local_files = load_config("files.json") + local_files[file]["rev"] = results["rev"] + save_config("files.json") + + opener = urllib2.build_opener(urllib2.HTTPHandler) + + for file in added: + put_data = {} + fd = open(secure_path(cagibi_folder, file), "r") + put_data["contents"] = fd.read() + put_string = urllib.urlencode(put_data) + fd.close() + + url = "%s/files/%s" % (server_url, file) + request = urllib2.Request(url, data=put_string) + request.add_header('Content-Type', 'application/json') + request.get_method = lambda: 'PUT' + url = opener.open(request) + + for file in removed: + url = "%s/files/%s" % (server_url, file) + request = urllib2.Request(url, data=put_string) + request.get_method = lambda: 'DELETE' + url = opener.open(request) + + + def checkout_upstream_changes(): """Checkout changes on the server""" @@ -94,7 +123,7 @@ if __name__ == "__main__": if "folder" in client_config: cagibi_folder = client_config["folder"] - fw = FileWatcher() + fw = FileWatcher(path=cagibi_folder) fw.addHandler(upload_local_changes) checkout_upstream_changes() fw.watch() diff --git a/cagibi/server.py b/cagibi/server.py index 0f793dd..2a5f5a2 100644 --- a/cagibi/server.py +++ b/cagibi/server.py @@ -4,6 +4,7 @@ import os, time import json from config import load_config, save_config from rsync import rsyncdelta, blockchecksums, patchstream +from util import secure_path cagibi_folder = "." server_config = {} @@ -28,6 +29,7 @@ def create_file(filename): - filename : the filename - contents : the contents """ + #FIXME: handle directories filename = os.path.basename(filename) contents = request.forms.get('contents') @@ -48,13 +50,14 @@ def delete_file(filename): filename = os.path.basename(filename) # FIXME: possible race condition + print filename in files_info if os.path.exists(filename) and filename in files_info: os.remove(secure_path(cagibi_folder, filename)) del files_info[filename] save_config(files_info, filename="files.json") return "Ok." else: - abort(501, "File doesn't exist or is not in database.") + abort(500, "File doesn't exist or is not in database.") @get('/files/') def file_info(filename): @@ -70,6 +73,7 @@ def file_hashes(filename): """Return the hashes of a file""" unpatched = open(secure_path(cagibi_folder, filename), "rb") hashes = blockchecksums(unpatched) + print hashes unpatched.close() return json.dumps(hashes) @@ -91,10 +95,10 @@ def update_file_hashes(filename): file_copy.close() save_to.close() - files_info[filename] = {"rev": 1} + files_info[filename] = {"rev": files_info[filename]["rev"] + 1} save_config(files_info, filename="files.json") - pass + return files_info[filename] @post('/files//deltas') def return_deltas(filename): diff --git a/cagibi/watch.py b/cagibi/watch.py index f33734f..f9a3243 100644 --- a/cagibi/watch.py +++ b/cagibi/watch.py @@ -8,8 +8,7 @@ class FileWatcher: self.paused = False def addHandler(self, fn): - """add an handler which is called everytime a change is detected. An handler function is a function which is called - whenever a file change is detected. + """add an handler which is called everytime a change is detected. This function takes three parameters : a list of files modified, a list of files added and a list of files removed.""" self.handlers.append(fn) @@ -22,7 +21,7 @@ class FileWatcher: after = dict ([(f, None) for f in os.listdir (self.path)]) added = [f for f in after if not f in before] removed = [f for f in before if not f in after] - modified = [f for f in after if os.path.getmtime(f) > elapsed_time] + modified = [f for f in after if os.path.getmtime(os.path.join(self.path, f)) > elapsed_time and f not in added] for handler in self.handlers: handler(modified, added, removed) -- 2.43.0