testsAndMisc/linux_configuration/scripts/periodic_background/hosts/generate_hosts_file.sh
Krzysztof kuhy Rudnicki 9e66638fda fix: sync test paths, drop stale assertions, fix coverage gap
- linux_configuration/tests: update script paths after periodic_background/
  reorganisation (hosts_file_monitor, makepkg_capped, music_parallelism,
  shutdown_timer_monitor, usage_monitoring_installer_efficiency)

- test_i3blocks_efficiency.sh: remove checks for HEARTBEAT_INTERVAL_S and
  WARP_POLL_INTERVAL_S constants that no longer exist

- test_pacman_wrapper_security.sh: remove tests 20-21 (builtin time helpers /
  external date calls) that are no longer applicable; update path

- generate_hosts_file.sh: add sed unblock rules for delio.com.pl and
  loverslab.com to stay consistent with install.sh whitelist

- steam_backlog_enforcer/scanning.py: remove unplayable_reason arg from
  logger.info call (too many format args); drop matching test assertion

- steam_backlog_enforcer/tests/test_protondb.py: add
  test_unplayable_reason_no_trending_tier to restore 100% branch coverage
  on protondb.py line 97 (was previously covered indirectly)
2026-05-16 15:46:02 +02:00

112 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
# ============================================================
# generate_hosts_file.sh
#
# Generates a full hosts file (StevenBlack base + custom entries
# from install.sh) to a path of your choice, without touching the
# live /etc/hosts or running any privileged operations.
#
# Used by:
# - phone_focus_mode/deploy.sh (to create a canonical hosts
# file to push to a rooted Android device).
#
# Keeps the custom-entries heredoc in install.sh as the single
# source of truth: this script extracts it via the same sed
# pattern install.sh uses for its protection check.
#
# Usage:
# generate_hosts_file.sh <output_path>
# generate_hosts_file.sh - # stdout
# HOSTS_CACHE=/tmp/sb.cache generate_hosts_file.sh out
# ============================================================
set -euo pipefail
OUT="${1:-}"
if [[ -z $OUT ]]; then
echo "Usage: $0 <output_path>|-" >&2
exit 1
fi
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
INSTALL_SH="$SCRIPT_DIR/install.sh"
URL="https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn-social/hosts"
# Default cache location: same as install.sh so both reuse the same file.
CACHE="${HOSTS_CACHE:-/etc/hosts.stevenblack}"
# Fall back to a per-user cache if /etc/hosts.stevenblack is not readable,
# or if we don't have write access (install.sh runs as root; this script
# may not). Avoid interactive `mv` prompts by checking writability up front.
if [[ ! -r $CACHE ]] || [[ -e $CACHE && ! -w $CACHE ]] || [[ ! -w $(dirname "$CACHE") ]]; then
CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/phone_focus_mode/hosts.stevenblack"
mkdir -p "$(dirname "$CACHE")"
fi
if [[ ! -f $INSTALL_SH ]]; then
echo "ERROR: cannot find install.sh at $INSTALL_SH" >&2
exit 1
fi
# ---- Fetch or reuse cache ----
need_download=0
if [[ ! -f $CACHE ]]; then
need_download=1
else
# Refresh if older than 7 days
if [[ -n $(find "$CACHE" -mtime +7 -print 2>/dev/null) ]]; then
need_download=1
fi
fi
if [[ $need_download -eq 1 ]]; then
tmp_dl="$(mktemp)"
if curl -LfsS --max-time 30 "$URL" -o "$tmp_dl"; then
mv -f "$tmp_dl" "$CACHE"
else
rm -f "$tmp_dl"
if [[ ! -f $CACHE ]]; then
echo "ERROR: failed to download $URL and no cache present" >&2
exit 1
fi
echo "WARNING: download failed, using stale cache at $CACHE" >&2
fi
fi
# ---- Build output ----
TMP="$(mktemp)"
trap 'rm -f "$TMP"' EXIT
cp "$CACHE" "$TMP"
# Apply the same unblocks install.sh does so generated file matches PC /etc/hosts.
sed -i 's/^0\.0\.0\.0 4chan\.com/#0.0.0.0 4chan.com/' "$TMP"
sed -i 's/^0\.0\.0\.0 www\.4chan\.com/#0.0.0.0 www.4chan.com/' "$TMP"
sed -i 's/^0\.0\.0\.0 4chan\.org/#0.0.0.0 4chan.org/' "$TMP"
sed -i 's/^0\.0\.0\.0 boards\.4chan\.org/#0.0.0.0 boards.4chan.org/' "$TMP"
sed -i 's/^0\.0\.0\.0 sys\.4chan\.org/#0.0.0.0 sys.4chan.org/' "$TMP"
sed -i 's/^0\.0\.0\.0 www\.4chan\.org/#0.0.0.0 www.4chan.org/' "$TMP"
sed -i 's/^0\.0\.0\.0 www\.facebook\.com/#0.0.0.0 www.facebook.com/' "$TMP"
sed -i 's/^0\.0\.0\.0 messenger\.com/#0.0.0.0 messenger.com/' "$TMP"
sed -i 's/^0\.0\.0\.0 delio\.com.pl/#0.0.0.0 delio.com.pl/' "$TMP"
sed -i -E 's/^(0\.0\.0\.0[[:space:]]+[a-zA-Z0-9._-]*\.?linkedin\.com)/#\1/' "$TMP"
sed -i -E 's/^(0\.0\.0\.0[[:space:]]+[a-zA-Z0-9._-]*\.?licdn\.com)/#\1/' "$TMP"
sed -i -E 's/^(0\.0\.0\.0[[:space:]]+[a-zA-Z0-9._-]*\.?loverslab\.com)/#\1/' "$TMP"
# Extract the custom-entries block from install.sh (between the
# "# Custom blocking entries" comment and the heredoc EOF marker).
# This is the same pattern install.sh uses for its protection check,
# so the two files stay in sync automatically.
{
echo ""
sed -n '/^# Custom blocking entries$/,/^EOF$/p' "$INSTALL_SH" |
sed '$d' # drop the trailing EOF line
} >>"$TMP"
if [[ $OUT == "-" ]]; then
cat "$TMP"
else
mkdir -p "$(dirname "$OUT")"
cp "$TMP" "$OUT"
chmod 644 "$OUT" 2>/dev/null || true
fi