tools/files.py (29 lines of code) (raw):

import fileinput import os import re import sys import tempfile import logging import shutil logger = logging.getLogger(__name__) def replace_in_file(filepath, search_replacements): """ In-place file search and replace. filepath - The path of the file to edit search_replacements - a list of tuples (regex, replacement) that represent however many search and replace operations you wish to perform. Note: This does not work with multi-line regexes. """ for line in fileinput.input(filepath, inplace=True): for regex, replacement in search_replacements: line = re.sub(regex, replacement, line) sys.stdout.write(line) def safe_mkdtemp(): tmpdir = tempfile.mkdtemp() # \ on Windows is interpreted as an escape character and doesn't do anyone any favors return tmpdir.replace('\\', '/') def size_of_files_in_dir(dir_name, verbose=True): """ Return the size of all files found in a non-recursive ls of the argument. Based on http://stackoverflow.com/a/1392549 """ files = [os.path.join(dir_name, f) for f in os.listdir(dir_name)] if verbose: logger.debug('getting sizes of these files: {}'.format(files)) return sum(os.path.getsize(f) for f in files) def copytree(src, dst, symlinks=False, ignore=None): for item in os.listdir(src): s = os.path.join(src, item) d = os.path.join(dst, item) if os.path.isdir(s): shutil.copytree(s, d, symlinks, ignore) else: shutil.copy2(s, d)