-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_init.sh
More file actions
46 lines (38 loc) · 1.04 KB
/
Copy pathpy_init.sh
File metadata and controls
46 lines (38 loc) · 1.04 KB
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
# Add __init__.py to specified directory and all its sub directories lol
# Print usage/help
print_usage() {
echo "Usage: $0 <directory_path>"
echo ""
echo "Recursively adds an __init__.py file to every subdirectory of the given path."
echo "Works with relative (e.g., .) and absolute paths."
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit"
}
# Handle help option
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
print_usage
exit 0
fi
# Check if directory argument is provided
if [ -z "$1" ]; then
echo "Error: No directory path provided."
print_usage
exit 1
fi
# Resolve to absolute path
TARGET_DIR=$(realpath "$1")
# Check if the path is a valid directory
if [ ! -d "$TARGET_DIR" ]; then
echo "Error: '$TARGET_DIR' is not a valid directory."
exit 1
fi
# Traverse all subdirectories and add __init__.py
find "$TARGET_DIR" -type d | while read -r dir; do
INIT_FILE="$dir/__init__.py"
if [ ! -f "$INIT_FILE" ]; then
touch "$INIT_FILE"
echo "Created: $INIT_FILE"
fi
done
echo "All done."