|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2006 Free Software Foundation, Inc. |
| 4 | +# |
| 5 | +# This file is part of GNU Radio |
| 6 | +# |
| 7 | +# GNU Radio is free software; you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation; either version 2, or (at your option) |
| 10 | +# any later version. |
| 11 | +# |
| 12 | +# GNU Radio is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License along |
| 18 | +# with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | +# |
| 21 | + |
| 22 | +import re |
| 23 | +import os |
| 24 | +import os.path |
| 25 | +import sys |
| 26 | +from optparse import OptionParser |
| 27 | + |
| 28 | +dry_run = False |
| 29 | +modified_files = [] |
| 30 | + |
| 31 | +dirs_to_ignore = ('CVS', '.svn', '.deps', '.libs') |
| 32 | +extensions_to_ignore = ('.o', '.lo', |
| 33 | + '.a', '.la', '.lai', |
| 34 | + '.so', '.soT', |
| 35 | + '.pyc', '.pyo', |
| 36 | + '.ko', '.fasl', '.rwbak', |
| 37 | + '.tar', '.gz', '.log', |
| 38 | + '.eps', '.ps', '.pdf', |
| 39 | + '.png', '.jpg', '.jpeg', '.bmp', |
| 40 | + '.dat', '.ihx', |
| 41 | + '.diff', |
| 42 | + '.lib', '.lst', '.rel', '.sym', '.asm', '.rst', 'mem', '.map', # sdcc output |
| 43 | + '.rbf', '.esf', '.qpf', '.psf', '.quartus', '.bsf', '.cmp' # quartus |
| 44 | + ) |
| 45 | + |
| 46 | +def destructively_remove(list, predicate): |
| 47 | + """ |
| 48 | + Destructively remove elements from LIST for which PREDICATE is true |
| 49 | + """ |
| 50 | + for i in range(len(list)): |
| 51 | + if predicate(list[i]): |
| 52 | + del list[i] |
| 53 | + destructively_remove(list, predicate) |
| 54 | + return |
| 55 | + |
| 56 | + |
| 57 | +def expunge_unwanted_dirnames(dirnames): |
| 58 | + """ |
| 59 | + Destructively remove directory names that we don't want to visit. |
| 60 | + This is a _very_ non-functional approach to programming... |
| 61 | + """ |
| 62 | + destructively_remove(dirnames, lambda d: d in dirs_to_ignore) |
| 63 | + |
| 64 | +def expunge_unwanted_filenames(filenames): |
| 65 | + """ |
| 66 | + Destructively remove filenames that we don't want to visit. |
| 67 | + This is a _very_ non-functional approach to programming... |
| 68 | + """ |
| 69 | + destructively_remove(filenames, |
| 70 | + lambda f: f.endswith('~') or os.path.splitext(f)[1] in extensions_to_ignore) |
| 71 | + |
| 72 | + |
| 73 | +def walk_directory(dirname): |
| 74 | + for dirpath, dirnames, filenames in os.walk(dirname): |
| 75 | + expunge_unwanted_dirnames(dirnames) |
| 76 | + expunge_unwanted_filenames(filenames) |
| 77 | + for f in filenames: |
| 78 | + update_one(os.path.join(dirpath, f)) |
| 79 | + |
| 80 | + |
| 81 | +addr_pattern = re.compile(r'\b59 Temple Place(,| *-) *Suite 330\b', re.IGNORECASE) |
| 82 | +addr_replacement = '51 Franklin Street' |
| 83 | +zip_pattern = re.compile(r'\b02111-1307\b') |
| 84 | +zip_replacement = '02110-1301' |
| 85 | + |
| 86 | +def update_one(filename): |
| 87 | + f = open(filename, 'r') |
| 88 | + s = f.read() |
| 89 | + f.close() |
| 90 | + t = s |
| 91 | + t = addr_pattern.sub(addr_replacement, t) |
| 92 | + t = zip_pattern.sub(zip_replacement, t) |
| 93 | + if s != t: |
| 94 | + modified_files.append(filename) |
| 95 | + if not dry_run: |
| 96 | + f = open(filename, 'w') |
| 97 | + f.write(t) |
| 98 | + |
| 99 | + |
| 100 | +def handle_file_or_dir(file_or_dir): |
| 101 | + if os.path.isfile(file_or_dir): |
| 102 | + update_one(file_or_dir) |
| 103 | + elif os.path.isdir(file_or_dir): |
| 104 | + walk_directory(file_or_dir) |
| 105 | + else: |
| 106 | + pass # ignore the other cases |
| 107 | + |
| 108 | + |
| 109 | +def main(): |
| 110 | + global dry_run |
| 111 | + |
| 112 | + usage = '%prog: [options] [file_or_dir...]' |
| 113 | + parser = OptionParser (usage=usage) |
| 114 | + parser.add_option('-l', '--list-modified-files', action='store_true', default=False, |
| 115 | + help='List modified files to stdout [default=%default]') |
| 116 | + parser.add_option('', '--dry-run', action='store_true', default=False, |
| 117 | + help="Don't modify any files, just report what would be modified [default=%default]") |
| 118 | + (options, args) = parser.parse_args() |
| 119 | + |
| 120 | + dry_run = options.dry_run |
| 121 | + if options.dry_run: |
| 122 | + options.list_modified_files = True |
| 123 | + |
| 124 | + for file_or_dir in args: |
| 125 | + handle_file_or_dir(file_or_dir) |
| 126 | + |
| 127 | + if options.list_modified_files: |
| 128 | + for f in modified_files: |
| 129 | + sys.stdout.write(f + '\n') |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + main() |
| 134 | + |
0 commit comments