Last active
January 10, 2016 21:55
-
-
Save dracorp/7ae2729c1ab2e131eac9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#=============================================================================== | |
# NAME | |
# repo-manage.sh - manage local ArchLinux repo for AUR packages | |
# | |
# SYNOPSIS | |
# repo-manage.sh [-hu] package action | |
# | |
# DESCRIPTION | |
# The program maintain local package database. Is auxilary program to adding or removing a package from local database when the package will be built by makepkg program or when will be removed. | |
# The program cooperate with daemon incron which monitor a directory patch for changes in filesystem. | |
# | |
# The package is name of package created by makepkg (from local build or invoked internally by eg. program yaourt). The action is mask from incron program eg. IN_CREATE, IN_MODIFY or IN_DELETE. For more see `man 5 incrontab`. | |
# | |
# OPTIONS | |
# | |
# -u update database, add missing and remove not existing in database | |
# -h - print help | |
# | |
# EXAMPLES | |
# | |
# REQUIREMENTS | |
# | |
# BUGS | |
# | |
# NOTES | |
# | |
# AUTHOR | |
# Piotr Rogoza aka dracorp <piotr.r.public@gmail.com> | |
# | |
# ORGANIZATION | |
# | |
# DATE | |
# $Date: $ | |
# | |
# REVISION | |
# $Revision: $ | |
# | |
# ID | |
# $Id: $ | |
# | |
#=============================================================================== | |
shopt -s extglob | |
USE_COLOR='y' | |
PROGRAM_OPTIONS='hu' | |
# shell-includes https://github.com/l0b0/shell-includes | |
help() { #{{{ | |
local header | |
local pre_header='' | |
while IFS= read -r line || [ -n "$line" ]; do | |
case "$line" in | |
'#!'*) # Shebang line | |
;; | |
'#='*) # comment from header | |
;; | |
''|'##'*|[!#]*) # End of comments | |
return #"${1:-0}" | |
;; | |
*) # Comment line | |
line=${line:2} # Remove comment prefix | |
if [[ "$1" = usage ]]; then | |
# print only usage | |
if [[ $pre_header = SYNOPSIS ]]; then | |
return | |
fi | |
if [[ "${line}" =~ ^[A-Z\s]+$ ]]; then | |
header=${line} | |
fi | |
if [[ "$header" = SYNOPSIS ]]; then | |
if [[ "$line" = SYNOPSIS ]]; then | |
printf '%s\n' 'Usage:' >&2 | |
else | |
printf '%s\n' "${line}" >&2 | |
fi | |
else | |
pre_header=$header | |
fi | |
else | |
printf '%s\n' "${line}" >&2 | |
fi | |
;; | |
esac | |
done < "$0" | |
} #}}} | |
usage() { #{{{ | |
help usage | |
exit 1 | |
} #}}} | |
# check if messages are to be printed using color {{{ | |
unset ALL_OFF BOLD BLUE GREEN RED YELLOW | |
if [[ -t 2 && ! $USE_COLOR = "n" ]]; then | |
# prefer terminal safe colored and bold text when tput is supported | |
if tput setaf 0 &>/dev/null; then | |
ALL_OFF="$(tput sgr0)" | |
BOLD="$(tput bold)" | |
BLUE="${BOLD}$(tput setaf 4)" | |
GREEN="${BOLD}$(tput setaf 2)" | |
RED="${BOLD}$(tput setaf 1)" | |
YELLOW="${BOLD}$(tput setaf 3)" | |
else | |
ALL_OFF="\e[1;0m" | |
BOLD="\e[1;1m" | |
BLUE="${BOLD}\e[1;34m" | |
GREEN="${BOLD}\e[1;32m" | |
RED="${BOLD}\e[1;31m" | |
YELLOW="${BOLD}\e[1;33m" | |
fi | |
fi | |
readonly ALL_OFF BOLD BLUE GREEN RED YELLOW | |
# }}} | |
REPO_NAME='laptop' | |
REPO_SOURCE_DIR='/var/cache/pacman/aur' | |
REPO_SYNC_DIR='/var/lib/pacman/sync' | |
REPO_DB_PATH="$REPO_SYNC_DIR/$REPO_NAME.db.tar.xz" | |
REPO_DB_FILES_PATH="$REPO_SYNC_DIR/$REPO_NAME.files.tar.xz" | |
PKG_EXT='pkg.tar.xz' | |
PACKAGES="*.$PKG_EXT" | |
OPTS_ADD='--quiet --new --remove' | |
OPTS_REMOVE='--nocolor' | |
LOG_FILE="$HOME/tmp/incron.log" | |
CARCHS=(x86_64 i686) | |
REPO_ADD="repo-add $OPTS_ADD $REPO_DB_PATH" | |
REPO_ADD_FILES="repo-add $OPTS_ADD --files $REPO_DB_FILES_PATH" | |
REPO_REMOVE="repo-remove $OPTS_REMOVE $REPO_DB_PATH" | |
REPO_REMOVE_FILES="repo-remove $OPTS_REMOVE $REPO_DB_FILES_PATH" | |
update_packages() { #{{{ | |
cd $REPO_SOURCE_DIR | |
# add all | |
$REPO_ADD $PACKAGES | |
$REPO_ADD_FILES $PACKAGES | |
# remove non existing in database | |
local packages package | |
packages=$(tar -tJf $REPO_DB_PATH | awk -F'/' '{print $1}' | sort -u) | |
echo "Removing not existing packages in $REPO_NAME.db.tar.xz" | |
for package in $packages; do | |
ls -1 $REPO_SOURCE_DIR | grep $package -q | |
if [[ $? -ne 0 ]]; then | |
echo "Checking $package" | |
package=$( echo $package | sed 's/-[0-9.]\+-[0-9]\+$//' ) | |
$REPO_REMOVE $package | |
fi | |
done | |
echo "Removing not existing packages in $REPO_NAME.files.tar.xz" | |
packages=$(tar -tJf $REPO_DB_FILES_PATH | awk -F'/' '{print $1}' | sort -u) | |
for package in $packages; do | |
ls -1 $REPO_SOURCE_DIR | grep $package -q | |
if [[ $? -ne 0 ]]; then | |
echo "Checking $package" | |
package=$( echo $package | sed 's/-[0-9.]\+-[0-9]\+$//' ) | |
$REPO_REMOVE_FILES $package | |
fi | |
done | |
} #}}} | |
remove_package() { #{{{ | |
local package carch | |
package=$1 | |
for carch in ${CARCHS[*]}; do | |
package=${package%%-$carch*} | |
done | |
$REPO_REMOVE $package | |
$REPO_REMOVE_FILES $package | |
} #}}} | |
add_package() { #{{{ | |
local package | |
package=$1 | |
cd $REPO_SOURCE_DIR | |
$REPO_ADD $package | |
$REPO_ADD_FILES $package | |
} #}}} | |
if [[ $# -lt 1 ]]; then | |
usage | |
fi | |
set -- $(getopt $PROGRAM_OPTIONS $*) | |
while [[ "$1" != -- ]]; do | |
case $1 in | |
-h) | |
help | |
exit | |
;; | |
-u) | |
update_packages | |
exit | |
;; | |
esac | |
shift | |
done | |
shift #remove -- | |
if [[ $# -gt 1 ]]; then | |
if [[ $1 =~ $PKG_EXT$ ]]; then | |
package=$1 | |
else | |
exit | |
fi | |
if [[ $2 == @(IN_DELETE) ]]; then | |
remove_package $package | |
elif [[ $2 == @(IN_MODIFY|IN_CREATE) ]]; then | |
add_package $package | |
fi | |
elif [[ $# -eq 0 ]]; then | |
update_packages | |
elif [[ $# -eq 1 ]]; then | |
usage | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment