Merge pull request #56 from kisslinux/pretty_print

Pretty print
This commit is contained in:
black 2019-09-21 21:36:10 +03:00 committed by GitHub
commit 78568c49e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

146
kiss
View File

@ -12,15 +12,26 @@
# #
# Dylan Araps. # Dylan Araps.
die() {
# Print a message and exit with '1' (error).
printf '\033[1;31m!>\033[m %s.\n' "$@" >&2
exit 1
}
log() { log() {
# Print a message prettily. # Print a message prettily.
printf '\033[1;32m->\033[m %s.\n' "$@" #
# This function uses the literal escape character (Ctrl+V+Escape) as
# a simple way of *safely* bypassing the escape sequence restrictions
# on 'printf %s'. Cheeky, I know.
#
# '\033[1;32m' Set text to color '2' and make it bold.
# '\033[m': Reset text formatting.
# '${3:-->}': If the 3rd argument is missing, set prefix to '->'.
# '${2:+[1;33m}': If the 2nd argument exists, set the text style of '$1'.
# '${2:+[m}': If the 2nd argument exists, reset text formatting.
printf '\033[1;33m%s \033[m%s\033[m %s\n' \
"${3:-->}" "${2:+}$1${2:+}" "$2"
}
die() {
# Print a message and exit with '1' (error).
log "$1" "$2" "!>" >&2
exit 1
} }
contains() { contains() {
@ -31,14 +42,14 @@ contains() {
pkg_lint() { pkg_lint() {
# Check that each mandatory file in the package entry exists. # Check that each mandatory file in the package entry exists.
log "[$1] Checking repository files" log "$1" "Checking repository files"
repo_dir=$(pkg_find "$1") repo_dir=$(pkg_find "$1")
cd "$repo_dir" || die "'$repo_dir' not accessible" cd "$repo_dir" || die "'$repo_dir' not accessible"
[ -f sources ] || die "[$1] Sources file not found" [ -f sources ] || die "$1" "Sources file not found"
[ -x build ] || die "[$1] Build file not found or not executable" [ -x build ] || die "$1" "Build file not found or not executable"
[ -s version ] || die "[$1] Version file not found or empty" [ -s version ] || die "$1" "Version file not found or empty"
read -r _ release < version read -r _ release < version
[ "$release" ] || die "Release field not found in version file" [ "$release" ] || die "Release field not found in version file"
@ -100,7 +111,7 @@ pkg_list() {
pkg_sources() { pkg_sources() {
# Download any remote package sources. The existence of local # Download any remote package sources. The existence of local
# files is also checked. # files is also checked.
log "[$1] Downloading sources" log "$1" "Downloading sources"
# Store each downloaded source in a directory named after the # Store each downloaded source in a directory named after the
# package it belongs to. This avoid conflicts between two packages # package it belongs to. This avoid conflicts between two packages
@ -114,21 +125,21 @@ pkg_sources() {
# Remote source. # Remote source.
*://*) *://*)
[ -f "${src##*/}" ] && { [ -f "${src##*/}" ] && {
log "[$1] Found cached source '${src##*/}'" log "$1" "Found cached source '${src##*/}'"
continue continue
} }
wget "$src" || { wget "$src" || {
rm -f "${src##*/}" rm -f "${src##*/}"
die "[$1] Failed to download $src" die "$1" "Failed to download $src"
} }
;; ;;
# Local source. # Local source.
*) *)
[ -f "$repo_dir/$src" ] || die "[$1] No local file '$src'" [ -f "$repo_dir/$src" ] || die "$1" "No local file '$src'"
log "[$1] Found local file '$src'" log "$1" "Found local file '$src'"
;; ;;
esac esac
done < "$repo_dir/sources" done < "$repo_dir/sources"
@ -137,7 +148,7 @@ pkg_sources() {
pkg_extract() { pkg_extract() {
# Extract all source archives to the build directory and copy over # Extract all source archives to the build directory and copy over
# any local repository files. # any local repository files.
log "[$1] Extracting sources" log "$1" "Extracting sources"
repo_dir=$(pkg_find "$1") repo_dir=$(pkg_find "$1")
@ -150,7 +161,7 @@ pkg_extract() {
# allows for manual extraction. # allows for manual extraction.
*://*.tar*|*://*.tgz) *://*.tar*|*://*.tgz)
tar xf "$src_dir/$1/${src##*/}" --strip-components 1 \ tar xf "$src_dir/$1/${src##*/}" --strip-components 1 \
|| die "[$1] Couldn't extract ${src##*/}" || die "$1" "Couldn't extract ${src##*/}"
;; ;;
*) *)
@ -163,7 +174,7 @@ pkg_extract() {
cp -f "$src_dir/$1/${src##*/}" . cp -f "$src_dir/$1/${src##*/}" .
else else
die "[$1] Local file $src not found" die "$1" "Local file $src not found"
fi fi
;; ;;
esac esac
@ -201,7 +212,7 @@ pkg_strip() {
# Package has stripping disabled, stop here. # Package has stripping disabled, stop here.
[ -f "$(pkg_find "$1")/nostrip" ] && return [ -f "$(pkg_find "$1")/nostrip" ] && return
log "[$1] Stripping binaries and libraries" log "$1" "Stripping binaries and libraries"
# Strip only files matching the below ELF types. # Strip only files matching the below ELF types.
find "$pkg_dir/$1" -type f | while read -r file; do find "$pkg_dir/$1" -type f | while read -r file; do
@ -223,7 +234,7 @@ pkg_fixdeps() {
# each binary and library with 'ldd'. This catches any extra # each binary and library with 'ldd'. This catches any extra
# libraries and or dependencies pulled in by the package's # libraries and or dependencies pulled in by the package's
# build suite. # build suite.
log "[$1] Checking for missing dependencies" log "$1" "Checking for missing dependencies"
# Go to the directory containing the built package to # Go to the directory containing the built package to
# simplify path building. # simplify path building.
@ -284,7 +295,7 @@ pkg_manifest() (
# Generate the package's manifest file. This is a list of each file # Generate the package's manifest file. This is a list of each file
# and directory inside the package. The file is used when uninstalling # and directory inside the package. The file is used when uninstalling
# packages, checking for package conflicts and for general debugging. # packages, checking for package conflicts and for general debugging.
log "[$1] Generating manifest" log "$1" "Generating manifest"
# This funcion runs as a sub-shell to avoid having to 'cd' back to the # This funcion runs as a sub-shell to avoid having to 'cd' back to the
# prior directory before being able to continue. # prior directory before being able to continue.
@ -301,16 +312,16 @@ pkg_manifest() (
pkg_tar() { pkg_tar() {
# Create a tar-ball from the built package's files. # Create a tar-ball from the built package's files.
# This tar-ball also contains the package's database entry. # This tar-ball also contains the package's database entry.
log "[$1] Creating tar-ball" log "$1" "Creating tar-ball"
# Read the version information to name the package. # Read the version information to name the package.
read -r version release < "$(pkg_find "$1")/version" read -r version release < "$(pkg_find "$1")/version"
# Create a tar-ball from the contents of the built package. # Create a tar-ball from the contents of the built package.
tar zpcf "$bin_dir/$1#$version-$release.tar.gz" -C "$pkg_dir/$1" . || tar zpcf "$bin_dir/$1#$version-$release.tar.gz" -C "$pkg_dir/$1" . ||
die "[$1] Failed to create tar-ball" die "$1" "Failed to create tar-ball"
log "[$1] Successfully created tar-ball" log "$1" "Successfully created tar-ball"
} }
pkg_build() { pkg_build() {
@ -375,7 +386,7 @@ pkg_build() {
# This calls 'args' to inherit a root check and call # This calls 'args' to inherit a root check and call
# to 'sudo' to elevate permissions. # to 'sudo' to elevate permissions.
[ -f "$bin_dir/$pkg#$version-$release.tar.gz" ] && { [ -f "$bin_dir/$pkg#$version-$release.tar.gz" ] && {
log "[$pkg] Found pre-built binary, installing" log "$pkg" "Found pre-built binary, installing"
(KISS_FORCE=1 args i "$bin_dir/$pkg#$version-$release.tar.gz") (KISS_FORCE=1 args i "$bin_dir/$pkg#$version-$release.tar.gz")
# Remove the now installed package from the build # Remove the now installed package from the build
@ -391,7 +402,7 @@ pkg_build() {
for pkg; do for pkg; do
# Ensure that checksums exist prior to building the package. # Ensure that checksums exist prior to building the package.
[ -f "$(pkg_find "$pkg")/checksums" ] || { [ -f "$(pkg_find "$pkg")/checksums" ] || {
log "[$pkg] Checksums are missing" log "$pkg" "Checksums are missing"
# Instead of dying above, log it to the terminal. Also define a # Instead of dying above, log it to the terminal. Also define a
# variable so we *can* die after all checksum files have been # variable so we *can* die after all checksum files have been
@ -410,7 +421,7 @@ pkg_build() {
# set. # set.
for pkg; do for pkg; do
pkg_checksums "$pkg" | cmp -s - "$(pkg_find "$pkg")/checksums" || { pkg_checksums "$pkg" | cmp -s - "$(pkg_find "$pkg")/checksums" || {
log "[$pkg] Checksum mismatch" log "$pkg" "Checksum mismatch"
# Instead of dying above, log it to the terminal. Also define a # Instead of dying above, log it to the terminal. Also define a
# variable so we *can* die after all checksum files have been # variable so we *can* die after all checksum files have been
@ -436,13 +447,13 @@ pkg_build() {
cd "$mak_dir/$pkg" cd "$mak_dir/$pkg"
# Call the build script. # Call the build script.
"$repo_dir/build" "$pkg_dir/$pkg" || die "[$pkg] Build failed" "$repo_dir/build" "$pkg_dir/$pkg" || die "$pkg" "Build failed"
# Copy the repository files to the package directory. # Copy the repository files to the package directory.
# This acts as the database entry. # This acts as the database entry.
cp -Rf "$repo_dir" "$pkg_dir/$pkg/$pkg_db/" cp -Rf "$repo_dir" "$pkg_dir/$pkg/$pkg_db/"
log "[$pkg] Successfully built package" log "$pkg" "Successfully built package"
# Create the manifest file early and make it empty. # Create the manifest file early and make it empty.
# This ensure that the manifest is added to the manifest... # This ensure that the manifest is added to the manifest...
@ -457,7 +468,7 @@ pkg_build() {
# Skip this check if this is a package update. # Skip this check if this is a package update.
contains "$explicit" "$pkg" && [ -z "$pkg_update" ] && continue contains "$explicit" "$pkg" && [ -z "$pkg_update" ] && continue
log "[$pkg] Needed as a dependency or has an update, installing" log "$pkg" "Needed as a dependency or has an update, installing"
(KISS_FORCE=1 args i "$pkg") (KISS_FORCE=1 args i "$pkg")
done done
@ -473,8 +484,8 @@ pkg_build() {
# Only ask for confirmation if more than one package needs to be installed. # Only ask for confirmation if more than one package needs to be installed.
[ $# -gt 1 ] && { [ $# -gt 1 ] && {
log "Install built packages? [$*]" \ log "Install built packages? [$*]"
"Press Enter to continue or Ctrl+C to abort here" log "Press Enter to continue or Ctrl+C to abort here"
# POSIX 'read' has none of the "nice" options like '-n', '-p' # POSIX 'read' has none of the "nice" options like '-n', '-p'
# etc etc. This is the most basic usage of 'read'. # etc etc. This is the most basic usage of 'read'.
@ -504,19 +515,19 @@ pkg_checksums() {
# Die here if source for some reason, doesn't exist. # Die here if source for some reason, doesn't exist.
else else
die "[$1] Couldn't find source '$src'" die "$1" "Couldn't find source '$src'"
fi fi
# An easy way to get 'sha256sum' to print with the 'basename' # An easy way to get 'sha256sum' to print with the 'basename'
# of files is to 'cd' to the file's directory beforehand. # of files is to 'cd' to the file's directory beforehand.
(cd "$src_path" && sha256sum "${src##*/}") || (cd "$src_path" && sha256sum "${src##*/}") ||
die "[$1] Failed to generate checksums" die "$1" "Failed to generate checksums"
done < "$repo_dir/sources" done < "$repo_dir/sources"
} }
pkg_conflicts() { pkg_conflicts() {
# Check to see if a package conflicts with another. # Check to see if a package conflicts with another.
log "[$2] Checking for package conflicts" log "$2" "Checking for package conflicts"
set +ef set +ef
@ -547,7 +558,7 @@ pkg_remove() {
# The package is not installed, don't do anything. # The package is not installed, don't do anything.
pkg_list "$1" >/dev/null || { pkg_list "$1" >/dev/null || {
log "[$1] Not installed" log "$1" "Not installed"
return return
} }
@ -565,9 +576,7 @@ pkg_remove() {
# Disable globbing. # Disable globbing.
set -f set -f
[ "$required_by" ] && [ "$required_by" ] && die "$1" "Package is required by ${required_by%, }"
die "[$1] Package is required by ${required_by%, }" \
"[$1] Aborting here..."
# Block being able to abort the script with 'Ctrl+C' during removal. # Block being able to abort the script with 'Ctrl+C' during removal.
# Removes all risk of the user aborting a package removal leaving # Removes all risk of the user aborting a package removal leaving
@ -590,7 +599,7 @@ pkg_remove() {
# we no longer need to block 'Ctrl+C'. # we no longer need to block 'Ctrl+C'.
trap pkg_clean EXIT INT trap pkg_clean EXIT INT
log "[$1] Removed successfully" log "$1" "Removed successfully"
} }
pkg_install() { pkg_install() {
@ -609,8 +618,7 @@ pkg_install() {
tar_name=$1\#$version-$release.tar.gz tar_name=$1\#$version-$release.tar.gz
[ -f "$bin_dir/$tar_name" ] || [ -f "$bin_dir/$tar_name" ] ||
die "Package '$1' has not been built" \ die "Package '$1' has not been built, run 'kiss build $1'"
"Run 'kiss build $1'"
tar_file=$bin_dir/$tar_name tar_file=$bin_dir/$tar_name
fi fi
@ -630,9 +638,9 @@ pkg_install() {
# Extract the tar-ball to catch any errors before installation begins. # Extract the tar-ball to catch any errors before installation begins.
tar pxf "$tar_file" -C "$tar_dir/$pkg_name" || tar pxf "$tar_file" -C "$tar_dir/$pkg_name" ||
die "[$pkg_name] Failed to extract tar-ball" die "$pkg_name" "Failed to extract tar-ball"
log "[$pkg_name] Checking that all dependencies are installed" log "$pkg_name" "Checking that all dependencies are installed"
# Make sure that all run-time dependencies are installed prior to # Make sure that all run-time dependencies are installed prior to
# installing the package. # installing the package.
@ -644,9 +652,9 @@ pkg_install() {
install_dep="$install_dep'$dep', " install_dep="$install_dep'$dep', "
done < "$tar_dir/$pkg_name/$pkg_db/$pkg_name/depends" done < "$tar_dir/$pkg_name/$pkg_db/$pkg_name/depends"
[ "$install_dep" ] && die "[$1] Package requires ${install_dep%, }" [ "$install_dep" ] && die "$1" "Package requires ${install_dep%, }"
log "[$pkg_name] Installing package incrementally" log "$pkg_name" "Installing package incrementally"
# Block being able to abort the script with Ctrl+C during installation. # Block being able to abort the script with Ctrl+C during installation.
# Removes all risk of the user aborting a package installation leaving # Removes all risk of the user aborting a package installation leaving
@ -713,11 +721,11 @@ pkg_install() {
trap pkg_clean EXIT INT trap pkg_clean EXIT INT
if [ -x "$sys_db/$pkg_name/post-install" ]; then if [ -x "$sys_db/$pkg_name/post-install" ]; then
log "[$pkg_name] Running post-install script" log "$pkg_name" "Running post-install script"
"$sys_db/$pkg_name/post-install" ||: "$sys_db/$pkg_name/post-install" ||:
fi fi
log "[$pkg_name] Installed successfully" log "$pkg_name" "Installed successfully"
} }
pkg_updates() { pkg_updates() {
@ -741,24 +749,24 @@ pkg_updates() {
cd "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null ||: cd "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null ||:
[ -d .git ] || { [ -d .git ] || {
log "[$repo] Not a git repository, skipping" log "$repo" "Not a git repository, skipping"
continue continue
} }
[ "$(git remote 2>/dev/null)" ] || { [ "$(git remote 2>/dev/null)" ] || {
log "[$repo] No remote, skipping" log "$repo" "No remote, skipping"
continue continue
} }
contains "$repos" "$PWD" || { contains "$repos" "$PWD" || {
repos="$repos $PWD " repos="$repos $PWD "
log "[$PWD] Updating repository" log "$PWD" "Updating repository"
if [ -w "$PWD" ]; then if [ -w "$PWD" ]; then
git pull git pull
else else
log "[$PWD] Need root to update" log "$PWD" "Need root to update"
sudo git pull sudo git pull
fi fi
} }
@ -786,9 +794,9 @@ pkg_updates() {
# If the package manager has an update, handle it first. # If the package manager has an update, handle it first.
contains "$outdated" kiss && { contains "$outdated" kiss && {
log "Detected package manager update" \ log "Detected package manager update"
"The package manager will be updated first" \ log "The package manager will be updated first"
"Continue?: Press Enter to continue or Ctrl+C to abort here" log "Continue?: Press Enter to continue or Ctrl+C to abort here"
# POSIX 'read' has none of the "nice" options like '-n', '-p' # POSIX 'read' has none of the "nice" options like '-n', '-p'
# etc etc. This is the most basic usage of 'read'. # etc etc. This is the most basic usage of 'read'.
@ -798,8 +806,8 @@ pkg_updates() {
pkg_build kiss pkg_build kiss
args i kiss args i kiss
log "Updated the package manager" \ log "Updated the package manager"
"Re-run 'kiss update' to update your system" log "Re-run 'kiss update' to update your system"
exit 0 exit 0
} }
@ -892,7 +900,7 @@ args() {
for pkg; do for pkg; do
pkg_checksums "$pkg" > "$(pkg_find "$pkg")/checksums" pkg_checksums "$pkg" > "$(pkg_find "$pkg")/checksums"
log "[$pkg] Generated checksums" log "$pkg" "Generated checksums"
done done
;; ;;
@ -927,7 +935,7 @@ args() {
for pkg in $remove_pkgs; do for pkg in $remove_pkgs; do
pkg_list "$pkg" >/dev/null || pkg_list "$pkg" >/dev/null ||
die "[$pkg] Not installed" die "$pkg" "Not installed"
pkg_remove "$pkg" "${KISS_FORCE:-check}" pkg_remove "$pkg" "${KISS_FORCE:-check}"
done done
@ -946,18 +954,18 @@ args() {
;; ;;
v|version|-v|--version) v|version|-v|--version)
printf 'kiss 0.30.2\n' log kiss 0.30.2
;; ;;
h|help|-h|--help|'') h|help|-h|--help|'')
log 'kiss [b|c|i|l|r|s|u] [pkg] [pkg] [pkg]' \ log 'kiss [b|c|i|l|r|s|u] [pkg] [pkg] [pkg]'
'build: Build a package' \ log 'build: Build a package'
'checksum: Generate checksums' \ log 'checksum: Generate checksums'
'install: Install a package' \ log 'install: Install a package'
'list: List installed packages' \ log 'list: List installed packages'
'remove: Remove a package' \ log 'remove: Remove a package'
'search: Search for a package' \ log 'search: Search for a package'
'update: Check for updates' log 'update: Check for updates'
;; ;;
*) die "'kiss $action' is not a valid command" ;; *) die "'kiss $action' is not a valid command" ;;