aboutsummaryrefslogtreecommitdiffstats
path: root/Offlink.app/Contents/MacOS
diff options
context:
space:
mode:
authorFilipp Lepalaan <filipp@mac.com>2016-04-29 19:45:46 +0300
committerFilipp Lepalaan <filipp@mac.com>2016-04-29 19:45:46 +0300
commit0369aa7d20d464e96098144dbd6edb9067e5a383 (patch)
tree49225cda6c7abc6b3bdda97b290186feee31144c /Offlink.app/Contents/MacOS
downloadofflink-master.tar.gz
offlink-master.tar.bz2
offlink-master.zip
Initial commitHEADmaster
Diffstat (limited to 'Offlink.app/Contents/MacOS')
-rwxr-xr-xOfflink.app/Contents/MacOS/dropletbin0 -> 25028 bytes
-rwxr-xr-xOfflink.app/Contents/MacOS/helper.py100
2 files changed, 100 insertions, 0 deletions
diff --git a/Offlink.app/Contents/MacOS/droplet b/Offlink.app/Contents/MacOS/droplet
new file mode 100755
index 0000000..4f7d67b
--- /dev/null
+++ b/Offlink.app/Contents/MacOS/droplet
Binary files differ
diff --git a/Offlink.app/Contents/MacOS/helper.py b/Offlink.app/Contents/MacOS/helper.py
new file mode 100755
index 0000000..686c194
--- /dev/null
+++ b/Offlink.app/Contents/MacOS/helper.py
@@ -0,0 +1,100 @@
+#!/usr/bin/env python
+
+import os.path
+import plistlib
+import subprocess
+
+PREF = os.path.expanduser("~/Library/Preferences/com.github.filipp.offlink.plist")
+
+def open_plist():
+ try:
+ return plistlib.readPlist(PREF)
+ except Exception:
+ d = dict(folders=[])
+ return plistlib.writePlist(d, PREF)
+
+def is_linked(path):
+ """
+ Returns True if path is in the linked list
+
+ >>> is_linked("/tmp2")
+ False
+ """
+ plist = open_plist()
+ for x in plist['folders']:
+ if x['source'] == path:
+ return True
+ return False
+
+def add_path(source, target):
+ """
+ Adds source and target to list of synced folders
+
+ >>> add_path('/blaa', '/bluu') #doctest: +ELLIPSIS
+ Traceback (most recent call last):
+ ...
+ ValueError...
+ >>> add_path('/var', '/tmp')
+ /var linked with /tmp
+ """
+ plist = open_plist()
+ for path in (source, target,):
+ if not os.path.exists(path):
+ raise ValueError("%s is not a valid path" % path)
+ f = dict(source=source, target=target)
+ plist['folders'] = plist['folders'] + [f]
+ plistlib.writePlist(plist, PREF)
+
+ print("%s linked with %s" % (source, target))
+
+def forget_path(path):
+ """
+ Removes this source from the linked folders list
+
+ >>> forget_path("/var")
+ /var removed from synced list
+ """
+ plist = open_plist()
+ newlist = []
+
+ for x in plist['folders']:
+ if x['source'] != path:
+ newlist.append(x)
+ plist['folders'] = newlist
+ plistlib.writePlist(plist, PREF)
+
+ print("%s removed from synced list" % path)
+
+def sync(source, target):
+ subprocess.call(["/usr/bin/rsync", "-auE", source, target])
+ print("%s synced with %s" % (source, target))
+
+def source_to_target():
+ plist = open_plist()
+
+ for x in plist['folders']:
+ sync(x['source'], x['target'])
+
+def target_to_source():
+ plist = open_plist()
+
+ for x in plist['folders']:
+ sync(x['target'], x['source'])
+
+
+if __name__ == '__main__':
+ import sys
+ import doctest
+
+ if len(sys.argv) == 1:
+ doctest.testmod()
+ sys.exit()
+
+ if sys.argv[1] == 'add':
+ add_path(sys.argv[2], sys.argv[3])
+ if sys.argv[1] == 'forget':
+ forget_path(sys.argv[2])
+ if sys.argv[1] == 'check':
+ sys.exit(255) if is_linked(sys.argv[2]) else sys.exit(0)
+ if sys.argv[1] == 'sync':
+ source_to_target()