-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_files.py
93 lines (71 loc) · 3.01 KB
/
copy_files.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import shutil
import argparse
def copy_files(source_dir, dest_dir, ignore_patterns):
ignore_patterns = read_gitignore(source_dir, ignore_patterns)
files_copied = 0
# Empty the destination directory
if os.path.exists(dest_dir):
shutil.rmtree(dest_dir)
for root, dirs, files in os.walk(source_dir):
rel_dir = os.path.relpath(root, source_dir)
dest_subdir = os.path.join(dest_dir, rel_dir)
# Skip ignored directories
if any([is_ignored(dir, ignore_patterns) for dir in get_parent_directories(rel_dir)]):
continue
for file in files:
file_path = os.path.join(root, file)
if not is_ignored(file_path, ignore_patterns):
create_directory(dest_subdir)
dest_path = os.path.join(dest_subdir, file)
shutil.copy2(file_path, dest_path)
files_copied += 1
print(f"Copied {file_path} to {dest_path}")
print(f"{files_copied} files have been copied.")
def read_gitignore(source_dir, ignore_patterns):
ignore_patterns.append('.git')
gitignore_file = os.path.join(source_dir, '.gitignore')
if os.path.isfile(gitignore_file):
with open(gitignore_file, 'r') as f:
for line in f:
pattern = line.strip()
# Skip empty lines or comments in .gitignore
if pattern and not pattern.startswith('#'):
ignore_patterns.append(pattern)
return ignore_patterns
def is_ignored(path, ignore_patterns):
rel_path = os.path.relpath(path)
for pattern in ignore_patterns:
if pattern.startswith('!'):
# Negated pattern, check if the path matches
if matches_pattern(rel_path, pattern[1:]):
return False
elif matches_pattern(rel_path, pattern):
return True
return False
def matches_pattern(path, pattern):
if pattern.endswith('/'):
# Directory pattern
return path.startswith(pattern.rstrip('/')) and os.path.isdir(path)
return path == pattern
def create_directory(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def get_parent_directories(directory):
directories = []
parts = directory.split('/')
for i in range(1, len(parts) + 1):
parent_dir = '/'.join(parts[:i])
directories.append(parent_dir)
return directories
def main():
# Parse command-line arguments
parser = argparse.ArgumentParser(description='Python Directory Copy Utility with Exclusion Patterns')
parser.add_argument('source_dir', help='Path to the source directory')
parser.add_argument('dest_dir', help='Path to the destination directory')
parser.add_argument('--exclude', metavar='PATTERN', nargs='+', default=[], help='Exclude patterns')
args = parser.parse_args()
# Call the copy_files function with the provided arguments
copy_files(args.source_dir, args.dest_dir, args.exclude)
if __name__ == '__main__':
main()