Jack Jansen | a1b7758 | 2003-06-19 22:35:20 +0000 | [diff] [blame] | 1 | """Recursively copy a directory but skip undesired files and |
| 2 | directories (CVS, backup files, pyc files, etc)""" |
| 3 | |
| 4 | import sys |
| 5 | import os |
| 6 | import shutil |
| 7 | |
| 8 | verbose = 1 |
| 9 | debug = 0 |
| 10 | |
| 11 | def isclean(name): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 12 | if name == 'CVS': return 0 |
| 13 | if name == '.cvsignore': return 0 |
| 14 | if name == '.DS_store': return 0 |
| 15 | if name.endswith('~'): return 0 |
| 16 | if name.endswith('.BAK'): return 0 |
| 17 | if name.endswith('.pyc'): return 0 |
| 18 | if name.endswith('.pyo'): return 0 |
| 19 | if name.endswith('.orig'): return 0 |
| 20 | return 1 |
| 21 | |
Jack Jansen | a1b7758 | 2003-06-19 22:35:20 +0000 | [diff] [blame] | 22 | def copycleandir(src, dst): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 23 | for cursrc, dirs, files in os.walk(src): |
| 24 | assert cursrc.startswith(src) |
| 25 | curdst = dst + cursrc[len(src):] |
| 26 | if verbose: |
| 27 | print "mkdir", curdst |
| 28 | if not debug: |
| 29 | if not os.path.exists(curdst): |
| 30 | os.makedirs(curdst) |
| 31 | for fn in files: |
| 32 | if isclean(fn): |
| 33 | if verbose: |
| 34 | print "copy", os.path.join(cursrc, fn), os.path.join(curdst, fn) |
| 35 | if not debug: |
| 36 | shutil.copy2(os.path.join(cursrc, fn), os.path.join(curdst, fn)) |
| 37 | else: |
| 38 | if verbose: |
| 39 | print "skipfile", os.path.join(cursrc, fn) |
| 40 | for i in range(len(dirs)-1, -1, -1): |
| 41 | if not isclean(dirs[i]): |
| 42 | if verbose: |
| 43 | print "skipdir", os.path.join(cursrc, dirs[i]) |
| 44 | del dirs[i] |
| 45 | |
Jack Jansen | a1b7758 | 2003-06-19 22:35:20 +0000 | [diff] [blame] | 46 | def main(): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 47 | if len(sys.argv) != 3: |
| 48 | sys.stderr.write("Usage: %s srcdir dstdir\n" % sys.argv[0]) |
| 49 | sys.exit(1) |
| 50 | copycleandir(sys.argv[1], sys.argv[2]) |
| 51 | |
Jack Jansen | a1b7758 | 2003-06-19 22:35:20 +0000 | [diff] [blame] | 52 | if __name__ == '__main__': |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 53 | main() |