CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/extensions/detect-unused-extensions.sh
Views: 3956
1
## Configuration
2
export LOG_ALL_HOOK_TRACES=no # Should we log all hook function traces to stdout? (no, or level: wrn info)
3
4
## Hooks
5
6
# A honeypot wishful hooking. To make sure the whole thing works.
7
# This is exactly the kind of hooking this extension is meant to detect.
8
# This will never run, and should be detected below, but is handled specially and ignored.
9
# Note: this will never run, since we (hopefully) don't have a hook_point called 'wishful_hooking_example'.
10
function wishful_hooking_example__this_will_never_run() {
11
echo "WISHFUL HOOKING -- this will never run. I promise."
12
}
13
14
# Run super late, hopefully at the last possible moment.
15
function extension_metadata_ready__999_detect_wishful_hooking() {
16
display_alert "Checking extensions and hooks for uncalled hook points"
17
declare -i found_honeypot_function=0
18
19
# Loop over the defined functions' keys. Find the info about the call. If not found, warn the user.
20
# shellcheck disable=SC2154 # hook-exported variable
21
for one_defined_function in ${!defined_hook_point_functions[*]}; do
22
local source_info defined_info line_info
23
defined_info="${defined_hook_point_functions["${one_defined_function}"]}"
24
source_info="${hook_point_function_trace_sources["${one_defined_function}"]}"
25
line_info="${hook_point_function_trace_lines["${one_defined_function}"]}"
26
stack="$(get_extension_hook_stracktrace "${source_info}" "${line_info}")"
27
if [[ "$source_info" != "" ]]; then
28
# log to debug log. it's reassuring.
29
echo "\$\$\$ Hook function stacktrace for '${one_defined_function}': '${stack}' (${defined_info})" >>"${EXTENSION_MANAGER_LOG_FILE}"
30
if [[ "${LOG_ALL_HOOK_TRACES}" != "no" ]]; then
31
display_alert "Hook function stacktrace for '${one_defined_function}'" "${stack}" "${LOG_ALL_HOOK_TRACES}"
32
fi
33
continue # found a caller, move on.
34
fi
35
36
# special handling for the honeypot function. it is supposed to be always detected as uncalled.
37
if [[ "${one_defined_function}" == "wishful_hooking_example__this_will_never_run" ]]; then
38
# we expect this wishful hooking, it is done on purpose below, to make sure this code works.
39
found_honeypot_function=1
40
else
41
# unexpected wishful hooking. Log and wrn the user.
42
echo "\$\$\$ Wishful hooking detected" "Function '${one_defined_function}' is defined (${defined_info}) but never called by the build." >>"${EXTENSION_MANAGER_LOG_FILE}"
43
display_alert "Wishful hooking detected" "Function '${one_defined_function}' is defined (${defined_info}) but never called by the build." "wrn"
44
fi
45
done
46
47
if [[ $found_honeypot_function -lt 1 ]]; then
48
display_alert "Wishful hook DETECTION FAILED" "detect-wishful-hooking is not working. Good chance the environment vars are corrupted. Avoid child shells. Sorry." "wrn" | tee -a "${EXTENSION_MANAGER_LOG_FILE}"
49
fi
50
}
51
52