]> git.eshelyaron.com Git - emacs.git/commitdiff
Add admin/emacs-shell-lib for shared bash code
authorStefan Kangas <stefankangas@gmail.com>
Wed, 19 Oct 2022 11:26:54 +0000 (13:26 +0200)
committerStefan Kangas <stefankangas@gmail.com>
Wed, 19 Oct 2022 11:26:54 +0000 (13:26 +0200)
* admin/emacs-shell-lib: New file for shared bash code.
* admin/automerge:
* admin/diff-tar-files:
* admin/emacs-shell-lib:
* admin/make-manuals:
* admin/update_autogen:
* admin/upload-manuals: Simplify and improve using above new library.

admin/automerge
admin/diff-tar-files
admin/emacs-shell-lib [new file with mode: 0644]
admin/make-manuals
admin/update_autogen
admin/upload-manuals

index c7c17dfb5ecc6a4cced93fd53266c844c974fc7a..d2c92948e17bfa23d7c1914cd54c0a5f650a6fc7 100755 (executable)
 ## it with the -d option in the repository directory, in case a pull
 ## updates this script while it is working.
 
-set -o nounset
-
-die ()                 # write error to stderr and exit
-{
-    [ $# -gt 0 ] && echo "$PN: $*" >&2
-    exit 1
-}
-
-PN=${0##*/}                     # basename of script
-PD=${0%/*}
-
-[ "$PD" = "$0" ] && PD=.        # if PATH includes PWD
+source "${0%/*}/emacs-shell-lib"
 
 usage ()
 {
@@ -129,13 +118,7 @@ OPTIND=1
 [ "$test" ] && build=1
 
 
-if [ -x "$(command -v mktemp)" ]; then
-    tempfile=$(mktemp "/tmp/$PN.XXXXXXXXXX")
-else
-    tempfile=/tmp/$PN.$$
-fi
-
-trap 'rm -f $tempfile 2> /dev/null' EXIT
+tempfile="$(emacs_mktemp)"
 
 
 [ -e Makefile ] && [ "$build" ] && {
@@ -263,5 +246,3 @@ git push || die "push error"
 
 
 exit 0
-
-### automerge ends here
index 6ab39eab2f5a21a25353386aa6829f17afec61c7..869c9421502166687e085a7944f41915b921ca2b 100755 (executable)
@@ -1,4 +1,4 @@
-#! /bin/sh
+#!/bin/bash
 
 # Copyright (C) 2001-2022 Free Software Foundation, Inc.
 
@@ -17,6 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
 
+source "${0%/*}/emacs-shell-lib"
 
 if [ $# != 2 ]; then
     cat <<EOF
@@ -31,9 +32,8 @@ fi
 old_tar=$1
 new_tar=$2
 
-old_tmp=/tmp/old.$$
-new_tmp=/tmp/new.$$
-trap "rm -f $old_tmp $new_tmp; exit 1" 1 2 15
+old_tmp="$(emacs_mktemp ${PN}-old)"
+new_tmp="$(emacs_mktemp ${PN}-new)"
 
 tar tf "$old_tar" | sed -e 's,^[^/]*,,' | sort > $old_tmp
 tar tf "$new_tar" | sed -e 's,^[^/]*,,' | sort > $new_tmp
diff --git a/admin/emacs-shell-lib b/admin/emacs-shell-lib
new file mode 100644 (file)
index 0000000..750f81e
--- /dev/null
@@ -0,0 +1,87 @@
+#!/bin/bash
+### emacs-shell-lib - shared code for Emacs shell scripts
+
+## Copyright (C) 2022 Free Software Foundation, Inc.
+
+## Author: Stefan Kangas <stefankangas@gmail.com>
+
+## This file is part of GNU Emacs.
+
+## GNU Emacs is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+
+## GNU Emacs is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+
+## You should have received a copy of the GNU General Public License
+## along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+### Code:
+
+# Set an explicit umask.
+umask 077
+
+# Treat unset variables as an error.
+set -o nounset
+
+# Exit immediately on error.
+set -o errexit
+
+# Avoid non-standard command output from non-C locales.
+unset LANG LC_ALL LC_MESSAGES
+
+PN=${0##*/}                     # basename of script
+PD=${0%/*}                      # script directory
+
+[ "$PD" = "$0" ] && PD=.        # if PATH includes PWD
+
+die ()                 # write error to stderr and exit
+{
+    [ $# -gt 0 ] && echo "$PN: $@" >&2
+    exit 1
+}
+
+emacs_tempfiles=()
+
+emacs_tempfiles_cleanup ()
+{
+    for file in ${emacs_tempfiles[@]}; do
+        rm -f "${file}" 2> /dev/null
+    done
+}
+
+trap '
+  ret=$?
+  emacs_tempfiles_cleanup
+  exit $ret
+' EXIT
+
+emacs_mktemp ()
+{
+    local readonly file="${1-}"
+    local tempfile
+    local prefix
+
+    if [ -z "$file" ]; then
+        prefix="$PN"
+    else
+        prefix="$1"
+    fi
+
+    if [ -x "$(command -v mktemp)" ]; then
+        tempfile=$(mktemp "${TMPDIR-/tmp}/${prefix}.XXXXXXXXXX")
+    else
+        tempfile="${TMPDIR-/tmp}/${prefix}.$RANDOM$$"
+        (umask 077 && touch "$tempfile")
+    fi
+
+    [ -z "${tempfile}" ] && die "Creating temporary file failed"
+
+    emacs_tempfiles+=("${tempfile}")
+
+    echo "$tempfile"
+}
index cb0c00a423f7e6b60131a1c6dcfce85bf329412f..a252bf20f1e65b55d4a76a93a7738795d47b83b4 100755 (executable)
 
 ### Code:
 
-set -o nounset
-
-die ()                          # write error to stderr and exit
-{
-    [ $# -gt 0 ] && echo "$PN: $@" >&2
-    exit 1
-}
-
-PN=${0##*/}                     # basename of script
+source "${0%/*}/emacs-shell-lib"
 
 usage ()
 {
@@ -96,8 +88,7 @@ OPTIND=1
 [ -e admin/admin.el ] || die "admin/admin.el not found"
 
 
-tempfile=/tmp/$PN.$$
-trap "rm -f $tempfile 2> /dev/null" EXIT
+tempfile="$(emacs_mktemp)"
 
 
 [ "$continue" ] || rm -rf $outdir
index d1f49d9f25ee46ca32a6219544d6e4b522282ea0..55e11be95c711f4b0d85ba9cd62da6130fbde8b3 100755 (executable)
 
 ### Code:
 
-set -o nounset
-
-die ()                 # write error to stderr and exit
-{
-    [ $# -gt 0 ] && echo "$PN: $@" >&2
-    exit 1
-}
-
-PN=${0##*/}                     # basename of script
-PD=${0%/*}
-
-[ "$PD" = "$0" ] && PD=.        # if PATH includes PWD
+source "${0%/*}/emacs-shell-lib"
 
 ## This should be the admin directory.
 cd $PD || exit
@@ -102,10 +91,7 @@ done
 
 [ "$basegen" ] || die "internal error"
 
-tempfile=/tmp/$PN.$$
-
-trap 'rm -f $tempfile 2> /dev/null' EXIT
-
+tempfile="$(emacs_mktemp)"
 
 while getopts ":hcfqA:CL" option ; do
     case $option in
@@ -312,5 +298,3 @@ commit "loaddefs" $modified || die "commit error"
 
 
 exit 0
-
-### update_autogen ends here
index 50336ee64c086d72685ad1eb8db6dbe93c3ace06..04f7c3acc7286d442868bafa863af15182f10c61 100755 (executable)
 
 ### Code:
 
-set -o nounset
-
-die ()                          # write error to stderr and exit
-{
-    [ $# -gt 0 ] && echo "$PN: $@" >&2
-    exit 1
-}
-
-PN=${0##*/}                     # basename of script
+source "${0%/*}/emacs-shell-lib"
 
 usage ()
 {