-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdelete.sh
More file actions
executable file
·381 lines (339 loc) · 13 KB
/
delete.sh
File metadata and controls
executable file
·381 lines (339 loc) · 13 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env bash
# delete_ltp_cmlreader.sh
# Usage:
# delete_ltp_cmlreader.sh <experiment> <subject> [session] [--dry-run|-n] [--log FILE] [--yes|-y]
#
# JSON schema:
# protocols -> ltp -> subjects -> <SUBJECT> -> experiments -> <EXPERIMENT> -> sessions -> <SESSION>
#
# Behavior:
# - If session is given: remove that session (JSON + folder), then prune empty ancestors.
# - If session is omitted: confirm; remove ALL sessions for that experiment; then prune.
# - Console output is minimal: only whether we found & deleted JSON/paths.
# - All details (plans, JSON content, paths) are written to the log file.
# - After a successful write, runs cp→rm→mv as RAM_maint so owner is RAM_maint, then chmod g+rw.
#
# Requirements: python3, sudo (for the owner-fix step)
set -euo pipefail
JSON="/protocols/ltp.json"
LTP_FOLDER="/protocols/ltp"
DRY_RUN=0
ASSUME_YES=0
LOG_FILE=""
usage() {
cat <<'EOF'
Usage:
delete_ltp_cmlreader.sh <experiment> <subject> [session] [--dry-run|-n] [--log FILE] [--yes|-y]
Options:
-n, --dry-run Show planned changes; do not modify JSON or delete files
--log FILE Append logs to FILE (default tries /var/log/... else ~/...)
-y, --yes Skip confirmation prompt when deleting ALL sessions
-h, --help Show this help
EOF
exit "${1:-0}"
}
# --- Parse positional / help ---
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then usage 0; fi
EXPERIMENT="${1:-}"; shift || true
SUBJECT="${1:-}"; shift || true
SESSION=""
if [[ "${1:-}" != "" && "${1:0:1}" != "-" ]]; then SESSION="$1"; shift || true; fi
# --- Prompt for any missing required args (interactive only) ---
if [[ -z "$EXPERIMENT" || -z "$SUBJECT" ]]; then
if [[ -t 0 ]]; then
echo "Some required arguments are missing."
# Ask only for what's missing; keep anything already provided
while [[ -z "$EXPERIMENT" ]]; do
read -r -p "Enter experiment name: " EXPERIMENT
[[ -z "$EXPERIMENT" ]] && echo "Experiment cannot be empty."
done
while [[ -z "$SUBJECT" ]]; do
read -r -p "Enter subject (e.g., LTP001): " SUBJECT
[[ -z "$SUBJECT" ]] && echo "Subject cannot be empty."
done
else
echo "Error: experiment and subject required (stdin not interactive)."
usage 1
fi
fi
# --- Parse flags ---
while (( "$#" )); do
case "$1" in
-n|--dry-run) DRY_RUN=1; shift ;;
-y|--yes) ASSUME_YES=1; shift ;;
--log) LOG_FILE="${2:-}"; shift 2 ;;
-h|--help) usage 0 ;;
*) echo "Unknown option: $1"; usage 1 ;;
esac
done
SESSIONS_DIR="$LTP_FOLDER/subjects/$SUBJECT/experiments/$EXPERIMENT/sessions"
# --- Logging helpers ---
pick_log_file() {
if [[ -n "$LOG_FILE" ]]; then echo "$LOG_FILE"; return; fi
local sys="/var/log/delete_ltp_cmlreader.log"
if { : >>"$sys"; } 2>/dev/null; then echo "$sys"; else echo "$HOME/delete_ltp_cmlreader.log"; fi
}
LOG_FILE="$(pick_log_file)"
log() { printf '[%(%Y-%m-%d %H:%M:%S%z)T] %s\n' -1 "$*" >> "$LOG_FILE"; }
note() { echo "$*" | tee -a "$LOG_FILE" >/dev/null; }
backup_json() {
local ts backup
ts="$(date +"%Y%m%d-%H%M%S")"; backup="${JSON}.bak.${ts}"
if (( DRY_RUN )); then
echo "Backup: planned" ; log "Backup planned: $backup"
else
cp -p "$JSON" "$backup"
echo "Backup: created"
log "Backup created: $backup"
fi
}
confirm_all() {
if (( ASSUME_YES )); then return 0; fi
read -r -p "Delete ALL sessions for subject='$SUBJECT', experiment='$EXPERIMENT'? Type YES to proceed: " ans
case "${ans,,}" in y|yes) return 0 ;; YES) return 0 ;; *) return 1 ;; esac
}
# --- Owner fix via RAM_maint: cp→rm→mv + chmod g+rw ---
fix_owner_with_copy() {
local json="${JSON:-/protocols/ltp.json}"
# Dry run
if (( DRY_RUN )); then
echo "Ownership: planned (maint:RAM_maint, chmod 644)"
log "OWNER-FIX PLAN: chown maint:RAM_maint \"$json\" && chmod 644 \"$json\""
return 0
fi
if [[ ! -f "$json" ]]; then
echo "Ownership: skipped (file not found) ⚠️"
log "OWNER-FIX SKIPPED: file not found at $json"
return 0
fi
# Apply ownership and permissions
if chown maint:RAM_maint "$json" && chmod 644 "$json"; then
echo "Ownership: set to maint:RAM_maint (rw-r--r--)"
log "OWNER-FIX DONE: $(ls -l "$json" 2>/dev/null || true)"
else
echo "Ownership: chown/chmod failed ⚠️"
log "OWNER-FIX ERROR: could not set ownership or permissions for $json"
fi
}
# ---------- Python helpers (schema-aware, no jq) ----------
py_show_single() {
python3 - "$JSON" "$SUBJECT" "$EXPERIMENT" "$SESSION" <<'PY'
import json, sys
path, subj, exp, sess = sys.argv[1:5]
with open(path) as f: d = json.load(f)
node = d.get("protocols", {}).get("ltp", {}).get("subjects", {}).get(subj, {}) \
.get("experiments", {}).get(exp, {}).get("sessions", {})
val = node.get(str(sess))
print("__MISSING__" if val is None else json.dumps(val, indent=2, sort_keys=True))
PY
}
py_show_all_sessions() {
python3 - "$JSON" "$SUBJECT" "$EXPERIMENT" <<'PY'
import json, sys
path, subj, exp = sys.argv[1:4]
with open(path) as f: d = json.load(f)
node = d.get("protocols", {}).get("ltp", {}).get("subjects", {}).get(subj, {}) \
.get("experiments", {}).get(exp, {}).get("sessions", None)
print("__MISSING__" if node is None else json.dumps(node, indent=2, sort_keys=True))
PY
}
# Delete one session, prune empties; plan|write (strict: errors if missing)
py_apply_single() {
python3 - "$JSON" "$SUBJECT" "$EXPERIMENT" "$SESSION" "$1" <<'PY'
import json, sys, tempfile, os, shutil, errno
path, subj, exp, sess, mode = sys.argv[1:6]
sess = str(sess)
def atomic_write(dst_path, data):
d = os.path.dirname(dst_path) or "."
fd, tmp = tempfile.mkstemp(prefix="ltp_json_", suffix=".tmp", dir=d)
os.close(fd)
with open(tmp, "w") as f: json.dump(data, f, indent=2, sort_keys=True)
try:
os.replace(tmp, dst_path)
except OSError as e:
if e.errno == errno.EXDEV:
shutil.move(tmp, dst_path)
else:
raise
def plan_delete_single(d):
removed = []
p = d.get("protocols", {}).get("ltp", {}).get("subjects", {}).get(subj)
if not p or "experiments" not in p or exp not in p["experiments"]:
raise SystemExit("ERROR: subject/experiment not found in JSON")
sessions = p["experiments"][exp].get("sessions", {})
if sess not in sessions:
raise SystemExit("ERROR: session not found in JSON")
sessions.pop(sess, None)
removed.append(f'json:protocols.ltp.subjects[{subj}].experiments[{exp}].sessions[{sess}]')
eobj = p["experiments"][exp]
if not sessions:
other = {k:v for k,v in eobj.items() if k!="sessions" and v not in ({}, [], "", None)}
if not other:
p["experiments"].pop(exp, None)
removed.append(f'json:protocols.ltp.subjects[{subj}].experiments[{exp}] (empty after prune)')
if not p.get("experiments"):
d["protocols"]["ltp"]["subjects"].pop(subj, None)
removed.append(f'json:protocols.ltp.subjects[{subj}] (empty after prune)')
return removed
with open(path) as f: data = json.load(f)
removed = plan_delete_single(data)
print("PLAN:"); [print(r) for r in removed]
if mode == "plan": sys.exit(0)
atomic_write(path, data)
print("WROTE:OK")
PY
}
# Delete all sessions, prune empties; tolerant if path missing; plan|write
py_apply_all_sessions() {
python3 - "$JSON" "$SUBJECT" "$EXPERIMENT" "$1" <<'PY'
import json, sys, tempfile, os, shutil, errno
path, subj, exp, mode = sys.argv[1:5]
def atomic_write(dst_path, data):
d = os.path.dirname(dst_path) or "."
fd, tmp = tempfile.mkstemp(prefix="ltp_json_", suffix=".tmp", dir=d)
os.close(fd)
with open(tmp, "w") as f: json.dump(data, f, indent=2, sort_keys=True)
try:
os.replace(tmp, dst_path)
except OSError as e:
if e.errno == errno.EXDEV:
shutil.move(tmp, dst_path)
else:
raise
def plan_delete_all_sessions(d):
removed = []
prot = d.get("protocols", {})
ltp = prot.get("ltp", {})
subs = ltp.get("subjects", {})
subj_node = subs.get(subj)
if not subj_node or "experiments" not in subj_node or exp not in subj_node["experiments"]:
return removed, False
exp_node = subj_node["experiments"][exp]
exp_node["sessions"] = {}
removed.append(f'json:protocols.ltp.subjects[{subj}].experiments[{exp}].sessions (cleared to {{}})')
other = {k:v for k,v in exp_node.items() if k!="sessions" and v not in ({}, [], "", None)}
if not exp_node.get("sessions") and not other:
subj_node["experiments"].pop(exp, None)
removed.append(f'json:protocols.ltp.subjects[{subj}].experiments[{exp}] (empty after prune)')
if not subj_node.get("experiments"):
subs.pop(subj, None)
removed.append(f'json:protocols.ltp.subjects[{subj}] (empty after prune)')
return removed, True
with open(path) as f: data = json.load(f)
removed, changed = plan_delete_all_sessions(data)
print("PLAN:"); [print(r) for r in removed] if removed else print("PLAN:(no JSON changes)")
if mode == "plan": sys.exit(0)
if changed:
atomic_write(path, data)
print("WROTE:OK")
else:
print("WROTE:SKIPPED")
PY
}
# ---------- FS helpers (concise console; detail to log) ----------
print_fs_plan_single() {
{
echo "FS PLAN single:"
local p="$SESSIONS_DIR/$SESSION"
[[ -d "$p" ]] && echo " delete $p" || echo " missing $p"
echo " prune $SESSIONS_DIR"
echo " prune $LTP_FOLDER/subjects/$SUBJECT/experiments/$EXPERIMENT"
echo " prune $LTP_FOLDER/subjects/$SUBJECT"
} >> "$LOG_FILE"
}
do_fs_single() {
local p="$SESSIONS_DIR/$SESSION"
if [[ -d "$p" ]]; then
rm -rf -- "$p"
echo "Folder deleted"
log "Removed folder: $p"
else
echo "Folder not found"
log "Folder missing (skipped): $p"
fi
rmdir "$SESSIONS_DIR" 2>/dev/null && log "Pruned empty: $SESSIONS_DIR" || true
rmdir "$LTP_FOLDER/subjects/$SUBJECT/experiments/$EXPERIMENT" 2>/dev/null && log "Pruned empty: $LTP_FOLDER/subjects/$SUBJECT/experiments/$EXPERIMENT" || true
rmdir "$LTP_FOLDER/subjects/$SUBJECT" 2>/dev/null && log "Pruned empty: $LTP_FOLDER/subjects/$SUBJECT" || true
}
print_fs_plan_all() {
{
echo "FS PLAN all:"
if [[ -d "$SESSIONS_DIR" ]]; then
echo " delete-all under $SESSIONS_DIR"
find "$SESSIONS_DIR" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | sed 's/^/ - /' | head -n 50
else
echo " missing $SESSIONS_DIR"
fi
echo " prune $SESSIONS_DIR"
echo " prune $LTP_FOLDER/subjects/$SUBJECT/experiments/$EXPERIMENT"
echo " prune $LTP_FOLDER/subjects/$SUBJECT"
} >> "$LOG_FILE"
}
do_fs_all() {
if [[ -d "$SESSIONS_DIR" ]]; then
find "$SESSIONS_DIR" -mindepth 1 -maxdepth 1 -type d -print0 2>/dev/null | xargs -0 -I{} rm -rf -- "{}" || true
echo "All session folders deleted"
log "Removed all session folders under: $SESSIONS_DIR"
else
echo "No session folder to delete"
log "Sessions directory not present: $SESSIONS_DIR"
fi
rmdir "$SESSIONS_DIR" 2>/dev/null && log "Pruned empty: $SESSIONS_DIR" || true
rmdir "$LTP_FOLDER/subjects/$SUBJECT/experiments/$EXPERIMENT" 2>/dev/null && log "Pruned empty: $LTP_FOLDER/subjects/$SUBJECT/experiments/$EXPERIMENT" || true
rmdir "$LTP_FOLDER/subjects/$SUBJECT" 2>/dev/null && log "Pruned empty: $LTP_FOLDER/subjects/$SUBJECT" || true
}
# ---------- Main ----------
echo "Deleting: experiment='$EXPERIMENT' subject='$SUBJECT' session='${SESSION:-ALL}'"
log "----- delete_ltp_cmlreader start -----"
log "Args: experiment='$EXPERIMENT' subject='$SUBJECT' session='${SESSION:-ALL}' dry_run=$DRY_RUN log='$LOG_FILE'"
if [[ ! -f "$JSON" ]]; then echo "JSON file missing"; log "ERROR: JSON not found at $JSON"; exit 1; fi
if [[ -n "$SESSION" ]]; then
# Show minimal JSON status, log details
if [[ "$(py_show_single)" == "__MISSING__" ]]; then
echo "JSON entry not found"
else
echo "JSON entry exists"
fi
{ echo "[DETAIL] Current JSON entry:"; py_show_single; } >> "$LOG_FILE"
if (( DRY_RUN )); then
py_apply_single plan >> "$LOG_FILE" 2>&1 && echo "Planned JSON delete" || { echo "Plan error"; log "Plan error (single)"; }
print_fs_plan_single
echo "Ownership: planned (RAM_maint)"
log "DRY-RUN complete"
exit 0
fi
backup_json
if py_apply_single write >> "$LOG_FILE" 2>&1; then
echo "JSON updated"
else
echo "JSON update failed"; exit 1
fi
do_fs_single
fix_owner_with_copy
echo "Done."
else
if [[ "$(py_show_all_sessions)" == "__MISSING__" ]]; then
echo "No JSON sessions found"
else
echo "JSON sessions found"
fi
{ echo "[DETAIL] Current JSON sessions:"; py_show_all_sessions; } >> "$LOG_FILE"
if (( DRY_RUN )); then
py_apply_all_sessions plan >> "$LOG_FILE" 2>&1 && echo "Planned JSON delete-all" || { echo "Plan error"; log "Plan error (all)"; }
print_fs_plan_all
echo "Ownership: planned (RAM_maint)"
log "DRY-RUN complete"
exit 0
fi
if ! confirm_all; then echo "Aborted."; log "Aborted by user."; exit 1; fi
backup_json
if py_apply_all_sessions write >> "$LOG_FILE" 2>&1; then
echo "JSON updated"
else
echo "JSON update skipped"; log "JSON write skipped (no JSON changes)"
fi
do_fs_all
fix_owner_with_copy
echo "Done."
fi
log "----- delete_ltp_cmlreader end -----"