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 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15 | if name == '.svn': return 0 |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 16 | if name.endswith('~'): return 0 |
| 17 | if name.endswith('.BAK'): return 0 |
| 18 | if name.endswith('.pyc'): return 0 |
| 19 | if name.endswith('.pyo'): return 0 |
| 20 | if name.endswith('.orig'): return 0 |
| 21 | return 1 |
| 22 | |
Jack Jansen | a1b7758 | 2003-06-19 22:35:20 +0000 | [diff] [blame] | 23 | def copycleandir(src, dst): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 24 | for cursrc, dirs, files in os.walk(src): |
| 25 | assert cursrc.startswith(src) |
| 26 | curdst = dst + cursrc[len(src):] |
| 27 | if verbose: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 28 | print("mkdir", curdst) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 29 | if not debug: |
| 30 | if not os.path.exists(curdst): |
| 31 | os.makedirs(curdst) |
| 32 | for fn in files: |
| 33 | if isclean(fn): |
| 34 | if verbose: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 35 | print("copy", os.path.join(cursrc, fn), os.path.join(curdst, fn)) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 36 | if not debug: |
| 37 | shutil.copy2(os.path.join(cursrc, fn), os.path.join(curdst, fn)) |
| 38 | else: |
| 39 | if verbose: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 40 | print("skipfile", os.path.join(cursrc, fn)) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 41 | for i in range(len(dirs)-1, -1, -1): |
| 42 | if not isclean(dirs[i]): |
| 43 | if verbose: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 44 | print("skipdir", os.path.join(cursrc, dirs[i])) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 45 | del dirs[i] |
| 46 | |
Jack Jansen | a1b7758 | 2003-06-19 22:35:20 +0000 | [diff] [blame] | 47 | def main(): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 48 | if len(sys.argv) != 3: |
| 49 | sys.stderr.write("Usage: %s srcdir dstdir\n" % sys.argv[0]) |
| 50 | sys.exit(1) |
| 51 | copycleandir(sys.argv[1], sys.argv[2]) |
| 52 | |
Jack Jansen | a1b7758 | 2003-06-19 22:35:20 +0000 | [diff] [blame] | 53 | if __name__ == '__main__': |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 54 | main() |