klenwell information services : FabricEditRemoteFile

Fabric: Edit a Remote File

return to DevFabric

Find and Replace String in Remote File

Simple example:
from fabric.api import get, put

REMOTE_DIR = "/remote/dir"
LOCAL_WD = "/tmp"

def update_remote_file(fname, subs=[]):
	"""subs is a list of tuples: (find, replace)"""
	local_path = pathjoin(LOCAL_WD, fname)
	remote_path = pathjoin(REMOTE_DIR, fname)
	get(remote_path, local_path)
	f = open(local_path, "r")
	contents = f.read()
	for find,replace in subs:	    
		contents = contents.replace(find, replace)
	f = open(local_path, "w")
	put(local_path, remote_path)