1
0

scripts: refine

This commit is contained in:
Konstantin Demin 2024-09-30 20:54:31 +03:00
parent 86af6345e5
commit 47515839a2
Signed by: krd
GPG Key ID: 4D56F87A8BA65FD0
7 changed files with 68 additions and 70 deletions

View File

@ -23,24 +23,28 @@ find /var/cache/debconf/ ! -type d -wholename '/var/cache/debconf/*-old' -delete
__t=$(mktemp) ; : "${__t:?}" __t=$(mktemp) ; : "${__t:?}"
debconf_trim_i18n() { debconf_trim_i18n() {
mawk 'BEGIN { m = 0 } mawk 'BEGIN { m = 0; }
$0 == "" { print } $0 == "" { print; }
/^[^[:space:]]/ { /^[^[:space:]]/ {
if ($1 ~ "\.[Uu][Tt][Ff]-?8:") { m = 1; next; } if ($1 ~ "\.[Uu][Tt][Ff]-?8:") {
m = 0; print $0; m = 1;
} next;
/^[[:space:]]/ { }
if (m == 1) next; m = 0;
print $0; print $0;
}' < "$1" > "${__t}" }
cat < "${__t}" > "$1" /^[[:space:]]/ {
if (m == 1) next;
print $0;
}' < "$1" > "${__t}"
cat < "${__t}" > "$1"
} }
debconf_trim_i18n /var/cache/debconf/templates.dat debconf_trim_i18n /var/cache/debconf/templates.dat
while read -r tmpl ; do while read -r tmpl ; do
[ -n "${tmpl}" ] || continue [ -n "${tmpl}" ] || continue
[ -s "${tmpl}" ] || continue [ -s "${tmpl}" ] || continue
debconf_trim_i18n "${tmpl}" debconf_trim_i18n "${tmpl}"
done <<EOF done <<EOF
$(find "${DPKG_ADMINDIR}/info/" -type f -name '*.templates' | sort -V) $(find "${DPKG_ADMINDIR}/info/" -type f -name '*.templates' | sort -V)
EOF EOF

View File

@ -37,16 +37,15 @@ normalize_list() {
[ -n "$1" ] || return 0 [ -n "$1" ] || return 0
printf '%s' "$1" \ printf '%s' "$1" \
| tr -s '[:space:]' ' ' \ | sed -zE 's/[[:space:]]+/ /g;s/^ //;s/ $//'
| sed -zE 's/^ //;s/ $//'
} }
sort_dedup_list() { sort_dedup_list() {
[ -n "$1" ] || return 0 [ -n "$1" ] || return 0
printf '%s' "$1" \ printf '%s' "$1" \
| tr -s '[:space:]' '\n' | sort -uV | paste -sd ' ' \ | tr -s '[:space:]' '\n' | sort -uV \
| sed -zE 's/^\s+//;s/\s+$//' | sed -zE 's/[[:space:]]+/ /g;s/^ //;s/ $//'
} }
pkgs=$(sort_dedup_list "${pkgs}") pkgs=$(sort_dedup_list "${pkgs}")

View File

@ -22,8 +22,9 @@ _apt_update() {
} }
_dpkg_avail_hack() { _dpkg_avail_hack() {
: "${DPKG_ADMINDIR:=/var/lib/dpkg}"
VERSION_CODENAME=$(. /etc/os-release ; printf '%s' "${VERSION_CODENAME}") || : VERSION_CODENAME=$(. /etc/os-release ; printf '%s' "${VERSION_CODENAME}") || :
f="${DPKG_ADMINDIR:-/var/lib/dpkg}/available" f="${DPKG_ADMINDIR}/available"
# if ${VERSION_CODENAME} is empty then we're on Debian sid or so :) # if ${VERSION_CODENAME} is empty then we're on Debian sid or so :)
case "${VERSION_CODENAME}" in case "${VERSION_CODENAME}" in
stretch | buster | bionic | focal ) stretch | buster | bionic | focal )
@ -31,7 +32,7 @@ _dpkg_avail_hack() {
if [ -s "$f" ] ; then if [ -s "$f" ] ; then
return return
fi fi
/usr/lib/dpkg/methods/apt/update "${DPKG_ADMINDIR:-/var/lib/dpkg}" apt apt /usr/lib/dpkg/methods/apt/update "${DPKG_ADMINDIR}" apt apt
;; ;;
* ) * )
touch "$f" touch "$f"

View File

@ -1,19 +1,17 @@
#!/bin/sh #!/bin/sh
set -f set -f
sed -znE '/^([^=]+)=.*$/s,,\1,p' /proc/self/environ \ sed -znE '/^([^=]+)=.*$/s,,\1,p' /proc/$$/environ \
| sed -zE \ | sed -zE \
-e '/^_$/d;/^ENVSUBST_/d;' \ -e '/^_$/d;/^ENVSUBST_/d;' \
-e '/^__IEP_/d;/^IEP_$/d' \ -e '/^__IEP_/d;/^IEP_$/d' \
| { | {
if [ -n "${ENVSUBST_EXCLUDE_REGEX:-}" ] ; then if [ -n "${ENVSUBST_EXCLUDE_REGEX:-}" ] ; then
grep -zEv -e "${ENVSUBST_EXCLUDE_REGEX}" grep -zEv -e "${ENVSUBST_EXCLUDE_REGEX}"
elif [ -n "${ENVSUBST_INCLUDE_REGEX:-}" ] ; then
grep -zE -e "${ENVSUBST_INCLUDE_REGEX}"
else else
if [ -n "${ENVSUBST_INCLUDE_REGEX:-}" ] ; then cat
grep -zE -e "${ENVSUBST_INCLUDE_REGEX}"
else
cat
fi
fi fi
} \ } \
| sort -zV \ | sort -zV \

