def upload_template(filename,  destination,  context={},  use_sudo=False, backup=True, modified_only=False):
    """
    Render and upload a template text file to a remote host using the Django
    template api. 

    ``filename`` should be the Django template name.
    
    ``context`` is the Django template dictionary context to use.

    The resulting rendered file will be uploaded to the remote file path
    ``destination`` (which should include the desired remote filename.) If the
    destination file already exists, it will be renamed with a ``.bak``
    extension.

    By default, the file will be copied to ``destination`` as the logged-in
    user; specify ``use_sudo=True`` to use `sudo` instead.
    """
    #Replaces the default fabric.contrib.files.upload_template
    basename = os.path.basename(filename)
    text = render_to_string(filename,context)

    func = use_sudo and sudo or run
    
    #check hashed template on server first
    if modified_only:
        hashfile_dir, hashfile = os.path.split(destination)
        hashfile_dir = ''.join(['/var/local/woven-backup',hashfile_dir])
        hashfile = '%s.hashfile'% hashfile
        hashfile_path = os.path.join(hashfile_dir, hashfile)
        hashed = sha1(text).hexdigest()
        if hashfile:
            if not exists(hashfile_dir): sudo('mkdir -p %s'% hashfile_dir)
            sudo('touch %s'% hashfile_path) #store the hash near the template
            previous_hashed = sudo('cat %s'% hashfile_path).strip()
            if previous_hashed == hashed:
                return False
            else: sudo('echo %s > %s'% (hashed, hashfile_path))

    temp_destination = '/tmp/' + basename

    # This temporary file should not be automatically deleted on close, as we
    # need it there to upload it (Windows locks the file for reading while open).
    tempfile_fd, tempfile_name = tempfile.mkstemp()
    output = open(tempfile_name, "w+b")
    
    output.write(text)
    output.close()
        
    # Upload the file.
    put(tempfile_name, temp_destination)
    os.close(tempfile_fd)
    os.remove(tempfile_name)

    
    # Back up any original file (need to do figure out ultimate destination)
    if backup:
        to_backup = destination
        with settings(hide('everything'), warn_only=True):
            # Is destination a directory?
            if func('test -f %s' % to_backup).failed:
                # If so, tack on the filename to get "real" destination
                to_backup = destination + '/' + basename
        if exists(to_backup):
            _backup_file(to_backup)
    # Actually move uploaded template to destination
    func("mv %s %s" % (temp_destination, destination))
    return True
dataset/ETHPy150Open bretth/woven/woven/deployment.py/upload_template/original