|
5 | 5 |
|
6 | 6 | # ruff: noqa: INP001 |
7 | 7 | import logging |
| 8 | +import re |
8 | 9 | import shutil |
9 | 10 | import sys |
10 | 11 | from pathlib import Path |
11 | 12 |
|
12 | 13 |
|
13 | | -def copy_files(source: Path, destination: Path) -> None: |
| 14 | +def copy_files(source: Path, destination: Path, pattern: str) -> None: |
14 | 15 | if destination.exists(): |
15 | 16 | shutil.rmtree(destination) |
16 | 17 | destination.mkdir() |
17 | 18 |
|
18 | 19 | for file in source.iterdir(): |
19 | 20 | if file.is_file(): |
20 | | - shutil.copy(file, destination / file.name) |
| 21 | + if not pattern or re.match(pattern, file.name): |
| 22 | + shutil.copy(file, destination / file.name) |
21 | 23 | else: |
22 | | - copy_files(file, destination / file.name) |
| 24 | + copy_files(file, destination / file.name, pattern) |
23 | 25 |
|
24 | 26 |
|
25 | 27 | if __name__ == "__main__": |
26 | | - if len(sys.argv) != 3: # noqa |
| 28 | + if len(sys.argv) not in {3, 4}: |
27 | 29 | logging.error( |
28 | | - "Script used incorrectly!\nUsage: python copy_dir.py <source_dir> <destination>" |
| 30 | + "Script used incorrectly!\nUsage: python copy_dir.py <source_dir> <destination> <optional:match_pattern>" |
29 | 31 | ) |
30 | 32 | sys.exit(1) |
31 | 33 |
|
32 | 34 | root_dir = Path(__file__).parent.parent.parent |
33 | | - src = Path(root_dir / sys.argv[1]) |
34 | | - dest = Path(root_dir / sys.argv[2]) |
| 35 | + _source = Path(root_dir / sys.argv[1]) |
| 36 | + _destintation = Path(root_dir / sys.argv[2]) |
| 37 | + _pattern = sys.argv[3] if len(sys.argv) == 4 else "" # noqa |
35 | 38 |
|
36 | | - if not src.exists(): |
37 | | - logging.error("Source directory %s does not exist", src) |
| 39 | + if not _source.exists(): |
| 40 | + logging.error("Source directory %s does not exist", _source) |
38 | 41 | sys.exit(1) |
39 | 42 |
|
40 | | - copy_files(src, dest) |
| 43 | + copy_files(_source, _destintation, _pattern) |
0 commit comments