From 481059fbbfd7d6bbf1a3b65cbb808d3200859c48 Mon Sep 17 00:00:00 2001 From: Eugen Konkov Date: Thu, 14 May 2015 19:49:48 +0300 Subject: [PATCH 01/10] Implemented INSTALL_PREFIX env variable Now you can 'INSTALL_PREFIX=~/bin ./ssh-copy-rsub [user@]host' to install into home directory --- ssh-copy-rsub | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ssh-copy-rsub b/ssh-copy-rsub index 30a2b71..310938d 100755 --- a/ssh-copy-rsub +++ b/ssh-copy-rsub @@ -3,6 +3,11 @@ # Shell script to install rsub on the remove server so we can open shell scripts locally. # Author: Leon Radley (http://github.com/leon) +if [ -z "$INSTALL_PREFIX" ] ; then + INSTALL_PREFIX="/usr/local/bin" +fi + + URL="https://raw.github.com/textmate/rmate/master/rmate" if [ "$#" -gt 1 ] || [ "$1" = "-b" ] || [ "$1" = "--bash" ]; then @@ -14,6 +19,9 @@ if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then cat < /usr/local/bin/rsub; chmod a+rx /usr/local/bin/rsub' || exit 1 +wget --quiet -O - "${URL}" | ssh ${1%:} "umask 077; test -d $INSTALL_PREFIX || mkdir -p $INSTALL_PREFIX; cat > $INSTALL_PREFIX/rsub; chmod a+rx $INSTALL_PREFIX/rsub" || exit 1 cat < Date: Mon, 21 Sep 2015 11:26:06 +0300 Subject: [PATCH 02/10] IMPLEMENTED: method to remove empty folders --- rsub.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/rsub.py b/rsub.py index 5a77246..0efec83 100644 --- a/rsub.py +++ b/rsub.py @@ -36,6 +36,27 @@ def __init__(self, socket): self.socket = socket self.temp_path = None + # http://www.jacobtomlinson.co.uk/2014/02/16/python-script-recursively-remove-empty-folders-directories/ + def removeEmptyFolders( self, path, removeRoot=True ): + 'Function to remove empty folders' + if not os.path.isdir(path): + return + + # remove empty subfolders + files = os.listdir(path) + if len(files): + for f in files: + fullpath = os.path.join(path, f) + if os.path.isdir(fullpath): + self.removeEmptyFolders(fullpath) + + # if folder empty, delete it + files = os.listdir(path) + if len(files) == 0 and removeRoot: + print( "Removing empty folder:", path ) + os.rmdir(path) + + def parse_input(self, input_line): if (input_line.strip() == b"open" or self.parse_done is True): return From dce40ffa830b2bcab466ddf14516a15baea6223c Mon Sep 17 00:00:00 2001 From: Eugen Konkov Date: Mon, 21 Sep 2015 11:30:23 +0300 Subject: [PATCH 03/10] Cleanup variable names rename: temp_path -> temp_file --- rsub.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/rsub.py b/rsub.py index 0efec83..ac6b539 100644 --- a/rsub.py +++ b/rsub.py @@ -34,7 +34,7 @@ def __init__(self, socket): self.in_file = False self.parse_done = False self.socket = socket - self.temp_path = None + self.temp_file = None # http://www.jacobtomlinson.co.uk/2014/02/16/python-script-recursively-remove-empty-folders-directories/ def removeEmptyFolders( self, path, removeRoot=True ): @@ -92,13 +92,13 @@ def close(self): self.socket.send(b"\n") self.socket.shutdown(socket.SHUT_RDWR) self.socket.close() - os.unlink(self.temp_path) + os.unlink(self.temp_file) os.rmdir(self.temp_dir) def send_save(self): self.socket.send(b"save\n") self.socket.send(b"token: " + self.env['token'].encode("utf8") + b"\n") - temp_file = open(self.temp_path, "rb") + temp_file = open(self.temp_file, "rb") new_file = temp_file.read() temp_file.close() self.socket.send(b"data: " + str(len(new_file)).encode("utf8") + b"\n") @@ -114,17 +114,17 @@ def on_done(self): except OSError as e: sublime.error_message('Failed to create rsub temporary directory! Error: %s' % e) return - self.temp_path = os.path.join(self.temp_dir, + self.temp_file = os.path.join(self.temp_dir, os.path.basename(self.env['display-name'].split(':')[-1])) try: - temp_file = open(self.temp_path, "wb+") + temp_file = open( self.temp_file, "wb+" ) temp_file.write(self.file[:self.file_size]) temp_file.flush() temp_file.close() except IOError as e: # Remove the file if it exists. - if os.path.exists(self.temp_path): - os.remove(self.temp_path) + if os.path.exists( self.temp_file ): + os.remove( self.temp_file ) try: os.rmdir(self.temp_dir) except OSError: @@ -137,7 +137,7 @@ def on_done(self): sublime.run_command("new_window") # Open it within sublime - view = sublime.active_window().open_file(self.temp_path) + view = sublime.active_window().open_file( self.temp_file ) # Add the file metadata to the view's settings # This is mostly useful to obtain the path of this file on the server From 44796d58831cf406d341fcc8e8b0a9106df55957 Mon Sep 17 00:00:00 2001 From: Eugen Konkov Date: Mon, 21 Sep 2015 11:41:45 +0300 Subject: [PATCH 04/10] IMPLEMENTED: WORKDIR This is ROOT temporary working directory --- rsub.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/rsub.py b/rsub.py index ac6b539..3aebefd 100644 --- a/rsub.py +++ b/rsub.py @@ -20,6 +20,7 @@ SESSIONS = {} server = None +WORKDIR = None def say(msg): @@ -87,13 +88,14 @@ def parse_file(self, line): self.file += line def close(self): + global WORKDIR self.socket.send(b"close\n") self.socket.send(b"token: " + self.env['token'].encode("utf8") + b"\n") self.socket.send(b"\n") self.socket.shutdown(socket.SHUT_RDWR) self.socket.close() os.unlink(self.temp_file) - os.rmdir(self.temp_dir) + os.rmdir(WORKDIR) def send_save(self): self.socket.send(b"save\n") @@ -106,11 +108,12 @@ def send_save(self): self.socket.send(b"\n") def on_done(self): + global WORKDIR # Create a secure temporary directory, both for privacy and to allow # multiple files with the same basename to be edited at once without # overwriting each other. try: - self.temp_dir = tempfile.mkdtemp(prefix='rsub-') + self.temp_dir = WORKDIR except OSError as e: sublime.error_message('Failed to create rsub temporary directory! Error: %s' % e) return @@ -126,7 +129,7 @@ def on_done(self): if os.path.exists( self.temp_file ): os.remove( self.temp_file ) try: - os.rmdir(self.temp_dir) + os.rmdir(WORKDIR) except OSError: pass @@ -207,7 +210,7 @@ def on_close(self, view): def plugin_loaded(): - global SESSIONS, server + global SESSIONS, WORKDIR, server # Load settings settings = sublime.load_settings("rsub.sublime-settings") @@ -219,6 +222,13 @@ def plugin_loaded(): Thread(target=start_server, args=[]).start() say('Server running on ' + host + ':' + str(port) + '...') + try: + WORKDIR = tempfile.mkdtemp(prefix='rsub-') + except OSError as e: + sublime.error_message('Failed to create rsub temporary working directory! Error: %s' % e) + return + + # call the plugin_loaded() function if running in sublime text 2 if (int(sublime.version())< 3000): plugin_loaded() From bf9bdbc2e4e8f7b4093c71f057958c8018bc98af Mon Sep 17 00:00:00 2001 From: Eugen Konkov Date: Mon, 21 Sep 2015 11:47:35 +0300 Subject: [PATCH 05/10] Remove empty folders recursively --- rsub.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rsub.py b/rsub.py index 3aebefd..43dcaa5 100644 --- a/rsub.py +++ b/rsub.py @@ -95,7 +95,11 @@ def close(self): self.socket.shutdown(socket.SHUT_RDWR) self.socket.close() os.unlink(self.temp_file) - os.rmdir(WORKDIR) + try: + self.removeEmptyFolders( WORKDIR, True ) + except OSError as e: + sublime.error_message( 'Can not clean WORKDIR: %s' % e ) + def send_save(self): self.socket.send(b"save\n") @@ -129,9 +133,9 @@ def on_done(self): if os.path.exists( self.temp_file ): os.remove( self.temp_file ) try: - os.rmdir(WORKDIR) - except OSError: - pass + self.removeEmptyFolders( self.temp_dir ) + except OSError as e: + sublime.error_message( 'Can not clean WORKDIR: %s' % e ) sublime.error_message('Failed to write to temp file! Error: %s' % str(e)) From 249f28a705a4abd03842a65d69a442eba3c08f99 Mon Sep 17 00:00:00 2001 From: Eugen Konkov Date: Mon, 21 Sep 2015 11:53:11 +0300 Subject: [PATCH 06/10] Create full directory tree structure on local host as on remote Here we relay on sublime feature when we open same file Also if we open files with same names sublime will distinguish them and pretty show additional directory path on tabs --- rsub.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rsub.py b/rsub.py index 43dcaa5..8657aec 100644 --- a/rsub.py +++ b/rsub.py @@ -116,8 +116,13 @@ def on_done(self): # Create a secure temporary directory, both for privacy and to allow # multiple files with the same basename to be edited at once without # overwriting each other. + + self.env['host'] = self.env['display-name'].split(':')[0] + self.temp_dir = WORKDIR +"/" +self.env['host'] +os.path.dirname( self.env['real-path'] ) + try: - self.temp_dir = WORKDIR + if not os.path.exists( self.temp_dir ) : + os.makedirs( self.temp_dir ) except OSError as e: sublime.error_message('Failed to create rsub temporary directory! Error: %s' % e) return From b9ca5cda4d6bc80769cc00db2c6726e5e59d491b Mon Sep 17 00:00:00 2001 From: Eugen Konkov Date: Mon, 21 Sep 2015 11:55:51 +0300 Subject: [PATCH 07/10] Reformatting code for readability --- rsub.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rsub.py b/rsub.py index 8657aec..cc58675 100644 --- a/rsub.py +++ b/rsub.py @@ -126,8 +126,10 @@ def on_done(self): except OSError as e: sublime.error_message('Failed to create rsub temporary directory! Error: %s' % e) return - self.temp_file = os.path.join(self.temp_dir, - os.path.basename(self.env['display-name'].split(':')[-1])) + self.temp_file = os.path.join( + self.temp_dir, + os.path.basename( self.env['display-name'].split(':')[-1] ) + ) try: temp_file = open( self.temp_file, "wb+" ) temp_file.write(self.file[:self.file_size]) From bbcc0dfb6fae42be0a7740a2462cd5ea122ceb68 Mon Sep 17 00:00:00 2001 From: Eugen Konkov Date: Mon, 21 Sep 2015 11:59:42 +0300 Subject: [PATCH 08/10] Revert "Implemented INSTALL_PREFIX env variable" This reverts commit 481059fbbfd7d6bbf1a3b65cbb808d3200859c48. --- ssh-copy-rsub | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/ssh-copy-rsub b/ssh-copy-rsub index 310938d..30a2b71 100755 --- a/ssh-copy-rsub +++ b/ssh-copy-rsub @@ -3,11 +3,6 @@ # Shell script to install rsub on the remove server so we can open shell scripts locally. # Author: Leon Radley (http://github.com/leon) -if [ -z "$INSTALL_PREFIX" ] ; then - INSTALL_PREFIX="/usr/local/bin" -fi - - URL="https://raw.github.com/textmate/rmate/master/rmate" if [ "$#" -gt 1 ] || [ "$1" = "-b" ] || [ "$1" = "--bash" ]; then @@ -19,9 +14,6 @@ if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then cat < $INSTALL_PREFIX/rsub; chmod a+rx $INSTALL_PREFIX/rsub" || exit 1 +wget --quiet -O - "${URL}" | ssh ${1%:} 'umask 077; test -d /usr/local/bin || mkdir -p /usr/local/bin; cat > /usr/local/bin/rsub; chmod a+rx /usr/local/bin/rsub' || exit 1 cat < Date: Mon, 21 Sep 2015 14:53:18 +0300 Subject: [PATCH 09/10] TYPO FIX --- rsub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rsub.py b/rsub.py index cc58675..e077f6f 100644 --- a/rsub.py +++ b/rsub.py @@ -141,7 +141,7 @@ def on_done(self): os.remove( self.temp_file ) try: self.removeEmptyFolders( self.temp_dir ) - except OSError as e: + except OSError as e: sublime.error_message( 'Can not clean WORKDIR: %s' % e ) sublime.error_message('Failed to write to temp file! Error: %s' % str(e)) From 37c1b6135230e2efb0a64976ad424533babb43f7 Mon Sep 17 00:00:00 2001 From: Eugen Konkov Date: Mon, 21 Sep 2015 17:58:11 +0300 Subject: [PATCH 10/10] Move removeEmptyFolders out of class --- rsub.py | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/rsub.py b/rsub.py index e077f6f..69a4ca0 100644 --- a/rsub.py +++ b/rsub.py @@ -26,6 +26,27 @@ def say(msg): print('[rsub] ' + msg) +# http://www.jacobtomlinson.co.uk/2014/02/16/python-script-recursively-remove-empty-folders-directories/ +def removeEmptyFolders( path, removeRoot=True ): + 'Function to remove empty folders' + if not os.path.isdir(path): + return + + # remove empty subfolders + files = os.listdir(path) + if len(files): + for f in files: + fullpath = os.path.join(path, f) + if os.path.isdir(fullpath): + removeEmptyFolders(fullpath) + + # if folder empty, delete it + files = os.listdir(path) + if len(files) == 0 and removeRoot: + print( "Removing empty folder:", path ) + os.rmdir(path) + + class Session: def __init__(self, socket): @@ -37,26 +58,6 @@ def __init__(self, socket): self.socket = socket self.temp_file = None - # http://www.jacobtomlinson.co.uk/2014/02/16/python-script-recursively-remove-empty-folders-directories/ - def removeEmptyFolders( self, path, removeRoot=True ): - 'Function to remove empty folders' - if not os.path.isdir(path): - return - - # remove empty subfolders - files = os.listdir(path) - if len(files): - for f in files: - fullpath = os.path.join(path, f) - if os.path.isdir(fullpath): - self.removeEmptyFolders(fullpath) - - # if folder empty, delete it - files = os.listdir(path) - if len(files) == 0 and removeRoot: - print( "Removing empty folder:", path ) - os.rmdir(path) - def parse_input(self, input_line): if (input_line.strip() == b"open" or self.parse_done is True): @@ -96,7 +97,7 @@ def close(self): self.socket.close() os.unlink(self.temp_file) try: - self.removeEmptyFolders( WORKDIR, True ) + removeEmptyFolders( WORKDIR, True ) except OSError as e: sublime.error_message( 'Can not clean WORKDIR: %s' % e ) @@ -140,9 +141,9 @@ def on_done(self): if os.path.exists( self.temp_file ): os.remove( self.temp_file ) try: - self.removeEmptyFolders( self.temp_dir ) + removeEmptyFolders( self.temp_dir ) except OSError as e: - sublime.error_message( 'Can not clean WORKDIR: %s' % e ) + sublime.error_message( 'Can not clean WORKDIR: %s' % e ) sublime.error_message('Failed to write to temp file! Error: %s' % str(e))