blob: ab1af71dfd823ffa8ef330e97e388a5277ba0aee [file] [log] [blame]
Jack Jansena1b77582003-06-19 22:35:20 +00001"""Recursively copy a directory but skip undesired files and
2directories (CVS, backup files, pyc files, etc)"""
3
4import sys
5import os
6import shutil
7
8verbose = 1
9debug = 0
10
11def isclean(name):
Tim Peters182b5ac2004-07-18 06:16:08 +000012 if name == 'CVS': return 0
13 if name == '.cvsignore': return 0
14 if name == '.DS_store': return 0
Thomas Wouters477c8d52006-05-27 19:21:47 +000015 if name == '.svn': return 0
Tim Peters182b5ac2004-07-18 06:16:08 +000016 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 Jansena1b77582003-06-19 22:35:20 +000023def copycleandir(src, dst):
Tim Peters182b5ac2004-07-18 06:16:08 +000024 for cursrc, dirs, files in os.walk(src):
25 assert cursrc.startswith(src)
26 curdst = dst + cursrc[len(src):]
27 if verbose:
Collin Wintere7bf59f2007-08-30 18:39:28 +000028 print("mkdir", curdst)
Tim Peters182b5ac2004-07-18 06:16:08 +000029 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 Wintere7bf59f2007-08-30 18:39:28 +000035 print("copy", os.path.join(cursrc, fn), os.path.join(curdst, fn))
Tim Peters182b5ac2004-07-18 06:16:08 +000036 if not debug:
37 shutil.copy2(os.path.join(cursrc, fn), os.path.join(curdst, fn))
38 else:
39 if verbose:
Collin Wintere7bf59f2007-08-30 18:39:28 +000040 print("skipfile", os.path.join(cursrc, fn))
Tim Peters182b5ac2004-07-18 06:16:08 +000041 for i in range(len(dirs)-1, -1, -1):
42 if not isclean(dirs[i]):
43 if verbose:
Collin Wintere7bf59f2007-08-30 18:39:28 +000044 print("skipdir", os.path.join(cursrc, dirs[i]))
Tim Peters182b5ac2004-07-18 06:16:08 +000045 del dirs[i]
46
Jack Jansena1b77582003-06-19 22:35:20 +000047def main():
Tim Peters182b5ac2004-07-18 06:16:08 +000048 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 Jansena1b77582003-06-19 22:35:20 +000053if __name__ == '__main__':
Tim Peters182b5ac2004-07-18 06:16:08 +000054 main()