[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Fab-user] proposed patch for files.contains
From: |
Thibault Ketterer |
Subject: |
[Fab-user] proposed patch for files.contains |
Date: |
Fri, 19 Aug 2011 12:29:39 +0200 |
Hi
I found astonishing that the contrib.files.contains was not able to
match a plain text string
It is somehow related to this http://code.fabfile.org/issues/show/188
Here is my proposition, a new option in contains to escape or not the
text string :
diff --git a/fabric/contrib/files.py b/fabric/contrib/files.py
index 7d9ae77..37df85a 100644
--- a/fabric/contrib/files.py
+++ b/fabric/contrib/files.py
@@ -256,7 +256,7 @@ def comment(filename, regex, use_sudo=False,
char='#', backup='.
)
-def contains(filename, text, exact=False, use_sudo=False):
+def contains(filename, text, exact=False, use_sudo=False, escape=False):
"""
Return True if ``filename`` contains ``text``.
@@ -271,11 +271,18 @@ def contains(filename, text, exact=False, use_sudo=False):
If ``use_sudo`` is True, will use `sudo` instead of `run`.
+ If ``escape`` is True, text will be regex-escaped, it is not the default
+ to preserve old behavior
+
.. versionchanged:: 1.0
Swapped the order of the ``filename`` and ``text`` arguments to be
consistent with other functions in this module.
"""
func = use_sudo and sudo or run
+ if escape:
+ text = re.escape(text).replace('"', r'\"')
+ else:
+ text = text.replace('"', r'\"')
if exact:
text = "^%s$" % text
with settings(hide('everything'), warn_only=True):
--
Thibault ketterer
- [Fab-user] proposed patch for files.contains,
Thibault Ketterer <=