#!/usr/bin/env python3
"""
This script creates a matching directory structure in a target directory based on a source directory.
It's particularly useful for creating test directories in Maven projects that mirror the src directory structure.
Usage:
python mirror_directories.py
Example:
python mirror_directories.py /path/to/maven/project/src /path/to/maven/project/test
The script will create directories in the target directory that match the structure of the source directory,
without deleting any existing content in the target directory.
"""
import argparse
from pathlib import Path
def create_matching_directories(source_dir: str, target_dir: str) -> None:
"""
Create a matching directory structure in the target directory based on the source directory.
This function walks through the source directory and creates corresponding directories
in the target directory. It does not delete any existing directories or files.
Args:
source_dir (str): Path to the source directory (e.g., Maven project's src directory)
target_dir (str): Path to the target directory (e.g., Maven project's test directory)
Returns:
None
"""
# Convert string paths to Path objects for easier manipulation
source_path = Path(source_dir)
target_path = Path(target_dir)
# Check if the source directory exists
if not source_path.exists() or not source_path.is_dir():
print(f"Error: Source directory '{source_dir}' does not exist.")
return
# Check if the target directory exists
if not target_path.exists() or not target_path.is_dir():
print(f"Error: Target directory '{target_dir}' does not exist.")
return
# Walk through all subdirectories in the source directory
for dir_path in source_path.rglob('*'):
if dir_path.is_dir():
# Calculate the relative path from the source to the current subdirectory
relative_path = dir_path.relative_to(source_path)
# Construct the new directory path in the target directory
new_dir = target_path / relative_path
# Create the new directory if it doesn't exist
if not new_dir.exists():
new_dir.mkdir(parents=True, exist_ok=True)
print(f"Created directory: {new_dir}")
def main():
"""
Main function to parse command-line arguments and call the directory creation function.
"""
# Set up command-line argument parsing
parser = argparse.ArgumentParser(
description="Create a matching directory structure for Maven project test directories.",
epilog="This script helps in setting up test directories that mirror the src directory structure in Maven projects."
)
parser.add_argument("source", help="Source directory path (e.g., Maven project's src directory)")
parser.add_argument("target", help="Target directory path (e.g., Maven project's test directory)")
# Parse the command-line arguments
args = parser.parse_args()
# Call the function to create matching directories
create_matching_directories(args.source, args.target)
if __name__ == "__main__":
main()