Fix backup logic in filter_file

This commit is contained in:
Adam J. Stewart 2016-06-23 16:44:41 -05:00
parent 65e3ac4132
commit 71a297f121

View file

@ -48,7 +48,7 @@
def filter_file(regex, repl, *filenames, **kwargs): def filter_file(regex, repl, *filenames, **kwargs):
"""Like sed, but uses python regular expressions. """Like sed, but uses python regular expressions.
Filters every line of file through regex and replaces the file Filters every line of each file through regex and replaces the file
with a filtered version. Preserves mode of filtered files. with a filtered version. Preserves mode of filtered files.
As with re.sub, ``repl`` can be either a string or a callable. As with re.sub, ``repl`` can be either a string or a callable.
@ -59,7 +59,7 @@ def filter_file(regex, repl, *filenames, **kwargs):
Keyword Options: Keyword Options:
string[=False] If True, treat regex as a plain string. string[=False] If True, treat regex as a plain string.
backup[=True] Make a backup files suffixed with ~ backup[=True] Make backup file(s) suffixed with ~
ignore_absent[=False] Ignore any files that don't exist. ignore_absent[=False] Ignore any files that don't exist.
""" """
string = kwargs.get('string', False) string = kwargs.get('string', False)
@ -80,26 +80,26 @@ def groupid_to_group(x):
regex = re.escape(regex) regex = re.escape(regex)
for filename in filenames: for filename in filenames:
backup = filename + "~" backup_filename = filename + "~"
if ignore_absent and not os.path.exists(filename): if ignore_absent and not os.path.exists(filename):
continue continue
shutil.copy(filename, backup) shutil.copy(filename, backup_filename)
try: try:
with closing(open(backup)) as infile: with closing(open(backup_filename)) as infile:
with closing(open(filename, 'w')) as outfile: with closing(open(filename, 'w')) as outfile:
for line in infile: for line in infile:
foo = re.sub(regex, repl, line) foo = re.sub(regex, repl, line)
outfile.write(foo) outfile.write(foo)
except: except:
# clean up the original file on failure. # clean up the original file on failure.
shutil.move(backup, filename) shutil.move(backup_filename, filename)
raise raise
finally: finally:
if not backup: if not backup:
shutil.rmtree(backup, ignore_errors=True) os.remove(backup_filename)
class FileFilter(object): class FileFilter(object):
@ -114,7 +114,7 @@ def filter(self, regex, repl, **kwargs):
def change_sed_delimiter(old_delim, new_delim, *filenames): def change_sed_delimiter(old_delim, new_delim, *filenames):
"""Find all sed search/replace commands and change the delimiter. """Find all sed search/replace commands and change the delimiter.
e.g., if the file contains seds that look like 's///', you can e.g., if the file contains seds that look like 's///', you can
call change_sed_delimeter('/', '@', file) to change the call change_sed_delimiter('/', '@', file) to change the
delimiter to '@'. delimiter to '@'.
NOTE that this routine will fail if the delimiter is ' or ". NOTE that this routine will fail if the delimiter is ' or ".
@ -179,7 +179,7 @@ def install(src, dest):
"""Manually install a file to a particular location.""" """Manually install a file to a particular location."""
tty.debug("Installing %s to %s" % (src, dest)) tty.debug("Installing %s to %s" % (src, dest))
# Expand dsst to its eventual full path if it is a directory. # Expand dest to its eventual full path if it is a directory.
if os.path.isdir(dest): if os.path.isdir(dest):
dest = join_path(dest, os.path.basename(src)) dest = join_path(dest, os.path.basename(src))
@ -219,7 +219,7 @@ def mkdirp(*paths):
if not os.path.exists(path): if not os.path.exists(path):
os.makedirs(path) os.makedirs(path)
elif not os.path.isdir(path): elif not os.path.isdir(path):
raise OSError(errno.EEXIST, "File alredy exists", path) raise OSError(errno.EEXIST, "File already exists", path)
def force_remove(*paths): def force_remove(*paths):
@ -309,7 +309,7 @@ def traverse_tree(source_root, dest_root, rel_path='', **kwargs):
Optional args: Optional args:
order=[pre|post] -- Whether to do pre- or post-order traveral. order=[pre|post] -- Whether to do pre- or post-order traversal.
ignore=<predicate> -- Predicate indicating which files to ignore. ignore=<predicate> -- Predicate indicating which files to ignore.
@ -414,7 +414,7 @@ def fix_darwin_install_name(path):
currently won't follow subfolders. currently won't follow subfolders.
Args: Args:
path: directory in which .dylib files are alocated path: directory in which .dylib files are located
""" """
libs = glob.glob(join_path(path, "*.dylib")) libs = glob.glob(join_path(path, "*.dylib"))
@ -438,7 +438,7 @@ def to_link_flags(library):
A string of linking flags. A string of linking flags.
""" """
dir = os.path.dirname(library) dir = os.path.dirname(library)
# Asume libXYZ.suffix # Assume libXYZ.suffix
name = os.path.basename(library)[3:].split(".")[0] name = os.path.basename(library)[3:].split(".")[0]
res = '-L%s -l%s' % (dir, name) res = '-L%s -l%s' % (dir, name)
return res return res