Permalink
Browse files

Refactored sed routine.

  • Loading branch information...
1 parent f955811 commit ee81f0ef1dc0637aa28a5220be5e2216312003dc @konradhalas konradhalas committed Mar 19, 2013
Showing with 9 additions and 6 deletions.
  1. +9 −6 fabric/contrib/files.py
View
@@ -168,6 +168,11 @@ def sed(filename, before, after, limit='', use_sudo=False, backup='.bak',
after = after.replace(char, r'\%s' % char)
if limit:
limit = r'/%s/ ' % limit
+ context = {
+ 'script': r"'%ss/%s/%s/%sg'" % (limit, before, after, flags),
+ 'filename': _expand_path(filename),
+ 'backup': backup
+ }
# Test the OS because of differences between sed versions
with hide('running', 'stdout'):
@@ -177,17 +182,15 @@ def sed(filename, before, after, limit='', use_sudo=False, backup='.bak',
hasher = hashlib.sha1()
hasher.update(env.host_string)
hasher.update(filename)
- tmp = "/tmp/%s" % hasher.hexdigest()
- filename = _expand_path(filename)
+ context['tmp'] = "/tmp/%s" % hasher.hexdigest()
# Use temp file to work around lack of -i
expr = r"""cp -p %(filename)s %(tmp)s \
-&& sed -r -e '%(limit)ss/%(before)s/%(after)s/%(flags)sg' %(filename)s > %(tmp)s \
+&& sed -r -e %(script)s %(filename)s > %(tmp)s \
&& cp -p %(filename)s %(filename)s%(backup)s \
&& mv %(tmp)s %(filename)s"""
- command = expr % locals()
else:
- expr = r"sed -i%s -r -e '%ss/%s/%s/%sg' %s"
- command = expr % (backup, limit, before, after, flags, _expand_path(filename))
+ expr = r"sed -i%(backup)s -r -e %(script)s %(filename)s"
+ command = expr % context
return func(command, shell=shell)

0 comments on commit ee81f0e

Please sign in to comment.