-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_script5.sh
More file actions
executable file
·704 lines (590 loc) · 25.1 KB
/
final_script5.sh
File metadata and controls
executable file
·704 lines (590 loc) · 25.1 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#!/bin/zsh
#
# ollama_analyzer.sh - Script to analyze code with Ollama models and track performance metrics
# Author: Ian Trimble
# Created: April 28, 2025
# Version: 1.0.0
#
# Enable debug mode only when explicitly requested
if [[ "$1" == "--debug" ]]; then
set -x
shift
fi
# =================== CONFIGURATION ===================
# App information
APP_NAME="Ollama Script Analyzer"
APP_VERSION="1.0.0"
APP_AUTHOR="Ian Trimble"
# Directory structure
BENCHMARK_DIR="${HOME}/ollama_benchmarks"
SESSION_ID=$(date +%Y%m%d_%H%M%S)_$(openssl rand -hex 4)
BENCHMARK_LOG="${BENCHMARK_DIR}/benchmark_log.csv"
METRICS_LOG="${BENCHMARK_DIR}/metrics_${SESSION_ID}.json"
CHANGELOG="${BENCHMARK_DIR}/changelog.md"
README="$(pwd)/README.md"
OLLAMA_API="http://localhost:11434/api/chat"
# Default model (can be overridden)
DEFAULT_MODEL="qwen2.5:1.5b"
# Create benchmark directory if it doesn't exist
mkdir -p "${BENCHMARK_DIR}"
# Initialize benchmark file if it doesn't exist
if [ ! -f "${BENCHMARK_LOG}" ]; then
echo "timestamp,session_id,script_name,script_size_bytes,script_lines,script_chars,model,operation,duration,tokens,cpu_usage,memory_usage" > "${BENCHMARK_LOG}"
fi
# Initialize changelog if it doesn't exist
if [ ! -f "${CHANGELOG}" ]; then
{
echo "# Ollama Analyzer Changelog"
echo ""
echo "## Version 1.0.0 - $(date '+%Y-%m-%d')"
echo "- Initial release"
echo "- Added benchmarking capabilities"
echo "- Added model selection"
echo "- Added detailed metrics tracking"
} > "${CHANGELOG}"
else
# Check if version entry exists, add if not
if ! grep -q "## Version ${APP_VERSION}" "${CHANGELOG}"; then
sed -i '' "1a\\
\\
## Version ${APP_VERSION} - $(date '+%Y-%m-%d')\\
- Enhanced benchmarking capabilities\\
- Improved error handling\\
- Added compatibility with more script types
" "${CHANGELOG}"
fi
fi
# Initialize metrics JSON log
echo "{\"session_id\": \"${SESSION_ID}\", \"timestamp\": \"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\", \"app_version\": \"${APP_VERSION}\", \"metrics\": []}" > "${METRICS_LOG}"
# =================== HELPER FUNCTIONS ===================
# Function to display usage information
show_usage() {
echo "Usage: $0 [OPTIONS] <input_file> [model]"
echo ""
echo "Options:"
echo " --debug Enable debug mode"
echo " --help Show this help message"
echo " --list-models List available Ollama models"
echo " --version Show version information"
echo ""
echo "Arguments:"
echo " <input_file> Path to script file to analyze"
echo " [model] Optional Ollama model name (default: ${DEFAULT_MODEL})"
echo ""
echo "Example:"
echo " $0 my_script.sh"
echo " $0 my_script.sh codellama:7b"
echo ""
exit 0
}
# Function to display version information
show_version() {
echo "${APP_NAME} v${APP_VERSION}"
echo "Author: ${APP_AUTHOR}"
echo "Created: April 28, 2025"
exit 0
}
# Function to get system information
get_system_info() {
local cpu_info=$(sysctl -n machdep.cpu.brand_string 2>/dev/null || echo "Unknown")
local memory_info=$(sysctl -n hw.memsize 2>/dev/null | awk '{print int($1/1024/1024/1024) " GB"}' || echo "Unknown")
local os_info=$(sw_vers -productVersion 2>/dev/null || echo "Unknown")
local ollama_version=$(ollama --version 2>/dev/null || echo "Unknown")
# Add system info to metrics log
jq --arg cpu "${cpu_info}" \
--arg mem "${memory_info}" \
--arg os "${os_info}" \
--arg ollama "${ollama_version}" \
'.system_info = {"cpu": $cpu, "memory": $mem, "os": $os, "ollama_version": $ollama}' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
echo "System Info: CPU: ${cpu_info}, Memory: ${memory_info}, OS: ${os_info}, Ollama: ${ollama_version}"
}
# Function to get current resource usage
get_resource_usage() {
local cpu_usage=$(ps -o %cpu= -p $$ | awk '{print $1}')
local memory_usage=$(ps -o rss= -p $$ | awk '{print int($1/1024) " MB"}')
echo "${cpu_usage},${memory_usage}"
}
# Function to log benchmark data
log_benchmark() {
local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
local script_name=$1
local script_size=$2
local script_lines=$3
local script_chars=$4
local model=$5
local operation=$6
local duration=$7
local token_count=$8
# Get resource usage
local resource_usage=$(get_resource_usage)
local cpu_usage=$(echo ${resource_usage} | cut -d, -f1)
local memory_usage=$(echo ${resource_usage} | cut -d, -f2)
# Log to CSV for detailed data
echo "${timestamp},${SESSION_ID},${script_name},${script_size},${script_lines},${script_chars},${model},${operation},${duration},${token_count},${cpu_usage},${memory_usage}" >> "${BENCHMARK_LOG}"
# Add metric to JSON log
jq --arg timestamp "${timestamp}" \
--arg script "${script_name}" \
--arg size "${script_size}" \
--arg lines "${script_lines}" \
--arg chars "${script_chars}" \
--arg model "${model}" \
--arg op "${operation}" \
--arg dur "${duration}" \
--arg tokens "${token_count}" \
--arg cpu "${cpu_usage}" \
--arg mem "${memory_usage}" \
'.metrics += [{"timestamp": $timestamp, "script": $script, "size_bytes": $size, "line_count": $lines, "char_count": $chars, "model": $model, "operation": $op, "duration": $dur, "token_count": $tokens, "cpu_usage": $cpu, "memory_usage": $mem}]' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
# Return for chaining
echo "${operation}:${duration}"
}
# Function to generate a benchmark summary
generate_benchmark_summary() {
local model=$1
local script_name=$2
local total_time=$3
local api_time=$4
local parse_time=$5
local script_size=$6
local script_lines=$7
local script_chars=$8
local token_count=$9
echo "═════════════════════════════════════════════════"
echo "📊 BENCHMARK SUMMARY FOR ${script_name}"
echo "═════════════════════════════════════════════════"
echo "🤖 Model: ${model}"
echo "⏱️ Total analysis time: ${total_time}s"
echo "🔄 API request time: ${api_time}s"
echo "🔍 Response parsing time: ${parse_time}s"
echo "📝 Response size: ~${token_count} words"
echo "📂 Script metrics:"
echo " - Size: ${script_size} bytes ($(printf "%.2f" $(echo "scale=2; ${script_size}/1024" | bc)) KB)"
echo " - Lines: ${script_lines}"
echo " - Characters: ${script_chars}"
echo "═════════════════════════════════════════════════"
echo "📋 Session ID: ${SESSION_ID}"
echo "📊 Detailed metrics saved to: ${METRICS_LOG}"
}
# Function to update the changelog
update_changelog() {
local script_name=$1
local model=$2
local duration=$3
# Get the current date
local today=$(date '+%Y-%m-%d')
# Check if there's already an entry for today
if grep -q "### ${today}" "${CHANGELOG}"; then
# Append to today's entry
sed -i '' "/### ${today}/a\\
- Analyzed ${script_name} with ${model} (${duration}s)
" "${CHANGELOG}"
else
# Create a new entry for today
sed -i '' "/^## Version/a\\
\\
### ${today}\\
- Analyzed ${script_name} with ${model} (${duration}s)
" "${CHANGELOG}"
fi
}
# Function to check if running in a terminal
is_terminal() {
[ -t 0 ]
}
# Function to count script features
count_script_features() {
local content="$1"
# Count functions (very basic, may need refinement)
local function_count=$(echo "${content}" | grep -c "function " || echo "0")
# Count commands (very rough estimate)
local command_count=$(echo "${content}" | grep -v "^#" | grep -v "^$" | grep -c ";" || echo "0")
# Count conditionals
local if_count=$(echo "${content}" | grep -c "if " || echo "0")
local case_count=$(echo "${content}" | grep -c "case " || echo "0")
# Count loops
local for_count=$(echo "${content}" | grep -c "for " || echo "0")
local while_count=$(echo "${content}" | grep -c "while " || echo "0")
# Add features to metrics log
jq --arg functions "${function_count}" \
--arg commands "${command_count}" \
--arg ifs "${if_count}" \
--arg cases "${case_count}" \
--arg fors "${for_count}" \
--arg whiles "${while_count}" \
'.script_features = {"function_count": $functions, "command_count": $commands, "if_count": $ifs, "case_count": $cases, "for_count": $fors, "while_count": $whiles}' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
}
# Function to check for dependencies
check_dependencies() {
local missing_deps=0
# Start timer
local start_time=$(date +%s.%N)
# Check for jq
if ! command -v jq &> /dev/null; then
echo "Error: jq is required. Please install jq (e.g., 'brew install jq')."
missing_deps=1
fi
# Check for bc
if ! command -v bc &> /dev/null; then
echo "Error: bc is required. Please install bc."
missing_deps=1
fi
# Check for ollama
if ! command -v ollama &> /dev/null; then
echo "Error: ollama is required. Please install Ollama."
missing_deps=1
else
# Check if Ollama server is running
if ! curl -s -m 2 "http://localhost:11434/api/tags" &> /dev/null; then
echo "Error: Ollama server is not running. Please start it with 'ollama serve'."
missing_deps=1
fi
fi
# End timer
local end_time=$(date +%s.%N)
local duration=$(printf "%.2f" $(echo "${end_time} - ${start_time}" | bc))
log_benchmark "system" "0" "0" "0" "system" "dependency_check" "${duration}" "0"
return ${missing_deps}
}
# Function to get available models
get_models() {
local start_time=$(date +%s.%N)
echo "Fetching available models..."
local ollama_output=$(ollama list 2>/dev/null)
if [ $? -ne 0 ]; then
echo "Error: Failed to run 'ollama list'. Ensure Ollama is running."
exit 1
fi
# Parse models
models=($(echo "$ollama_output" | tail -n +2 | awk '{print $1}'))
model_ids=($(echo "$ollama_output" | tail -n +2 | awk '{print $2}'))
model_sizes=($(echo "$ollama_output" | tail -n +2 | awk '{print $3, $4}'))
if [ ${#models[@]} -eq 0 ]; then
echo "No models found. Please download a model using 'ollama pull <model>'."
exit 1
fi
# Add models to metrics log
local models_json="["
for ((i=1; i<=${#models[@]}; i++)); do
models_json+="\"${models[$i]}\""
if [ $i -lt ${#models[@]} ]; then
models_json+=","
fi
done
models_json+="]"
jq --argjson models "${models_json}" '.available_models = $models' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
local end_time=$(date +%s.%N)
local duration=$(printf "%.2f" $(echo "${end_time} - ${start_time}" | bc))
log_benchmark "system" "0" "0" "0" "system" "fetch_models" "${duration}" "${#models[@]}"
# Just list models if that's what was requested
if [[ "$1" == "--list" ]]; then
echo "Available models:"
for ((i=1; i<=${#models[@]}; i++)); do
echo "${i}. ${models[$i]}: ${model_sizes[$i]}"
done
exit 0
fi
echo "Available models: ${models[@]}"
}
# Function to select a model
select_model() {
local start_time=$(date +%s.%N)
# If a model is provided as an argument, use it
if [ $# -ge 2 ]; then
local provided_model="$2"
# Check if the model is available
if ollama list | grep -q "${provided_model}"; then
model="${provided_model}"
else
echo "Warning: Model '$provided_model' not found in available models."
echo "Pulling the model..."
ollama pull "${provided_model}"
if [ $? -ne 0 ]; then
echo "Error: Failed to pull model '${provided_model}'. Using default model: ${DEFAULT_MODEL}"
model="${DEFAULT_MODEL}"
else
model="${provided_model}"
fi
fi
elif is_terminal; then
# Interactive selection if terminal is available
echo "Select a model:"
select model in "${models[@]}"; do
if [ -n "$model" ]; then
break
fi
done
else
# Non-interactive: use the default model
model="${DEFAULT_MODEL}"
echo "No terminal available for interactive selection. Using default model: ${model}"
fi
local end_time=$(date +%s.%N)
local duration=$(printf "%.2f" $(echo "${end_time} - ${start_time}" | bc))
log_benchmark "${INPUT}" "0" "0" "0" "${model}" "model_selection" "${duration}" "0"
# Record the selected model
jq --arg model "${model}" '.selected_model = $model' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
echo "Selected model: ${model}"
}
# Function to validate input file
validate_input_file() {
local input="$1"
local start_time=$(date +%s.%N)
if [ ! -f "${input}" ]; then
echo "Error: Input file '${input}' does not exist."
exit 1
fi
# Get file stats
local file_size=$(stat -f%z "${input}")
CONTENT=$(cat "${input}")
local line_count=$(echo "${CONTENT}" | wc -l | tr -d ' ')
local char_count=$(echo "${CONTENT}" | wc -c | tr -d ' ')
# Record file metrics
jq --arg file "${input}" \
--arg size "${file_size}" \
--arg lines "${line_count}" \
--arg chars "${char_count}" \
'.input_file = {"path": $file, "size_bytes": $size, "line_count": $lines, "char_count": $chars}' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
# Determine file extension
ext="${input##*.}"
# Check supported file types
case "${ext}" in
sh|bash|zsh)
# Check if it's base64 encoded (from legacy code)
if echo "$CONTENT" | grep -qE '^[A-Za-z0-9+/=]+$'; then
# Attempt to decode as base64
local DECODED_CONTENT=$(echo "$CONTENT" | base64 -d 2>/dev/null)
if [ $? -eq 0 ] && echo "$DECODED_CONTENT" | grep -qE "^#!/bin/(bash|zsh|sh)"; then
CONTENT="$DECODED_CONTENT"
echo "Input detected as base64-encoded shell script. Decoded successfully."
else
echo "Input appears to be base64 but failed to decode as a valid shell script. Treating as plain text."
fi
fi
# Validate as shell script
if ! echo "${CONTENT}" | grep -qE "^#!/bin/(bash|zsh|sh)"; then
echo "Warning: Input does not appear to be a valid shell script (missing shebang)."
fi
SCRIPT_TYPE="shell"
;;
scpt|applescript)
# AppleScript
SCRIPT_TYPE="applescript"
;;
py|python)
# Python script
SCRIPT_TYPE="python"
;;
rb|ruby)
# Ruby script
SCRIPT_TYPE="ruby"
;;
js|javascript)
# JavaScript
SCRIPT_TYPE="javascript"
;;
*)
echo "Warning: Unsupported file type: ${ext}. Will attempt to analyze as generic script."
SCRIPT_TYPE="generic"
;;
esac
# Count script features
count_script_features "${CONTENT}"
local end_time=$(date +%s.%N)
local duration=$(printf "%.2f" $(echo "${end_time} - ${start_time}" | bc))
log_benchmark "${input}" "${file_size}" "${line_count}" "${char_count}" "${model}" "file_validation" "${duration}" "0"
# Store file metrics for later use
FILE_SIZE="${file_size}"
LINE_COUNT="${line_count}"
CHAR_COUNT="${char_count}"
echo "File validated: ${input} (${file_size} bytes, ${line_count} lines)"
}
# Function to analyze script with Ollama
analyze_script() {
local input="$1"
local model="$2"
local script_basename=$(basename "${input}")
local start_time=$(date +%s.%N)
echo "Analyzing script with ${model}..."
# Create request payload
local payload=$(jq -n \
--arg model "${model}" \
--arg content "${CONTENT}" \
--arg filename "${script_basename}" \
--arg script_type "${SCRIPT_TYPE}" \
'{
"model": $model,
"messages": [
{
"role": "system",
"content": "You are an expert code analyst tasked with producing professional, accurate, and comprehensive documentation. Analyze the provided \($script_type) script with precision, describing only the functionality explicitly present in the code. Generate a detailed Markdown README section that is clear, thorough, and professionally structured, suitable for developers and end-users."
},
{
"role": "user",
"content": "Analyze the following \($script_type) script provided as plain text. Pay close attention to specific elements such as references to applications, system paths, and command-line tools. Consider the script'\''s potential impact on the system.\n\nGenerate a Markdown README section with these sections:\n\n- **Overview**: Summarize the script'\''s purpose and primary actions.\n- **Requirements**: List prerequisites inferred from the script.\n- **Usage**: Provide precise instructions for running the script.\n- **What the Script Does**: Describe the script'\''s operations step-by-step.\n- **Important Notes**: Highlight critical details derived from the script.\n- **Disclaimer**: Warn about risks of running the script.\n\nFile: \($filename)\n\nScript Content:\n\($content)"
}
],
"stream": false
}')
# Log the prompt size (in characters)
local prompt_size=$(echo "${payload}" | jq -r '.messages[1].content' | wc -c | tr -d ' ')
jq --arg size "${prompt_size}" '.prompt_size_chars = $size' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
# Send request to Ollama API
echo "Sending request to Ollama API..."
local request_start_time=$(date +%s.%N)
# Temporary file for response
local temp_response=$(mktemp)
# Send the request
curl -s -X POST "${OLLAMA_API}" \
-H "Content-Type: application/json" \
-d "${payload}" > "${temp_response}"
local request_end_time=$(date +%s.%N)
local request_duration=$(printf "%.2f" $(echo "${request_end_time} - ${request_start_time}" | bc))
log_benchmark "${script_basename}" "${FILE_SIZE}" "${LINE_COUNT}" "${CHAR_COUNT}" "${model}" "api_request" "${request_duration}" "${prompt_size}"
# Parse response
echo "Processing response..."
local parse_start_time=$(date +%s.%N)
if grep -q "error" "${temp_response}"; then
echo "Error in Ollama response:"
cat "${temp_response}"
jq --arg error "$(cat ${temp_response})" '.error = $error' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
rm "${temp_response}"
exit 1
fi
# Extract metrics from response if available
local total_duration=$(jq -r '.total_duration // 0' "${temp_response}")
local eval_count=$(jq -r '.eval_count // 0' "${temp_response}")
local prompt_eval_count=$(jq -r '.prompt_eval_count // 0' "${temp_response}")
local eval_duration=$(jq -r '.eval_duration // 0' "${temp_response}")
local prompt_eval_duration=$(jq -r '.prompt_eval_duration // 0' "${temp_response}")
# Convert to seconds if in nanoseconds
if [ "${total_duration}" -gt 1000000000 ]; then
total_duration=$(printf "%.2f" $(echo "scale=2; ${total_duration}/1000000000" | bc))
fi
# Add Ollama-reported metrics to log
jq --arg total "${total_duration}" \
--arg eval "${eval_count}" \
--arg prompt_eval "${prompt_eval_count}" \
--arg eval_dur "${eval_duration}" \
--arg prompt_dur "${prompt_eval_duration}" \
'.ollama_metrics = {"total_duration": $total, "eval_count": $eval, "prompt_eval_count": $prompt_eval, "eval_duration": $eval_dur, "prompt_eval_duration": $prompt_dur}' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
# Extract response content - try different methods for compatibility
if jq -e '.message.content' "${temp_response}" > /dev/null 2>&1; then
RESPONSE=$(jq -r '.message.content' "${temp_response}")
else
# Fallback method using grep and sed
RESPONSE=$(grep -o '"content":"[^"]*"' "${temp_response}" | sed 's/"content":"//;s/"//')
# If that fails, try perl
if [ -z "${RESPONSE}" ]; then
RESPONSE=$(perl -0777 -ne 'print $1 if /"content":"(.*?)"/s' "${temp_response}")
fi
fi
# If still empty, try one more approach
if [ -z "${RESPONSE}" ]; then
RESPONSE=$(cat "${temp_response}" | python3 -c "
import json, sys
try:
data = json.load(sys.stdin)
print(data['message']['content'])
except Exception as e:
print(f'Error parsing JSON: {e}', file=sys.stderr)
sys.exit(1)
" 2>/dev/null)
fi
# Check if response is empty
if [ -z "${RESPONSE}" ]; then
echo "Error: Empty response from Ollama."
echo "Raw response:"
cat "${temp_response}"
jq --arg error "Empty response" '.error = $error' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
rm "${temp_response}"
exit 1
fi
# Calculate response metrics
local response_char_count=$(echo "${RESPONSE}" | wc -c | tr -d ' ')
local response_line_count=$(echo "${RESPONSE}" | wc -l | tr -d ' ')
local response_word_count=$(echo "${RESPONSE}" | wc -w | tr -d ' ')
# Add response metrics to log
jq --arg chars "${response_char_count}" \
--arg lines "${response_line_count}" \
--arg words "${response_word_count}" \
'.response_metrics = {"char_count": $chars, "line_count": $lines, "word_count": $words}' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
# Save the full response for analysis
echo "${RESPONSE}" > "${BENCHMARK_DIR}/response_${SESSION_ID}.md"
# Clean up
rm "${temp_response}"
local parse_end_time=$(date +%s.%N)
local parse_duration=$(printf "%.2f" $(echo "${parse_end_time} - ${parse_start_time}" | bc))
log_benchmark "${script_basename}" "${FILE_SIZE}" "${LINE_COUNT}" "${CHAR_COUNT}" "${model}" "response_parsing" "${parse_duration}" "${response_word_count}"
# Update README.md (with model information)
echo "Updating README.md..."
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
{
echo -e "\n## ${script_basename} (Analyzed with ${model})"
echo -e "#### Analysis Date: ${timestamp}"
echo -e "${RESPONSE}\n"
echo -e "### License"
echo -e "This script is provided under the MIT License.\n"
echo -e "MIT License\n"
echo -e "Copyright (c) $(date +%Y) ${APP_AUTHOR}\n"
echo -e "Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n"
echo -e "The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n"
echo -e "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
} >> "${README}"
echo "README.md updated for ${script_basename}"
local end_time=$(date +%s.%N)
local total_duration=$(printf "%.2f" $(echo "${end_time} - ${start_time}" | bc))
# Log total analysis time
log_benchmark "${script_basename}" "${FILE_SIZE}" "${LINE_COUNT}" "${CHAR_COUNT}" "${model}" "total_analysis" "${total_duration}" "${response_word_count}"
# Update changelog
update_changelog "${script_basename}" "${model}" "${total_duration}"
# Generate benchmark summary
generate_benchmark_summary "${model}" "${script_basename}" "${total_duration}" "${request_duration}" "${parse_duration}" "${FILE_SIZE}" "${LINE_COUNT}" "${CHAR_COUNT}" "${response_word_count}"
# Add final timestamp to metrics log
jq --arg ts "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" '.completion_timestamp = $ts' "${METRICS_LOG}" > "${METRICS_LOG}.tmp" && mv "${METRICS_LOG}.tmp" "${METRICS_LOG}"
return 0
}
# =================== MAIN SCRIPT ===================
# Start timer for overall execution
SCRIPT_START_TIME=$(date +%s.%N)
# Get system information
get_system_info
# Process command-line arguments
case "$1" in
--help)
show_usage
;;
--version)
show_version
;;
--list-models)
get_models --list
;;
"")
show_usage
;;
esac
# Input handling
if [ $# -lt 1 ]; then
echo "Error: No input file specified."
echo "Run '$0 --help' for usage information."
exit 1
fi
INPUT="$1"
echo "Input file: ${INPUT}"
# Check dependencies
check_dependencies || exit 1
# Get available models
get_models
# Select model and analyze
select_model "$@"
validate_input_file "${INPUT}"
analyze_script "${INPUT}" "${model}"
# End timer for overall execution
SCRIPT_END_TIME=$(date +%s.%N)
TOTAL_EXECUTION_TIME=$(printf "%.2f" $(echo "${SCRIPT_END_TIME} - ${SCRIPT_START_TIME}" | bc))
log_benchmark "${INPUT}" "${FILE_SIZE}" "${LINE_COUNT}" "${CHAR_COUNT}" "${model}" "script_execution" "${TOTAL_EXECUTION_TIME}" "0"
echo "Script completed in ${TOTAL_EXECUTION_TIME} seconds"
echo "Detailed metrics saved to:"
echo "- CSV log: ${BENCHMARK_LOG}"
echo "- JSON metrics: ${METRICS_LOG}"
echo "- Response: ${BENCHMARK_DIR}/response_${SESSION_ID}.md"
echo "- Changelog: ${CHANGELOG}"