#from __future__ import with_statement from fabric import Connection from util import find_data_file from util import setup_child from util import fprint from util import macos from invoke import exceptions import sys def sftp_send_data(config, filename, filetype): setup_child() if not macos: fprint("Connecting over SSH to " + config['sftp']['host']) c = Connection(host=config['sftp']['host'], user=config['sftp']['user'], port=config['sftp']['port'], connect_kwargs={"key_filename": find_data_file(config['sftp']['keyfile']),}) fprint("Sending data over SFTP: " + filename) fprint(c.put(find_data_file(filename), remote=config['sftp']['filepath'][filetype])) fprint("Data sent over SFTP successfully") #command = 'ls ' + config['sftp']['filepath'][filetype] #fprint(c.run(command)) def check_for_file(config, filename, location): setup_child() fprint("Connecting over SSH to " + config['sftp']['host']) c = Connection(host=config['sftp']['host'], user=config['sftp']['user'], port=config['sftp']['port'], connect_kwargs={"key_filename": find_data_file(config['sftp']['keyfile']),}) fprint("Checking for existence of file " + config['sftp']['filepath'][location] + "/" + filename) try: res = c.run("ls -l " + config['sftp']['filepath'][location] + "/" + filename, hide=True) fprint("File " + filename + " exists!") return c.run("cat " + config['sftp']['filepath'][location] + "/" + filename, hide=True) except exceptions.UnexpectedExit: return False def run_ssh(config, command, location): setup_child() fprint("Connecting over SSH to " + config['sftp']['host']) c = Connection(host=config['sftp']['host'], user=config['sftp']['user'], port=config['sftp']['port'], connect_kwargs={"key_filename": find_data_file(config['sftp']['keyfile']),}) fprint("cd to " + config['sftp']['filepath'][location]) with c.cd(config['sftp']['filepath'][location]): fprint("Running ssh command: " + command) res = c.run(command, hide=True, asynchronous=True) return res