-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_indexer
More file actions
executable file
·896 lines (780 loc) · 30.6 KB
/
kernel_indexer
File metadata and controls
executable file
·896 lines (780 loc) · 30.6 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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
#!/bin/bash
# R Kernel Indexer
# This script inventories R kernels and generates package manifests
# Check for required tools
if [ -z $(type -p jq) ]; then
echo "ERROR: Need tool jq installed to proceed." >&2
exit 1
fi
if [ -z $(type -p conda) ]; then
echo "ERROR: Need tool conda installed to proceed." >&2
exit 1
fi
# Default configuration paths
icrn_base=".icrn"
ICRN_USER_BASE=${ICRN_USER_BASE:-${HOME}/${icrn_base}}
ICRN_MANAGER_CONFIG=${ICRN_MANAGER_CONFIG:-${ICRN_USER_BASE}/manager_config.json}
# Try to read kernel root from config, with fallback
if [ -e "${ICRN_MANAGER_CONFIG}" ]; then
ICRN_KERNEL_REPOSITORY=$(jq -r ".\"icrn_central_catalog_path\"" "${ICRN_MANAGER_CONFIG}" 2>/dev/null)
if [ -n "$ICRN_KERNEL_REPOSITORY" ] && [ "$ICRN_KERNEL_REPOSITORY" != "null" ]; then
DEFAULT_KERNEL_ROOT="${ICRN_KERNEL_REPOSITORY}"
fi
fi
# Global variables
KERNEL_ROOT=""
OUTPUT_PATH=""
KERNEL_NAME=""
KERNEL_VERSION=""
LANGUAGE_FILTER=""
# Display usage information
function help() {
echo ""
echo "usage: $0 <command> [options]"
echo ""
echo "Commands:"
echo " index Index R kernels and generate manifest files"
echo " collate Run both collate-by-kernels and collate-by-packages"
echo " collate-by-kernels Collate all manifest files into a kernel-centric index"
echo " collate-by-packages Collate all manifest files into a package-centric index"
echo ""
echo "Index options:"
echo " --kernel-root PATH Path to kernel repository root (default: from config or required)"
echo " Should contain language subdirectories (R/, Python/, etc.)"
echo " --language LANG Filter by specific language (R, Python, etc.). If omitted, processes all languages"
echo " --kernel-name NAME Index only a specific kernel (optional)"
echo " --kernel-version VER Index only a specific version (requires --kernel-name)"
echo ""
echo "Collate options:"
echo " --kernel-root PATH Path to kernel repository root (default: from config or required)"
echo " Should contain language subdirectories (R/, Python/, etc.)"
echo " --language LANG Filter by specific language (R, Python, etc.). If omitted, processes all languages"
echo " --output-dir DIR Directory for output files (default: {kernel-root})"
echo " Outputs: {output-dir}/collated_manifests.json and {output-dir}/package_index.json"
echo ""
echo "Collate-by-kernels options:"
echo " --kernel-root PATH Path to kernel repository root (default: from config or required)"
echo " Should contain language subdirectories (R/, Python/, etc.)"
echo " --language LANG Filter by specific language (R, Python, etc.). If omitted, processes all languages"
echo " --output PATH Path for collated manifest (default: {kernel-root}/collated_manifests.json)"
echo ""
echo "Collate-by-packages options:"
echo " --kernel-root PATH Path to kernel repository root (default: from config or required)"
echo " Should contain language subdirectories (R/, Python/, etc.)"
echo " --language LANG Filter by specific language (R, Python, etc.). If omitted, processes all languages"
echo " --output PATH Path for package-centric manifest (default: {kernel-root}/package_index.json)"
echo ""
echo "Examples:"
echo " $0 index --kernel-root /path/to/repo/R"
echo " $0 index --kernel-root /path/to/repo/R --kernel-name cowsay"
echo " $0 index --kernel-root /path/to/repo/R --kernel-name cowsay --kernel-version 1.0"
echo " $0 collate --kernel-root /path/to/repo --output-dir /path/to/output"
echo " $0 collate --kernel-root /path/to/repo --language R"
echo " $0 collate-by-kernels --kernel-root /path/to/repo --language Python --output /path/to/collated.json"
echo " $0 collate-by-packages --kernel-root /path/to/repo --output /path/to/package_index.json"
echo ""
}
# Get ISO 8601 timestamp
function get_timestamp() {
date -u +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || date -u +"%Y-%m-%dT%H:%M:%S+00:00"
}
# Discover all kernels in the kernel root directory, optionally filtered by language
# Parameters:
# $1 - kernel_root: Root directory containing language subdirectories (e.g., R/, Python/)
# $2 - language_filter: Optional language to filter by (e.g., "R", "Python"). If empty, discovers all languages
# Returns: List of kernel tuples (language, name, version, path) via stdout
function discover_kernels() {
local kernel_root=$1
local language_filter=$2
if [ ! -d "$kernel_root" ]; then
echo "WARNING: Kernel root directory does not exist: $kernel_root" >&2
return 1
fi
# Determine which language directories to search
local language_dirs
if [ -n "$language_filter" ]; then
# Filter by specific language
language_dirs="$kernel_root/$language_filter"
if [ ! -d "$language_dirs" ]; then
echo "WARNING: Language directory does not exist: $language_dirs" >&2
return 1
fi
else
# Discover all language directories
language_dirs="$kernel_root/*"
fi
# Find all directories that match [language]/[kernel_name]/[kernel_version]/ structure
# and contain conda-meta directory
for lang_dir in $language_dirs; do
if [ ! -d "$lang_dir" ]; then
continue
fi
local language=$(basename "$lang_dir")
# Find kernel version directories
find "$lang_dir" -mindepth 2 -maxdepth 2 -type d | while read -r version_dir; do
kernel_name=$(basename "$(dirname "$version_dir")")
kernel_version=$(basename "$version_dir")
# Check if this is a valid conda environment
if [ -d "$version_dir/conda-meta" ]; then
echo "$language|$kernel_name|$kernel_version|$version_dir"
fi
done
done
}
# Extract R version from kernel environment
function get_r_version() {
local kernel_path=$1
local rscript_path="$kernel_path/bin/Rscript"
if [ ! -x "$rscript_path" ]; then
echo "" >&2
return 1
fi
# Get R version using Rscript
local r_version=$(R_LIBS='' R_HOME='' R_LIBS_USER='' R_LIBS_SITE='' "$rscript_path" --vanilla -e 'cat(R.version$major, ".", R.version$minor, sep="")' 2>/dev/null)
if [ $? -eq 0 ] && [ -n "$r_version" ]; then
echo "$r_version"
else
echo "" >&2
return 1
fi
}
# Extract Python version from kernel environment
function get_python_version() {
local kernel_path=$1
local python_path="$kernel_path/bin/python"
if [ ! -x "$python_path" ]; then
echo "" >&2
return 1
fi
# Get Python version
local py_version=$("$python_path" --version 2>&1 | sed 's/Python //' | cut -d. -f1,2)
if [ $? -eq 0 ] && [ -n "$py_version" ]; then
echo "$py_version"
else
echo "" >&2
return 1
fi
}
# Extract packages from conda list
function extract_conda_packages() {
local kernel_path=$1
local conda_list_output
# Activate conda environment and get package list
# Use conda run to execute in the environment without activating
conda_list_output=$(conda run -p "$kernel_path" conda list --json 2>/dev/null)
if [ $? -ne 0 ] || [ -z "$conda_list_output" ]; then
echo "WARNING: Failed to get conda package list for $kernel_path" >&2
echo "[]"
return 1
fi
# Parse conda JSON output and extract package name and version
echo "$conda_list_output" | jq -r '.[] | select(.name != null) | {name: .name, version: .version, source: "conda"}' | jq -s '.'
}
# Extract packages from R's installed.packages()
function extract_r_packages() {
local kernel_path=$1
local rscript_path="$kernel_path/bin/Rscript"
if [ ! -x "$rscript_path" ]; then
echo "ERROR: Rscript not found at $rscript_path" >&2
echo "[]"
return 1
fi
# Get installed packages using Rscript
# Use --vanilla to avoid loading user configs
# Extract Package, Version columns (columns 1 and 3)
# Output one package per line in format: package_name|package_version
local r_output=$(R_LIBS='' R_HOME='' R_LIBS_USER='' R_LIBS_SITE='' "$rscript_path" --vanilla -e 'pkgs <- installed.packages()[,c(1,3),drop=FALSE]; writeLines(paste(rownames(pkgs), pkgs[,2], sep="|"))' 2>/dev/null)
if [ $? -ne 0 ] || [ -z "$r_output" ]; then
echo "ERROR: Failed to get R package list" >&2
echo "[]"
return 1
fi
# Convert pipe-delimited output to JSON using jq for proper escaping
# Use a temporary file to build JSON array (avoids subshell issues)
local temp_file=$(mktemp)
echo "$r_output" | while IFS='|' read -r pkg_name pkg_version; do
if [ -n "$pkg_name" ] && [ -n "$pkg_version" ]; then
jq -n --arg name "$pkg_name" --arg version "$pkg_version" '{name: $name, version: $version, source: "r"}' >> "$temp_file"
fi
done
jq -s '.' "$temp_file"
local result=$?
rm -f "$temp_file"
return $result
}
# Merge package lists, with R packages taking precedence
function merge_package_lists() {
local conda_packages=$1
local r_packages=$2
# Create a temporary file approach or use jq to merge
# Strategy: Start with R packages, add conda packages not in R list
echo "$conda_packages" | jq --argjson r_pkgs "$r_packages" '
. as $conda |
($r_pkgs | map(.name)) as $r_names |
($conda | map(select(.name as $name | ($r_names | index($name) | not)))) as $conda_only |
$r_pkgs + $conda_only
'
}
# Detect language of a kernel by checking available binaries
function detect_kernel_language() {
local kernel_path=$1
if [ -x "$kernel_path/bin/Rscript" ] && [ -x "$kernel_path/bin/R" ]; then
echo "R"
return 0
elif [ -x "$kernel_path/bin/python" ] || [ -x "$kernel_path/bin/python3" ]; then
echo "Python"
return 0
else
# Default to R for backward compatibility, or could return "Unknown"
echo "R"
return 1
fi
}
# Index a single kernel (supports R, Python, and other languages)
function index_kernel() {
local kernel_path=$1
local kernel_name=$2
local kernel_version=$3
local language=$4 # Optional: if provided, use it; otherwise detect
echo "Indexing kernel: $kernel_name version $kernel_version"
echo " Path: $kernel_path"
# Validate conda environment
if [ ! -d "$kernel_path/conda-meta" ]; then
echo "ERROR: Not a valid conda environment (missing conda-meta/): $kernel_path" >&2
return 1
fi
# Detect language if not provided
if [ -z "$language" ]; then
language=$(detect_kernel_language "$kernel_path")
fi
echo " Language: $language"
local lang_version="unknown"
local all_packages="[]"
# Extract packages based on language
case "$language" in
R)
# Validate R and Rscript binaries
if [ ! -x "$kernel_path/bin/Rscript" ] || [ ! -x "$kernel_path/bin/R" ]; then
echo "ERROR: R/Rscript not found" >&2
return 1
fi
# Get R version
lang_version=$(get_r_version "$kernel_path")
if [ -z "$lang_version" ]; then
echo "WARNING: Could not determine R version, continuing anyway" >&2
lang_version="unknown"
fi
# Extract packages
echo " Extracting conda packages..."
local conda_packages=$(extract_conda_packages "$kernel_path")
if [ $? -ne 0 ]; then
echo "ERROR: Failed to extract conda packages" >&2
return 1
fi
echo " Extracting R packages..."
local r_packages=$(extract_r_packages "$kernel_path")
if [ $? -ne 0 ]; then
echo "ERROR: Failed to extract R packages" >&2
return 1
fi
# Merge package lists
echo " Merging package lists..."
all_packages=$(merge_package_lists "$conda_packages" "$r_packages")
;;
Python)
# Validate Python binary
local python_path="$kernel_path/bin/python"
if [ ! -x "$python_path" ] && [ ! -x "$kernel_path/bin/python3" ]; then
echo "ERROR: Python not found" >&2
return 1
fi
# Get Python version
lang_version=$(get_python_version "$kernel_path")
if [ -z "$lang_version" ]; then
echo "WARNING: Could not determine Python version, continuing anyway" >&2
lang_version="unknown"
fi
# Extract packages - conda tracks all packages including pip-installed ones
echo " Extracting conda packages..."
all_packages=$(extract_conda_packages "$kernel_path")
if [ $? -ne 0 ]; then
echo "ERROR: Failed to extract conda packages" >&2
return 1
fi
# Update source field to "python" for Python kernels
all_packages=$(echo "$all_packages" | jq 'map(.source = "python")')
;;
*)
# Unknown languages are not supported
echo "ERROR: Unsupported language '$language'" >&2
echo "Supported languages: R, Python" >&2
return 1
;;
esac
# Get package count
local package_count=$(echo "$all_packages" | jq 'length')
echo " Found $package_count packages"
# Generate manifest
local manifest_file="$kernel_path/package_manifest.json"
local indexed_date=$(get_timestamp)
# Create manifest JSON
local manifest=$(jq -n \
--arg name "$kernel_name" \
--arg version "$kernel_version" \
--arg lang "$language" \
--arg lang_ver "$lang_version" \
--arg date "$indexed_date" \
--argjson packages "$all_packages" \
'{
kernel_name: $name,
kernel_version: $version,
language: $lang,
language_version: $lang_ver,
indexed_date: $date,
packages: $packages
}')
# Write manifest file
echo "$manifest" | jq '.' > "$manifest_file"
if [ $? -eq 0 ]; then
echo " Created manifest: $manifest_file"
return 0
else
echo "ERROR: Failed to write manifest file: $manifest_file" >&2
return 1
fi
}
# Index all kernels (supports all languages)
function index_all_kernels() {
local kernel_root=$1
local filter_name=$2
local filter_version=$3
local language_filter=$4
if [ -n "$language_filter" ]; then
echo "Discovering kernels in: $kernel_root (language: $language_filter)"
else
echo "Discovering kernels in: $kernel_root (all languages)"
fi
local kernels=$(discover_kernels "$kernel_root" "$language_filter")
if [ $? -ne 0 ] || [ -z "$kernels" ]; then
echo "No kernels found or error discovering kernels" >&2
return 1
fi
local total=0
local success=0
local failed=0
# Use process substitution to avoid subshell issues
while IFS='|' read -r language kernel_name kernel_version kernel_path; do
# Apply filters if specified
if [ -n "$filter_name" ] && [ "$kernel_name" != "$filter_name" ]; then
continue
fi
if [ -n "$filter_version" ] && [ "$kernel_version" != "$filter_version" ]; then
continue
fi
total=$((total + 1))
if index_kernel "$kernel_path" "$kernel_name" "$kernel_version" "$language"; then
success=$((success + 1))
else
failed=$((failed + 1))
echo "Failed to index: $language $kernel_name $kernel_version" >&2
fi
echo ""
done < <(echo "$kernels")
echo "Indexing complete: $success succeeded, $failed failed out of $total total"
if [ $failed -gt 0 ]; then
return 1
fi
return 0
}
# Collate all manifest files
function collate_manifests() {
local kernel_root=$1
local output_path=$2
local language_filter=$3
if [ -n "$language_filter" ]; then
echo "Collating manifests from: $kernel_root (language: $language_filter)"
else
echo "Collating manifests from: $kernel_root (all languages)"
fi
local kernels=$(discover_kernels "$kernel_root" "$language_filter")
if [ $? -ne 0 ] || [ -z "$kernels" ]; then
echo "No kernels found or error discovering kernels" >&2
return 1
fi
local collated_date=$(get_timestamp)
local kernel_root_abs=$(cd "$kernel_root" 2>/dev/null && pwd || echo "$kernel_root")
# Use temporary file to collect kernel JSON objects (avoids command-line length limits)
local temp_kernels_file=$(mktemp)
local total_kernels=0
local skipped=0
while IFS='|' read -r language kernel_name kernel_version kernel_path; do
local manifest_file="$kernel_path/package_manifest.json"
if [ ! -f "$manifest_file" ]; then
echo "WARNING: Manifest not found for $kernel_name $kernel_version, skipping" >&2
skipped=$((skipped + 1))
continue
fi
# Read and validate manifest
if ! jq '.' "$manifest_file" >/dev/null 2>&1; then
echo "ERROR: Invalid manifest file: $manifest_file" >&2
skipped=$((skipped + 1))
continue
fi
# Calculate relative path from kernel root
local kernel_path_abs=$(cd "$kernel_path" 2>/dev/null && pwd || echo "$kernel_path")
local rel_path
if command -v realpath >/dev/null 2>&1; then
rel_path=$(realpath --relative-to="$kernel_root_abs" "$kernel_path_abs" 2>/dev/null || echo "$language/$kernel_name/$kernel_version")
else
# Fallback: construct relative path manually
rel_path="$language/$kernel_name/$kernel_version"
fi
# Get package count and add enhanced manifest to temp file
local package_count=$(jq '.packages | length' "$manifest_file")
# Add enhanced manifest to temp file (one JSON object per line)
jq --arg rel_path "$rel_path/package_manifest.json" \
--argjson count "$package_count" \
'. + {
manifest_path: $rel_path,
package_count: $count
}' "$manifest_file" >> "$temp_kernels_file"
total_kernels=$((total_kernels + 1))
echo " Added: $kernel_name $kernel_version ($package_count packages)"
done < <(echo "$kernels")
# Create final collated manifest by reading all kernels from temp file
local collated=$(jq -n \
--arg date "$collated_date" \
--argjson total "$total_kernels" \
--slurpfile kernels "$temp_kernels_file" \
'{
indexed_date: $date,
total_kernels: $total,
kernels: $kernels
}')
# Clean up temp file
rm -f "$temp_kernels_file"
# Write output
echo "$collated" | jq '.' > "$output_path"
if [ $? -eq 0 ]; then
echo ""
echo "Collated manifest written to: $output_path"
echo "Total kernels: $total_kernels"
if [ $skipped -gt 0 ]; then
echo "Skipped: $skipped"
fi
return 0
else
echo "ERROR: Failed to write collated manifest: $output_path" >&2
return 1
fi
}
# Collate manifests into a package-centric index
# Each package entry lists all kernels that contain it
function collate_package_centric() {
local kernel_root=$1
local output_path=$2
local language_filter=$3
if [ -n "$language_filter" ]; then
echo "Collating manifests into package-centric index from: $kernel_root (language: $language_filter)"
else
echo "Collating manifests into package-centric index from: $kernel_root (all languages)"
fi
local kernels=$(discover_kernels "$kernel_root" "$language_filter")
if [ $? -ne 0 ] || [ -z "$kernels" ]; then
echo "No kernels found or error discovering kernels" >&2
return 1
fi
local collated_date=$(get_timestamp)
# Use a temporary file to collect package-kernel mappings
# Format: package_name|package_version|source|kernel_name|kernel_version|language
local temp_mappings_file=$(mktemp)
local total_kernels=0
local skipped=0
while IFS='|' read -r language kernel_name kernel_version kernel_path; do
local manifest_file="$kernel_path/package_manifest.json"
if [ ! -f "$manifest_file" ]; then
echo "WARNING: Manifest not found for $kernel_name $kernel_version, skipping" >&2
skipped=$((skipped + 1))
continue
fi
# Read and validate manifest
if ! jq '.' "$manifest_file" >/dev/null 2>&1; then
echo "ERROR: Invalid manifest file: $manifest_file" >&2
skipped=$((skipped + 1))
continue
fi
# Get language from manifest
local kernel_language=$(jq -r '.language // "R"' "$manifest_file")
# Extract each package with kernel information (including language)
jq -r --arg kname "$kernel_name" --arg kver "$kernel_version" --arg lang "$kernel_language" \
'.packages[] | "\(.name)|\(.version)|\(.source)|\($kname)|\($kver)|\($lang)"' \
"$manifest_file" >> "$temp_mappings_file"
total_kernels=$((total_kernels + 1))
local package_count=$(jq '.packages | length' "$manifest_file")
echo " Processed: $kernel_name $kernel_version ($package_count packages)"
done < <(echo "$kernels")
# Now build package-centric index using jq
# Group by package name, then collect all kernel occurrences
local temp_packages_file=$(mktemp)
# Use jq to read pipe-delimited file and group by package name
jq -R -s '
split("\n") |
map(select(length > 0)) |
map(split("|")) |
group_by(.[0]) |
map({
name: .[0][0],
kernel_count: (map({
kernel_name: .[3],
kernel_version: .[4],
package_version: .[1],
source: .[2],
kernel_language: .[5]
}) | length),
kernels: map({
kernel_name: .[3],
kernel_version: .[4],
package_version: .[1],
source: .[2],
kernel_language: .[5]
})
})
' "$temp_mappings_file" > "$temp_packages_file"
# Create final package-centric manifest
local total_packages=$(jq 'length' "$temp_packages_file")
local collated=$(jq -n \
--arg date "$collated_date" \
--argjson total "$total_packages" \
--slurpfile packages "$temp_packages_file" \
'{
indexed_date: $date,
total_packages: $total,
packages: $packages[0]
}')
# Clean up temp files
rm -f "$temp_mappings_file" "$temp_packages_file"
# Write output
echo "$collated" | jq '.' > "$output_path"
if [ $? -eq 0 ]; then
echo ""
echo "Package-centric index written to: $output_path"
echo "Total packages: $total_packages"
echo "Total kernels processed: $total_kernels"
if [ $skipped -gt 0 ]; then
echo "Skipped: $skipped"
fi
return 0
else
echo "ERROR: Failed to write package-centric index: $output_path" >&2
return 1
fi
}
# Parse command line arguments
function parse_args() {
local cmd=$1
shift
case "$cmd" in
index)
while [ $# -gt 0 ]; do
case "$1" in
--kernel-root)
KERNEL_ROOT="$2"
shift 2
;;
--kernel-name)
KERNEL_NAME="$2"
shift 2
;;
--kernel-version)
KERNEL_VERSION="$2"
shift 2
;;
--language)
LANGUAGE_FILTER="$2"
shift 2
;;
*)
echo "ERROR: Unknown option: $1" >&2
help
exit 1
;;
esac
done
# Validate kernel-version requires kernel-name
if [ -n "$KERNEL_VERSION" ] && [ -z "$KERNEL_NAME" ]; then
echo "ERROR: --kernel-version requires --kernel-name" >&2
exit 1
fi
# Set default kernel root if not provided
if [ -z "$KERNEL_ROOT" ]; then
if [ -n "$DEFAULT_KERNEL_ROOT" ]; then
KERNEL_ROOT="$DEFAULT_KERNEL_ROOT"
else
echo "ERROR: --kernel-root is required" >&2
help
exit 1
fi
fi
;;
collate)
while [ $# -gt 0 ]; do
case "$1" in
--kernel-root)
KERNEL_ROOT="$2"
shift 2
;;
--language)
LANGUAGE_FILTER="$2"
shift 2
;;
--output-dir)
OUTPUT_PATH="$2"
shift 2
;;
*)
echo "ERROR: Unknown option: $1" >&2
help
exit 1
;;
esac
done
# Set default kernel root if not provided
if [ -z "$KERNEL_ROOT" ]; then
if [ -n "$DEFAULT_KERNEL_ROOT" ]; then
KERNEL_ROOT="$DEFAULT_KERNEL_ROOT"
else
echo "ERROR: --kernel-root is required" >&2
help
exit 1
fi
fi
;;
collate-by-kernels)
while [ $# -gt 0 ]; do
case "$1" in
--kernel-root)
KERNEL_ROOT="$2"
shift 2
;;
--language)
LANGUAGE_FILTER="$2"
shift 2
;;
--output)
OUTPUT_PATH="$2"
shift 2
;;
*)
echo "ERROR: Unknown option: $1" >&2
help
exit 1
;;
esac
done
# Set default kernel root if not provided
if [ -z "$KERNEL_ROOT" ]; then
if [ -n "$DEFAULT_KERNEL_ROOT" ]; then
KERNEL_ROOT="$DEFAULT_KERNEL_ROOT"
else
echo "ERROR: --kernel-root is required" >&2
help
exit 1
fi
fi
# Set default output path if not provided
if [ -z "$OUTPUT_PATH" ]; then
OUTPUT_PATH="$KERNEL_ROOT/collated_manifests.json"
fi
;;
collate-by-packages)
while [ $# -gt 0 ]; do
case "$1" in
--kernel-root)
KERNEL_ROOT="$2"
shift 2
;;
--language)
LANGUAGE_FILTER="$2"
shift 2
;;
--output)
OUTPUT_PATH="$2"
shift 2
;;
*)
echo "ERROR: Unknown option: $1" >&2
help
exit 1
;;
esac
done
# Set default kernel root if not provided
if [ -z "$KERNEL_ROOT" ]; then
if [ -n "$DEFAULT_KERNEL_ROOT" ]; then
KERNEL_ROOT="$DEFAULT_KERNEL_ROOT"
else
echo "ERROR: --kernel-root is required" >&2
help
exit 1
fi
fi
# Set default output path if not provided
if [ -z "$OUTPUT_PATH" ]; then
OUTPUT_PATH="$KERNEL_ROOT/package_index.json"
fi
;;
*)
echo "ERROR: Unknown command: $cmd" >&2
help
exit 1
;;
esac
}
# Main entry point
function main() {
local cmd=$1
shift
if [ -z "$cmd" ]; then
help
exit 1
fi
parse_args "$cmd" "$@"
case "$cmd" in
index)
index_all_kernels "$KERNEL_ROOT" "$KERNEL_NAME" "$KERNEL_VERSION" "$LANGUAGE_FILTER"
;;
collate)
# Run both collation methods
# Set default output directory if not provided
local output_dir="$OUTPUT_PATH"
if [ -z "$output_dir" ]; then
output_dir="$KERNEL_ROOT"
fi
echo "Running collate-by-kernels..."
collate_manifests "$KERNEL_ROOT" "$output_dir/collated_manifests.json" "$LANGUAGE_FILTER"
local result1=$?
echo ""
echo "Running collate-by-packages..."
collate_package_centric "$KERNEL_ROOT" "$output_dir/package_index.json" "$LANGUAGE_FILTER"
local result2=$?
if [ $result1 -eq 0 ] && [ $result2 -eq 0 ]; then
return 0
else
return 1
fi
;;
collate-by-kernels)
collate_manifests "$KERNEL_ROOT" "$OUTPUT_PATH" "$LANGUAGE_FILTER"
;;
collate-by-packages)
collate_package_centric "$KERNEL_ROOT" "$OUTPUT_PATH" "$LANGUAGE_FILTER"
;;
*)
echo "ERROR: Unknown command: $cmd" >&2
help
exit 1
;;
esac
}
# Run main function
main "$@"