def put(local_file, remote_file=None, verbose=False, permissions=None):
    #create new remote file if none specified
    if remote_file is None:
        suffix = engine.get_suffix(local_file)
        remote_file = engine.get_new_remote_file(suffix)

    #print some info
    out.log(local_file + " -> " + remote_file, 'upload')

    #choose correct transfer system
    if engine.TRANSFER_SYSTEM == 'FTP':
        #put file
        command = 'put ' + local_file + ' ' + ftp_path(remote_file)
        #set permissions on remote
        if permissions is not None:
            command += "\nchmod " + str(permissions) + " " + ftp_path(
                remote_file)
        #...execute!
        ftp.execute(command)
    elif engine.TRANSFER_SYSTEM == 'SSH':
        out.log("Error: TRANSFER_SYSTEM SSH not implemented yet", 'upload',
                out.LEVEL_ERROR)
    else:
        out.log("Error: Unknown TRANSFER_SYSTEM " + engine.TRANSFER_SYSTEM,
                'upload', out.LEVEL_ERROR)

    #return filename of uploaded file
    return remote_file
def get(remote_file, local_file=None, verbose=False, permissions=None):
    #create new local file if not spcefied
    if local_file is None:
        #keep suffix
        suffix = engine.get_suffix(remote_file)
        local_file = engine.get_new_local_file(suffix)

    #print some info
    out.log(remote_file + " -> " + local_file, 'download')

    #get transfer system
    if engine.TRANSFER_SYSTEM == 'FTP':
        command = 'get ' + ftp_path(remote_file) + ' ' + local_file
        ftp.execute(command)
    elif engine.TRANSFER_SYSTEM == 'SSH':
        out.log("Error: TRANSFER_SYSTEM SSH not implemented yet", 'download',
                out.LEVEL_ERROR)
        engine.quit()
    else:
        out.log("Error: Unknown TRANSFER_SYSTEM " + engine.TRANSFER_SYSTEM,
                'download', out.LEVEL_ERROR)
        engine.quit()

    #set permissions
    if permissions is not None:
        run.local('chmod ' + str(permissions) + ' ' + local_file)

    #return new filename
    return local_file
def get_multiple(file_list):
    import php
    #split into ascii and non-ascii
    ascii_files, non_ascii_files = engine.split_by_encoding(file_list)

    if engine.FORCE_FTP_FILE_TRANSFER:
        ascii_files = []
        non_ascii_files = file_list

    #ascii files
    if len(ascii_files) > 0:
        #use buffering
        had_buffer = php.has_buffer()
        php.start_buffer()

        remote_tar = tar.pack_remote_list(ascii_files)
        local_tar = get_compressed(
            remote_tar, fast_compression=True)  #will flush php buffer

        if had_buffer:
            php.end_buffer()

        tar.unpack_local(local_tar)

    #take the non-ascii files and upload them one after another using ftp
    if len(non_ascii_files) > 0:
        command = ''
        for f in non_ascii_files:
            command += u'get ' + ftp_path(
                f) + u' ' + engine.LOCAL_WWW_DIR + '/' + f + u'\n'
        ftp.execute(command)
def put_multiple(file_list):
    #split into ascii and non-ascii
    ascii_files, non_ascii_files = engine.split_by_encoding(file_list)

    if engine.FORCE_FTP_FILE_TRANSFER:
        ascii_files = []
        non_ascii_files = file_list

    #pack ascii files, upload compressed and unpack on server
    if len(ascii_files) > 0:
        out.log('uploading files with ascii compatible names', 'transfer')
        local_tar = tar.pack_local_list(ascii_files)
        remote_tar = put_compressed(local_tar)
        tar.unpack_remote(remote_tar)

    #take the non-ascii files and upload them one after another using ftp
    if len(non_ascii_files) > 0:
        out.log('uploading files with non-ascii filename', 'transfer')
        directories = engine.get_all_directories(non_ascii_files)
        for directory in directories:
            create_remote_directory(directory)
        command = ''
        for f in non_ascii_files:
            command += u'put ' + engine.LOCAL_WWW_DIR + '/' + f + u' ' + ftp_path(
                f) + u'\n'
        ftp.execute(command)
def create_remote_directory(directory, permissions=None):
    out.log('create remote directory: ' + directory, 'transfer',
            out.LEVEL_VERBOSE)
    command = 'mkdir ' + ftp_path(directory)
    if engine.FTP_CLIENT == 'sftp':
        command = '-' + command
    if permissions is not None:
        command += "\nchmod " + str(permissions) + " " + ftp_path(directory)
    ftp.execute(command)
def remove_remote(filename):
    out.log('remove remote file: ' + filename, 'transfer')
    if engine.FTP_CLIENT == 'ftp':
        command = 'delete ' + ftp_path(filename)
    elif engine.FTP_CLIENT == 'sftp':
        command = 'rm ' + ftp_path(filename)
    else:
        out.log('Unknown ftp client ' + engine.FTP_CLIENT, 'transfer',
                out.LEVEL_ERROR)
        engine.quit()
    ftp.execute(command)
def remote_remote_directory_contents(directory):
    out.log('remove content of remote directory: ' + dircetory, 'transfer',
            out.LEVEL_VERBOSE)
    if engine.FTP_CLIENT == 'ftp':
        command = 'delete ' + ftp_path(directory) + '/*'
    elif engine.FTP_CLIENT == 'sftp':
        command = 'rm ' + ftp_path(directory) + '/*'
    else:
        out.log('Unknown ftp client ' + engine.FTP_CLIENT, 'transfer',
                out.LEVEL_ERROR)
        engine.quit()
    ftp.execute(command)
def remove_remote_multiple(file_list):
    out.log('removing multiple remote files', 'transfer')
    command = ''
    for file in file_list:
        if engine.FTP_CLIENT == 'ftp':
            command += 'delete ' + ftp_path(file) + '\n'
        elif engine.FTP_CLIENT == 'sftp':
            command += 'rm ' + ftp_path(file) + '\n'
        else:
            out.log('Unknown ftp client ' + engine.FTP_CLIENT, 'transfer',
                    out.LEVEL_ERROR)
            engine.quit()
    ftp.execute(command)
def set_remote_mode(file, mode):
    out.log('setting mode ' + str(mode) + ' on ' + file, 'transfer',
            out.LEVEL_VERBOSE)
    command = "chmod " + str(mode) + ' ' + ftp_path(file)
    ftp.execute(command)
def remove_remote_directory(directory):
    out.log('remove remote directory: ' + directory, 'transfer',
            out.LEVEL_VERBOSE)
    command = 'rmdir ' + ftp_path(directory)
    ftp.execute(command)
def remote_move(from_file, to_file):
    out.log('move remote file: ' + from_file + ' -> ' + to_file, 'transfer',
            out.LEVEL_VERBOSE)
    command = 'rename ' + ftp_path(from_file) + ' ' + ftp_path(to_file)
    ftp.execute(command)
def test_module():
    out.log("Testing FTP connection...", 'transfer')
    ftp.execute('pwd')
    out.log("FTP connection successful", 'transfer')