fw = FileWatcher(path=cagibi_folder)
fw.addHandler(push_filewatcher_changes)
fw.watch()
- print "ended"
def push_filewatcher_changes(modified, added, removed):
for file in added:
while not mqueue["modified"].empty():
file = mqueue["modified"].get()
print "Detect %s modified" % file
-
+
url = "%s/files/%s/hashes" % (server_url, file)
try:
fd = urllib2.urlopen(url)
except HTTPError:
# The server may return us an error if things have gone south during
# the file creation process.
+ # If so, mark the file as added instead of modified.
+ print "mark as added instead"
mqueue["added"].put(file)
continue
hashes = json.load(fd)
try:
+ print "%f" % os.path.getmtime(secure_path(cagibi_folder, file))
patchedfile = open(secure_path(cagibi_folder, file), "rb")
deltas = encode_deltas(rsyncdelta(patchedfile, hashes))
patchedfile.close()
+ print "%f" % os.path.getmtime(secure_path(cagibi_folder, file))
except IOError:
"IOError - continuing"
continue
# Send the deltas to the server.
- print "deltas: %s" % deltas
post_data = {}
post_data["deltas"] = json.dumps(deltas)
post_string = urllib.urlencode(post_data)
local_files = load_config("files.json")
local_files[file]["rev"] = results["rev"]
save_config("files.json")
+ print "%f" % os.path.getmtime(secure_path(cagibi_folder, file))
opener = urllib2.build_opener(urllib2.HTTPHandler)
else:
if server_files[file]["rev"] > local_files[file]["rev"]:
# Get it too, but using the rsync algo
+ print "Retrieving file, using the rsync algorithm"
try:
unpatched = open(secure_path(cagibi_folder, file), "rb")
file_copy.write(save_to.read())
file_copy.close()
local_files[file]["rev"] = server_files[file]["rev"]
+ modified = True
except IOError:
continue
while not self.paused:
elapsed_time = time.time()
- time.sleep (10)
+ time.sleep(10)
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(os.path.join(self.path, f)) > elapsed_time and f not in added]
-
+
for handler in self.handlers:
handler(modified, added, removed)