| import fileinput |
| import os |
| import re |
| import sys |
| import tempfile |
| import logging |
| |
| 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) |