Skip to content

Commit d1fe8c7

Browse files
committed
allow extension matching in copy dir
1 parent 732780e commit d1fe8c7

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/build_scripts/copy_dir.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,39 @@
55

66
# ruff: noqa: INP001
77
import logging
8+
import re
89
import shutil
910
import sys
1011
from pathlib import Path
1112

1213

13-
def copy_files(source: Path, destination: Path) -> None:
14+
def copy_files(source: Path, destination: Path, pattern: str) -> None:
1415
if destination.exists():
1516
shutil.rmtree(destination)
1617
destination.mkdir()
1718

1819
for file in source.iterdir():
1920
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)
2123
else:
22-
copy_files(file, destination / file.name)
24+
copy_files(file, destination / file.name, pattern)
2325

2426

2527
if __name__ == "__main__":
26-
if len(sys.argv) != 3: # noqa
28+
if len(sys.argv) not in {3, 4}:
2729
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>"
2931
)
3032
sys.exit(1)
3133

3234
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
3538

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)
3841
sys.exit(1)
3942

40-
copy_files(src, dest)
43+
copy_files(_source, _destintation, _pattern)

0 commit comments

Comments
 (0)