File size: 3,478 Bytes
0ba52cd
 
47295d2
 
 
 
 
 
0ba52cd
 
 
 
 
7ffae14
0ba52cd
 
 
 
 
 
 
 
 
 
 
 
 
 
47295d2
0ba52cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47295d2
0ba52cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47295d2
0ba52cd
 
 
47295d2
 
 
 
 
 
0ba52cd
47295d2
 
0ba52cd
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash

# Ensure pigz is installed
if ! command -v pigz &> /dev/null; then
    echo "Error: pigz is not installed. Please install it and try again."
    exit 1
fi

# Defined the root directory of the whole dataset
ROOT_DIR="/ssd2/TzuYu/data"

# Function to display usage
usage() {
    echo "Usage: $0 [--method METHOD --config CONFIG] | [--dir-path DIR_PATH] | [--all]"
    exit 1
}
# Function to zip a single directory
zip_config() {
    local method=$1
    local config=$2
    local dir_path="$ROOT_DIR/$method/$config"
    local output_path="./data/$method/$config/$config.tar.gz"

    # Ensure the output directory exists
    mkdir -p "$(dirname "$output_path")"

    # Create the tar.gz file
    echo "Zipping $dir_path to $output_path"
    tar -cv --use-compress-program=pigz -f "$output_path" -C "$dir_path" .
    if [[ $? -ne 0 ]]; then
        echo "Error: Failed to create tar.gz for $dir_path"
        exit 1
    fi
}

# Parse arguments
while [[ "$#" -gt 0 ]]; do
    case $1 in
        --help) usage ;;
        --h) usage ;;
        --all) ALL="true" ;;
        --method) METHOD="$2"; shift ;;
        --config) CONFIG="$2"; shift ;;
        --dir-path) DIR_PATH="$2"; shift ;;
        *) echo "Unknown parameter: $1"; usage ;;
    esac
    shift
done

# zipping all folders
if [[ "$ALL" == "true" ]]; then
    # Iterate through all methods and configs
    for method_dir in "$ROOT_DIR"/*; do
        if [[ -d "$method_dir" && "$(basename "$method_dir")" != "Origin" ]]; then
            method=$(basename "$method_dir")
            for config_dir in "$method_dir"/config*; do
                if [[ -d "$config_dir" ]]; then
                    config=$(basename "$config_dir")
                    zip_config "$method" "$config"
                fi
            done
        fi
    done

    if [ ! -e "./data/Origin/Origin.tar.gz" ]; then
        # Ensure the output directory exists
        mkdir -p "./data/Origin"

        # Create the tar.gz file
        echo "Zipping $ROOT_DIR/Origin to ./data/Origin/Origin.tar.gz"
        tar -cv --use-compress-program=pigz -f "./data/Origin/Origin.tar.gz" -C "$ROOT_DIR/Origin" .
        if [[ $? -ne 0 ]]; then
            echo "Error: Failed to create tar.gz for $ROOT_DIR/Origin"
            exit 1
        fi
    fi
    echo "All configurations have been zipped successfully."
    exit 0
fi

SPECIFIED="false"

# Validate arguments
if [[ -n "$METHOD" && -n "$CONFIG" ]]; then
    DIR_PATH="$ROOT_DIR/$METHOD/config$CONFIG"
    SPECIFIED="true"
elif [[ -z "$DIR_PATH" ]]; then
    echo "Error: Either --method and --config or --dir-path must be specified."
    usage
fi

# Check if the directory exists
if [[ ! -d "$DIR_PATH" ]]; then
    echo "Error: Directory $DIR_PATH does not exist."
    exit 1
fi

# Extract method and config index from the directory path
METHOD=$(basename "$(dirname "$DIR_PATH")")
CONFIG=$(basename "$DIR_PATH")

# Define the output tar file name
# If method and config path specified, saved to the data hirarchy
# Otherwise, saved to the repo root directory
if [[ "$SPECIFIED" == "false" ]]; then
    OUTPUT_TAR="./$CONFIG.tar.gz"
    echo "Zipping $DIR_PATH to $OUTPUT_TAR"
    tar -cv --use-compress-program=pigz -f "$OUTPUT_TAR" -C "$DIR_PATH" .
    if [[ $? -ne 0 ]]; then
        echo "Error: Failed to create tar.gz for $dir_path"
        exit 1
    fi
else
    # Create the tar.gz file
    zip_config "$METHOD" config"$CONFIG"
fi

echo "Directory zipped successfully to: $OUTPUT_TAR"