Skip to content

Commit

Permalink
Merge pull request #13 from cfpettersson/master
Browse files Browse the repository at this point in the history
Python2 and Python3 compatible, fix bug with files in subfolders.
  • Loading branch information
hnsl authored Apr 11, 2017
2 parents 66470ea + a94501d commit d24ebba
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions unox.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@

import sys
import os
import time
import urllib
import traceback
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import signal

# Import depending on python version
if sys.version_info.major < 3:
from urllib import quote, unquote
else:
from urllib.parse import quote, unquote


def sigint_handler(signal, frame):
sys.exit(0)
sys.exit(0)


signal.signal(signal.SIGINT, sigint_handler)

Expand All @@ -47,12 +53,13 @@ def sigint_handler(signal, frame):

# Dict of pending replicas that are beeing waited on.
# Replica hash mapped to True if replica is pending.
pending_reps = {}
pending_reps = {}

# Dict of triggered replicas.
# Replica hash mapped to recursive dict where keys are path tokens or True for pending leaf.
triggered_reps = {}


def format_exception(e):
# Thanks for not bundling this function in the Python library Guido. *facepalm*
exception_list = traceback.format_stack()
Expand All @@ -64,6 +71,7 @@ def format_exception(e):
exception_str = exception_str[:-1]
return exception_str


def _debug_triggers():
global pending_reps, triggered_reps
if not _in_debug_plus:
Expand All @@ -73,31 +81,38 @@ def _debug_triggers():
wait_info = " | wait=" + str(pending_reps)
sys.stderr.write(my_log_prefix + "[DEBUG+]: trig=" + str(triggered_reps) + wait_info + "\n")


def _debug(msg):
sys.stderr.write(my_log_prefix + "[DEBUG]: " + msg.strip() + "\n")


def warn(msg):
sys.stderr.write(my_log_prefix + "[WARN]: " + msg.strip() + "\n")


def sendCmd(cmd, args):
raw_cmd = cmd
for arg in args:
raw_cmd += " " + urllib.quote(arg);
raw_cmd += " " + quote(arg);
if _in_debug: _debug("sendCmd: " + raw_cmd)
sys.stdout.write(raw_cmd + "\n")


# Safely injects a command to send from non-receive context.
def injectCmd(cmd, args):
sendCmd(cmd, args)
sys.stdout.flush()


def sendAck():
sendCmd("OK", [])


def sendError(msg):
sendCmd("ERROR", [msg])
os._exit(1)


def recvCmd():
# We flush before stalling on read instead of
# flushing every write for optimization purposes.
Expand All @@ -116,16 +131,18 @@ def recvCmd():
words = line.strip().split(" ")
args = []
for word in words[1:]:
args.append(urllib.unquote(word))
args.append(unquote(word))
return [words[0], args]


def pathTokenize(path):
path_toks = []
for path_tok in path.split("/"):
if len(path_tok) > 0:
path_toks.append(path_tok)
return path_toks


def triggerReplica(replica, local_path_toks):
global pending_reps, triggered_reps
if replica in pending_reps:
Expand Down Expand Up @@ -194,7 +211,7 @@ def startReplicaMon(replica, fspath, path):
# and compare against a snapshot. This means there's no point in us doing it, better leave it to Unison.
if _in_debug: _debug("replica:[" + replica + "] watching path [" + fspath + "]")
handler = Handler(fspath, replica)
watch = observer.schedule(handler, fspath)
watch = observer.schedule(handler, fspath, recursive=True)
except Exception as e:
sendError(str(e))
replicas[replica] = {
Expand All @@ -213,13 +230,15 @@ def startReplicaMon(replica, fspath, path):
else:
sendError("unexpected cmd in replica start: " + cmd)


def reportRecursiveChanges(local_path, cur_lvl):
if (cur_lvl == True):
sendCmd("RECURSIVE", [local_path])
return
for path_tok, new_lvl in cur_lvl.items():
reportRecursiveChanges(os.path.join(local_path, path_tok), new_lvl);


def main():
global replicas, pending_reps, triggered_reps
# Version handshake.
Expand Down Expand Up @@ -287,6 +306,7 @@ def main():
else:
sendError("unexpected root cmd: " + cmd)


if __name__ == '__main__':
try:
main()
Expand Down

0 comments on commit d24ebba

Please sign in to comment.