Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 1e9db847eb32b276dad9402d2b0b34bcb7b2aec2 @tnhu committed Jun 27, 2012
Showing with 129 additions and 0 deletions.
  1. +17 −0 Main.sublime-menu
  2. +99 −0 SimpleSync.py
  3. +13 −0 SimpleSync.sublime-settings
@@ -0,0 +1,17 @@
+[{
+ "caption": "Preferences",
+ "mnemonic": "n",
+ "id": "preferences",
+ "children": [{
+ "caption": "Package Settings",
+ "mnemonic": "P",
+ "id": "package-settings",
+ "children": [{
+ "command": "open_file",
+ "args": {
+ "file": "${packages}/SimpleSync/SimpleSync.sublime-settings"
+ },
+ "caption": "SimpleSync Settings"
+ }]
+ }]
+}]
@@ -0,0 +1,99 @@
+#
+# Sublime Text 2 SimpleSync plugin
+#
+# Help the orphans, street children, disadvantaged people
+# and physically handicapped in Vietnam (http://bit.ly/LPgJ1m)
+#
+# Copyright (c) Tan Nhu, tnhu AT me . COM
+# @version 0.0.1
+# @licence MIT
+#
+import sublime
+import sublime_plugin
+import subprocess
+import threading
+
+#
+# Run a process
+# @param cmd process command
+#
+def runProcess(cmd):
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+
+ while (True):
+ retcode = p.poll() #returns None while subprocess is running
+ line = p.stdout.readline()
+ yield line
+
+ if (retcode is not None):
+ break
+
+# Populate settings
+settings = sublime.load_settings("SimpleSync.sublime-settings")
+sync = settings.get("sync")
+
+#
+# Get sync item(s) for a file
+# @param local_file full path of a local file
+# @return sync item(s)
+#
+def getSyncItem(local_file):
+ ret = []
+
+ for item in sync:
+ if local_file.startswith(item["local"]):
+ ret += [item]
+
+ return ret
+
+#
+# ScpCopier does actual copying using threading to avoid UI blocking
+#
+class ScpCopier(threading.Thread):
+ def __init__(self, host, username, local_file, remote_file):
+ self.host = host
+ self.username = username
+ self.local_file = local_file
+ self.remote_file = remote_file
+
+ threading.Thread.__init__(self)
+
+ def run(self):
+ arg = self.username + "@" + self.host + ":" + self.remote_file
+
+ print "SimpleSync: ", self.local_file, " -> ", self.remote_file
+
+ for line in runProcess(["scp", self.local_file, arg]):
+ print line,
+
+#
+# LocalCopier does local copying using threading to avoid UI blocking
+#
+class LocalCopier(threading.Thread):
+ def __init__(self, local_file, remote_file):
+ self.local_file = local_file
+ self.remote_file = remote_file
+ threading.Thread.__init__(self)
+
+ def run(self):
+ print "SimpleSync: ", self.local_file, " -> ", self.remote_file
+
+ for line in runProcess(['cp', self.local_file, self.remote_file]):
+ print line,
+
+#
+# Subclass sublime_plugin.EventListener
+#
+class SimpleSync(sublime_plugin.EventListener):
+ def on_post_save(self, view):
+ local_file = view.file_name()
+ syncItems = getSyncItem(local_file)
+
+ if (len(syncItems) > 0):
+ for item in syncItems:
+ remote_file = local_file.replace(item["local"], item["remote"])
+
+ if (item["type"] == "ssh"):
+ ScpCopier(item["host"], item["username"], local_file, remote_file).start()
+ elif (item["type"] == "local"):
+ LocalCopier(local_file, remote_file).start()
@@ -0,0 +1,13 @@
+{
+ "sync": [{
+ "type" : "ssh",
+ "host" : "tnhu-ld",
+ "username" : "tnhu",
+ "local" : "/Users/tnhu/workspace/trunk",
+ "remote" : "/home/tnhu/workspace/trunk"
+ }, {
+ "type" : "local",
+ "local" : "/Users/tnhu/Library/Application Support/Sublime Text 2/Packages/SimpleSync",
+ "remote" : "/Users/tnhu/Dropbox/projects/SimpleSync"
+ }]
+}

0 comments on commit 1e9db84

Please sign in to comment.