Created
July 13, 2014 20:20
-
-
Save BDHesse/364b9b2faa854f06aa27 to your computer and use it in GitHub Desktop.
downloadFileFromSFTP.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
from errno import ENOTDIR, ENOENT | |
import socket | |
import editor | |
import console | |
import paramiko | |
# import getpass | |
import os | |
import sys | |
import stat | |
def chdir_pythonista_document_root(): | |
''' | |
change the current working directory to the base documents for pythonista | |
''' | |
curr_dir = editor.get_path() | |
while True: | |
# split the current directory | |
head, tail = os.path.split(curr_dir) | |
if tail == 'Documents': | |
os.chdir(curr_dir) | |
break | |
curr_dir = head | |
def sftp_walktree(sftp, top, callback): | |
""" | |
File to walk the SFTP Tree of a given folder | |
and sending it's full path to a callback function. | |
Only can handle directories and regular files at this time. | |
This is recursive due to the limits of the sftp. | |
Revise to be iterative? | |
""" | |
for f in sftp.listdir(top): | |
pathname = os.path.join(top, f) | |
mode = sftp.stat(pathname).st_mode | |
if stat.S_ISDIR(mode): | |
sftp_walktree(sftp, pathname, callback) | |
elif stat.S_ISREG(mode): | |
callback(pathname) | |
else: | |
print('Skipping %s' % pathname, file=sys.stderr) | |
def progress(curr, totl): | |
print('%05d of %05d bytes' % (curr, totl)) | |
def main(): | |
username = 'ssh login' | |
password = 'sftp password' | |
username, password = console.login_alert('SFTP LOGIN', 'Please provide server login details:', username, password, 'Login') | |
hostname = 'sftp.server.address' | |
hostport = 22 | |
remote_dir = 'my_pythonista_files' | |
local_dir = 'Downloaded' | |
sftp = None | |
try: | |
transport = paramiko.Transport((hostname,hostport)) | |
transport.connect(username=username, password=password) | |
sftp = paramiko.SFTPClient.from_transport(transport) | |
except socket.error,e: | |
print(str(e), file=sys.stderr) | |
except paramiko.SSHException, e: | |
print(str(e), file=sys.stderr) | |
if sftp: | |
chdir_pythonista_document_root() | |
if not os.path.isdir(local_dir): | |
try: | |
os.mkdir(local_dir) | |
except (OSError, IOError), e: | |
console.hud_alert(str(e), 'error') | |
raise e | |
print('\nSelect the index of the file you wish to download:') | |
files = [] | |
console.show_activity() | |
sftp_walktree(sftp, remote_dir, files.append) | |
console.hide_activity() | |
for index, filename in enumerate(files): | |
print("%3d:\t%s"%(index, filename)) | |
choice = '-1' | |
try: | |
# this always throws a keyboard interrupt when hide_cancle_button is true | |
choice = console.input_alert('Choose','Which file(s) would you like to download? (non-valid values cancel)', '-1', 'Download', True) | |
except KeyboardInterrupt, e: | |
console.hud_alert(str(e), 'error') | |
try: | |
choice = [int(i) for i in choice.split()] | |
except ValueError, e: | |
console.hud_alert(str(e), 'error') | |
raise e | |
console.show_activity() | |
for index, filename in enumerate(files): | |
if index in choice: | |
local_path = os.path.join(local_dir, os.path.split(filename)[1]) | |
try: | |
sftp.get(filename, local_path, callback=progress) | |
except IOError, e: | |
raise e | |
else: | |
console.hud_alert('Got file '+local_path) | |
console.hide_activity() | |
sftp.close() | |
transport.close() | |
console.hud_alert('Complete') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment