-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy.py
57 lines (43 loc) · 1.7 KB
/
copy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import argparse
import os
import shutil
parser = argparse.ArgumentParser(description='Optional app description')
# Required positional argument
parser.add_argument('source', type=str,
help='Source folder')
# Optional positional argument
parser.add_argument('dest', type=str,
help='Destination folder')
# Optional argument
parser.add_argument('--dry-run', action='store_true')
parser.add_argument('-v','--verbose', action='store_true')
parser.add_argument('-l','--list', action='store_true',
help='Just print a list of matching files')
parser.add_argument('-x','--extensions', action='append', help='List of extensions to match')
args = parser.parse_args()
if not os.path.isdir(args.source):
print('source is not a valid directory')
exit(-1)
if not os.path.isdir(args.dest):
print('dest is not a valid directory')
exit(-1)
def file_match(name):
return (not name.startswith('._')) and name.lower().endswith(tuple(args.extensions))
copied_count = 0
skipped_count = 0
for root, dirs, files in os.walk(args.source):
for name in files:
if file_match(name):
full_path = os.path.join(root, name)
rel_path = os.path.relpath(full_path,args.source)
if args.list:
print(rel_path)
continue
dest_path = os.path.join(args.dest,rel_path)
if args.verbose: print(rel_path + ' -> ' + dest_path)
if not args.dry_run:
shutil.copy2(args.source,dest_path)
copied_count += 1
else: skipped_count += 1
print(str(copied_count) + (' not' if args.dry_run else '') + ' copied. ')
print(str(skipped_count) + ' skipped. ')