#NEWLINE# def sed( filename , before , after , limit = '' , use_sudo = False , backup = '.bak' , flags = '' , shell = False ) : #NEWLINE# #INDENT# '\n    Run a search-and-replace on ``filename`` with given regex patterns.\n\n    Equivalent to ``sed -i<backup> -r -e "/<limit>/ s/<before>/<after>/<flags>g"\n    <filename>``. Setting ``backup`` to an empty string will, disable backup\n    file creation.\n\n    For convenience, ``before`` and ``after`` will automatically escape forward\n    slashes, single quotes and parentheses for you, so you don\'t need to\n    specify e.g.  ``http:\\/\\/foo\\.com``, instead just using ``http://foo\\.com``\n    is fine.\n\n    If ``use_sudo`` is True, will use `sudo` instead of `run`.\n\n    The ``shell`` argument will be eventually passed to `run`/`sudo`. It\n    defaults to False in order to avoid problems with many nested levels of\n    quotes and backslashes. However, setting it to True may help when using\n    ``~fabric.operations.cd`` to wrap explicit or implicit ``sudo`` calls.\n    (``cd`` by it\'s nature is a shell built-in, not a standalone command, so it\n    should be called within a shell.)\n\n    Other options may be specified with sed-compatible regex flags -- for\n    example, to make the search and replace case insensitive, specify\n    ``flags="i"``. The ``g`` flag is always specified regardless, so you do not\n    need to remember to include it when overriding this parameter.\n\n    .. versionadded:: 1.1\n        The ``flags`` parameter.\n    .. versionadded:: 1.6\n        Added the ``shell`` keyword argument.\n    ' #NEWLINE# func = ( ( use_sudo and sudo ) or run ) #NEWLINE# for char in "/'" : #NEWLINE# #INDENT# before = before . replace ( char , ( '\\%s' % char ) ) #NEWLINE# after = after . replace ( char , ( '\\%s' % char ) ) #NEWLINE# #UNINDENT# for char in '()' : #NEWLINE# #INDENT# after = after . replace ( char , ( '\\%s' % char ) ) #NEWLINE# #UNINDENT# if limit : #NEWLINE# #INDENT# limit = ( '/%s/ ' % limit ) #NEWLINE# #UNINDENT# context = { #NEWLINE# #INDENT# 'script' : ( "'%ss/%s/%s/%sg'" % ( limit , before , after , flags ) ) , #NEWLINE# 'filename' : _expand_path ( filename ) , #NEWLINE# 'backup' : backup , #NEWLINE# #UNINDENT# } #NEWLINE# with hide ( 'running' , 'stdout' ) : #NEWLINE# #INDENT# platform = run ( 'uname' ) #NEWLINE# #UNINDENT# if ( platform in ( 'NetBSD' , 'OpenBSD' , 'QNX' ) ) : #NEWLINE# #INDENT# hasher = hashlib . sha1 ( ) #NEWLINE# hasher . update ( env . host_string ) #NEWLINE# hasher . update ( filename ) #NEWLINE# context [ 'tmp' ] = ( '/tmp/%s' % hasher . hexdigest ( ) ) #NEWLINE# expr = 'cp -p %(filename)s %(tmp)s \\\n&& sed -r -e %(script)s %(filename)s > %(tmp)s \\\n&& cp -p %(filename)s %(filename)s%(backup)s \\\n&& mv %(tmp)s %(filename)s' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# context [ 'extended_regex' ] = ( '-E' if ( platform == 'Darwin' ) else '-r' ) #NEWLINE# expr = 'sed -i%(backup)s %(extended_regex)s -e %(script)s %(filename)s' #NEWLINE# #UNINDENT# command = ( expr % context ) #NEWLINE# return func ( command , shell = shell )