#NEWLINE# def append( filename , text , use_sudo = False , partial = False , escape = True , shell = False ) : #NEWLINE# #INDENT# '\n    Append string (or list of strings) ``text`` to ``filename``.\n\n    When a list is given, each string inside is handled independently (but in\n    the order given.)\n\n    If ``text`` is already found in ``filename``, the append is not run, and\n    None is returned immediately. Otherwise, the given text is appended to the\n    end of the given ``filename`` via e.g. ``echo \'$text\' >> $filename``.\n\n    The test for whether ``text`` already exists defaults to a full line match,\n    e.g. ``^<text>$``, as this seems to be the most sensible approach for the\n    "append lines to a file" use case. You may override this and force partial\n    searching (e.g. ``^<text>``) by specifying ``partial=True``.\n\n    Because ``text`` is single-quoted, single quotes will be transparently\n    backslash-escaped. This can be disabled with ``escape=False``.\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``. See\n    description of the same argumnet in ``~fabric.contrib.sed`` for details.\n\n    .. versionchanged:: 0.9.1\n        Added the ``partial`` keyword argument.\n    .. versionchanged:: 1.0\n        Swapped the order of the ``filename`` and ``text`` arguments to be\n        consistent with other functions in this module.\n    .. versionchanged:: 1.0\n        Changed default value of ``partial`` kwarg to be ``False``.\n    .. versionchanged:: 1.4\n        Updated the regular expression related escaping to try and solve\n        various corner cases.\n    .. versionadded:: 1.6\n        Added the ``shell`` keyword argument.\n    ' #NEWLINE# func = ( ( use_sudo and sudo ) or run ) #NEWLINE# if isinstance ( text , basestring ) : #NEWLINE# #INDENT# text = [ text ] #NEWLINE# #UNINDENT# for line in text : #NEWLINE# #INDENT# regex = ( ( '^' + _escape_for_regex ( line ) ) + ( '' if partial else '$' ) ) #NEWLINE# if ( exists ( filename , use_sudo = use_sudo ) and line and contains ( filename , regex , use_sudo = use_sudo , escape = False , shell = shell ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# line = ( line . replace ( "'" , "'\\\\''" ) if escape else line ) #NEWLINE# func ( ( "echo '%s' >> %s" % ( line , _expand_path ( filename ) ) ) )