View File

@ -20,11 +20,16 @@ w_cleanup() {
} }
bundle_offsets() { bundle_offsets() {
awk ' mawk 'BEGIN { OFS = ","; i_begin = 0; }
BEGIN { OFS = "," ; i_begin = 0 ; } $0 == "-----BEGIN CERTIFICATE-----" {
$0 == "-----BEGIN CERTIFICATE-----" { i_begin = NR ; } i_begin = NR;
$0 == "-----END CERTIFICATE-----" { if (i_begin > 0) { print i_begin,NR ; i_begin = 0 ; } } }
' "$1" $0 == "-----END CERTIFICATE-----" {
if (i_begin > 0) {
print i_begin, NR;
i_begin = 0;
}
}' "$1"
} }
bundle_fingerprints() { bundle_fingerprints() {

View File

@ -1,7 +1,9 @@
#!/bin/sh #!/bin/sh
set -f set -f
for i ; do for i ; do
find "$i/" -name __pycache__ -exec rm -rf {} + [ -n "$i" ] || continue
find "$i/" ! -type d -name '*.py[co]' -exec rm -f {} + [ -d "$i" ] || continue
find "$i/" -name __pycache__ -exec rm -rf {} +
find "$i/" ! -type d -name '*.py[co]' -exec rm -f {} +
done done
exit 0 exit 0

View File

@ -1,52 +1,41 @@
#!/bin/sh #!/bin/sh
set -f set -f
COMPRESS_MIN_RATIO=90
if command -V gzip >/dev/null ; then has_gzip=1 ; fi if command -V gzip >/dev/null ; then has_gzip=1 ; fi
if command -V brotli >/dev/null ; then has_brotli=1 ; fi if command -V brotli >/dev/null ; then has_brotli=1 ; fi
if command -V zstd >/dev/null ; then has_zstd=1 ; fi if command -V zstd >/dev/null ; then has_zstd=1 ; fi
do_gzip() { [ -s "$1.gz" ] || gzip -1kf "$1" ; comp_fixup "$1" "$1.gz" ; } do_gzip() { [ -s "$1.gz" ] || gzip -1kf "$1" || return ; comp_fixup "$1" "$1.gz" || rm -f "$1.gz" ; }
do_brotli() { [ -s "$1.br" ] || brotli -1kf "$1" ; comp_fixup "$1" "$1.br" ; } do_brotli() { [ -s "$1.br" ] || brotli -1kf "$1" || return ; comp_fixup "$1" "$1.br" || rm -f "$1.br" ; }
do_zstd() { [ -s "$1.zst" ] || zstd -q1kf "$1" ; comp_fixup "$1" "$1.zst" ; } do_zstd() { [ -s "$1.zst" ] || zstd -q1kf "$1" || return ; comp_fixup "$1" "$1.zst" || rm -f "$1.zst" ; }
float_div() {
mawk -v "a=$1" -v "b=$2" 'BEGIN{print a/b;exit;}' </dev/null
}
comp_fixup() { comp_fixup() {
size1=$(env stat -c '%s' "$1") || return [ -f "$1" ] || return 1
size1=$(env stat -Lc '%s' "$1") || return 1
[ -n "${size1}" ] || return 1
[ "${size1}" != 0 ] || return 1
[ -f "$2" ] || return [ -f "$2" ] || return 1
[ -s "$2" ] || { rm -f "$2" ; return ; } size2=$(env stat -c '%s' "$2") || return 1
size2=$(env stat -c '%s' "$2") || return [ -n "${size2}" ] || return 1
[ "${size2}" != 0 ] || return 1
pow1=${#size1} ; pow2=${#size2} ratio=$(float_div "${size2}" "${size1}") || return 1
case "${ratio}" in
[0-9]*e-[0-9]* )
## doubtful but okay (c) Oleg Tinkov
;;
0.[0-8]* | 0.90* )
## compression ratio below 90% is fine
;;
* ) return 1 ;;
esac
## if size2 is _longer_ than size1 - compression did something wrong (file is bigger) return 0
if [ ${pow2} -gt ${pow1} ] ; then
rm -f "$2" ; return
fi
## if size1 is _longer_ size2 more than 2 digits - compression was done very successful
## doubtful but okay (c) Oleg Tinkov
if [ $(( pow1 - pow2 )) -gt 2 ] ; then
return
fi
## math hack!
if [ ${pow1} -gt 7 ] ; then
skew=$(( pow1 - 4 ))
pow1=$(( pow1 - skew ))
pow2=$(( pow2 - skew ))
size1=$(printf '%s' "${size1}" | cut -c 1-${pow1})
size2=$(printf '%s' "${size2}" | cut -c 1-${pow2})
fi
ratio=$(( (100 * size2) / size1 ))
if [ ${ratio} -ge ${COMPRESS_MIN_RATIO} ] ; then
rm -f "$2"
else
## seems to be excessive
: touch -r "$1" -m "$2"
fi
} }
for i ; do for i ; do