Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/src/doc/en/reference/make_module_list.sh
4112 views
1
#!/bin/bash
2
3
# This script traverses files with .py and .pyx suffixes in src/<directory>
4
# tree of the Sage library and store the module names sorted by their path
5
# and titles.
6
#
7
# EXAMPLE:
8
#
9
# $ src/doc/en/reference/make_module_list.sh sage/combinat
10
11
if [ $# -ne 1 ]; then
12
echo "Usage: $0 <directory>"
13
echo "Example: $0 sage/combinat"
14
exit 1
15
fi
16
17
module_dir="$1"
18
input_dir="$SAGE_ROOT/src/$1"
19
20
if [ ! -d "$input_dir" ]; then
21
echo "Error: Directory '$input_dir' not found."
22
exit 1
23
fi
24
25
tmpfile=$(mktemp)
26
27
cd "$input_dir"
28
29
# Find all .py and .pyx files inside the given directory, excluding __init__.py
30
find . -type f \( -name "*.py" -o -name "*.pyx" \) ! -name "__init__.py" | while read -r file; do
31
# Extract first line after the first triple quotes
32
title=$(awk 'BEGIN {found=0}
33
/^[rR]?"""/ {if (found == 0) {found=1; next}}
34
found && NF {print; exit}' "$file")
35
36
# Format module name: Remove .py or .pyx, replace directory prefix
37
mod_name=$(echo "$file" | sed -E "s/\.(pyx|py)$//; s|^\./| $module_dir/|")
38
path_name=$(dirname "$file")
39
40
# Store sorting path/title and module name in temporary file
41
printf "%s\t%s\n" "$path_name,$title" "$mod_name" >> "$tmpfile"
42
done
43
44
# Sort and output module names to /tmp/module_list.rst
45
LC_ALL=C sort "$tmpfile" | cut -f2 > /tmp/module_list
46
47
rm -f "$tmpfile"
48
49
echo "Sorted module list saved to /tmp/module_list"
50
51