File size: 3,729 Bytes
58e6885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
# Link this workspace's output/ directory to the current user's shared output.
#
# Usage:
#   bash scripts/use_shared_output.sh
#   CHARTPIPE_SHARED_OUTPUT=/path/to/shared_output bash scripts/use_shared_output.sh
#   CHARTPIPE_SHARED_USER=alice bash scripts/use_shared_output.sh
#   bash scripts/use_shared_output.sh /path/to/shared_output

set -euo pipefail
umask 0002

DEFAULT_SHARED_ROOT="/data/liduan/ChartPipeline/ChartPipeline/shared_output"
SHARED_ROOT="${1:-${CHARTPIPE_SHARED_OUTPUT:-$DEFAULT_SHARED_ROOT}}"
SHARED_USER="${CHARTPIPE_SHARED_USER:-${USER:-$(id -un)}}"
USER_OUTPUT_ROOT="$SHARED_ROOT/personal/$SHARED_USER"

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
OUTPUT_PATH="$PROJECT_ROOT/output"

canonical_path() {
    local target="$1"
    if command -v readlink >/dev/null 2>&1; then
        readlink -f "$target" 2>/dev/null || printf '%s\n' "$target"
    else
        printf '%s\n' "$target"
    fi
}

is_empty_dir() {
    local dir="$1"
    [ -d "$dir" ] && [ -z "$(ls -A "$dir")" ]
}

ensure_shared_dirs() {
    mkdir -p "$SHARED_ROOT"/{archive,beautify,manual,personal}
    mkdir -p "$USER_OUTPUT_ROOT"/{quality_check,chart_template_samples,d3_regression,regression,manual}

    chmod g+rwX \
        "$SHARED_ROOT" \
        "$SHARED_ROOT"/archive \
        "$SHARED_ROOT"/beautify \
        "$SHARED_ROOT"/manual \
        "$SHARED_ROOT"/personal \
        "$USER_OUTPUT_ROOT" \
        "$USER_OUTPUT_ROOT"/quality_check \
        "$USER_OUTPUT_ROOT"/chart_template_samples \
        "$USER_OUTPUT_ROOT"/d3_regression \
        "$USER_OUTPUT_ROOT"/regression \
        "$USER_OUTPUT_ROOT"/manual 2>/dev/null || {
        echo "[shared-output] WARN: could not chmod group-write on shared output directories" >&2
    }

    local probe="$USER_OUTPUT_ROOT/.write_test.$$"
    if ! : > "$probe" 2>/dev/null; then
        echo "[shared-output] ERROR: user output root is not writable: $USER_OUTPUT_ROOT" >&2
        exit 1
    fi
    rm -f "$probe"
}

link_output() {
    ln -s "$USER_OUTPUT_ROOT" "$OUTPUT_PATH"
    echo "[shared-output] linked: $OUTPUT_PATH -> $USER_OUTPUT_ROOT"
}

echo "[shared-output] project root: $PROJECT_ROOT"
echo "[shared-output] shared root : $SHARED_ROOT"
echo "[shared-output] shared user : $SHARED_USER"
echo "[shared-output] user output : $USER_OUTPUT_ROOT"

ensure_shared_dirs

if [ -L "$OUTPUT_PATH" ]; then
    current_target="$(readlink "$OUTPUT_PATH")"
    current_real="$(canonical_path "$OUTPUT_PATH")"
    user_output_real="$(canonical_path "$USER_OUTPUT_ROOT")"
    if [ "$current_real" = "$user_output_real" ]; then
        echo "[shared-output] output already points to user output: $current_target"
        exit 0
    fi

    echo "[shared-output] ERROR: output is already a symlink to a different target:" >&2
    echo "  $OUTPUT_PATH -> $current_target" >&2
    echo "  expected -> $USER_OUTPUT_ROOT" >&2
    echo "Move or remove the existing symlink manually, then rerun this script." >&2
    exit 2
fi

if [ -e "$OUTPUT_PATH" ]; then
    if is_empty_dir "$OUTPUT_PATH"; then
        rmdir "$OUTPUT_PATH"
        link_output
        exit 0
    fi

    echo "[shared-output] ERROR: output exists and is not an empty directory:" >&2
    echo "  $OUTPUT_PATH" >&2
    echo "This script will not overwrite or migrate existing results automatically." >&2
    echo "Recommended migration:" >&2
    echo "  1. Create/choose a versioned folder under $SHARED_ROOT/archive/" >&2
    echo "  2. Move or copy current output contents there." >&2
    echo "  3. Remove the local output directory." >&2
    echo "  4. Rerun: bash scripts/use_shared_output.sh" >&2
    exit 2
fi

link_output