Skip to content

Commit

Permalink
Merge remote-tracking branch 'ploxiln/contrib_escape_egrep' into 1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
bitprophet committed Apr 24, 2017
2 parents f6d1898 + d0caedf commit 7dd8c0c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
27 changes: 17 additions & 10 deletions fabric/contrib/files.py
Expand Up @@ -5,7 +5,6 @@
from __future__ import with_statement

import hashlib
import re
import os
from StringIO import StringIO
from functools import partial
Expand Down Expand Up @@ -347,7 +346,7 @@ def contains(filename, text, exact=False, use_sudo=False, escape=True,
added.)
The ``shell`` argument will be eventually passed to ``run/sudo``. See
description of the same argumnet in ``~fabric.contrib.sed`` for details.
description of the same argument in ``~fabric.contrib.sed`` for details.
If ``case_sensitive`` is False, the `-i` flag will be passed to ``egrep``.
Expand Down Expand Up @@ -429,14 +428,22 @@ def append(filename, text, use_sudo=False, partial=False, escape=True,

def _escape_for_regex(text):
"""Escape ``text`` to allow literal matching using egrep"""
regex = re.escape(text)
# Seems like double escaping is needed for \
regex = regex.replace('\\\\', '\\\\\\')
# Triple-escaping seems to be required for $ signs
regex = regex.replace(r'\$', r'\\\$')
# Whereas single quotes should not be escaped
regex = regex.replace(r"\'", "'")
return regex
re_specials = '\\^$|(){}[]*+?.'
sh_specials = '\\$`"'
re_chars = []
sh_chars = []

for c in text:
if c in re_specials:
re_chars.append('\\')
re_chars.append(c)

for c in re_chars:
if c in sh_specials:
sh_chars.append('\\')
sh_chars.append(c)

return ''.join(sh_chars)

def _expand_path(path):
return '"$(echo %s)"' % path
3 changes: 3 additions & 0 deletions sites/www/changelog.rst
Expand Up @@ -2,6 +2,9 @@
Changelog
=========

* :bug:`1294` fix text escaping for `~fabric.contrib.files.contains` and
`~fabric.contrib.files.append` which would fail if the text contained e.g.
``>``. Thanks to ``@ecksun`` for report & Pierce Lopez for the patch.
* :support:`1065 backported` Fix incorrect SSH config reference in the docs for
``env.keepalive``; it corresponds to ``ClientAliveInterval``, not
``ServerAliveInterval``. Credit: Harry Percival.
Expand Down

0 comments on commit 7dd8c0c

Please sign in to comment.