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