Skip to content

Commit

Permalink
pkg_replace-20221222
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeguchi committed Dec 22, 2022
1 parent 23847ab commit 18c311e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 30 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
20221222
*Option "-dd" or "-RR", as fast as possible and strictly checking dependencies

20221217
*Fix bugs related "-P" or "-PP" option
If binary package file exists,
Expand Down
13 changes: 10 additions & 3 deletions pkg_replace.1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.\" $Id: pkg_replace.1,v 1.2 2007/01/25 12:25:56 securedog Exp $
.\"
.\" Modified by Ken DEGUCHI (December 17, 2022)"
.Dd December 17, 2022
.\" Modified by Ken DEGUCHI (December 22, 2022)"
.Dd December 22, 2022
.Dt PKG_REPLACE 1
.Os
.Sh NAME
Expand Down Expand Up @@ -90,7 +90,14 @@ replace all packages that required by those packages as well.
Strictly checking dependencies more than
.Fl d, Fl R,
e.g. build dependencies.
However, this mode is very slow.
However, this mode is slow.
So,
.Nm
creates temporary files in the
.Ar PKG_REPLACE_DB_DIR
directory to run as fast as possible.
See config file
.Ar pkg_replace.conf .
.Pp
.It Fl \-debug
Debug mode.
Expand Down
8 changes: 7 additions & 1 deletion pkg_replace.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# PKG_DBDIR="/var/db/pkg"
# PKG_TMPDIR="/var/tmp"
# PKGCOMPATDIR="/usr/local/lib/compat/pkg"

#
# Alternative locations for pkg_install: string
#
# PKG_ADD="/usr/sbin/pkg add"
Expand Down Expand Up @@ -55,6 +55,12 @@
#
# PKG_REPLACE="-bv -l /root/pkg_replace.log -L /var/log/pkg/"

# PKG_REPLACE_DB_DIR: string
#
# Temporary directory for saving build dependencies of each ports.
#
# PKG_REPLACE_DB_DIR="/var/tmp/pkg_replace"

# IGNORE: array
#
# List of packages that do not use pkg_replace(1) to upgrade.
Expand Down
102 changes: 76 additions & 26 deletions pkg_replace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# - Cleanup Code


PKG_REPLACE_VERSION=20221217
PKG_REPLACE_VERSION=20221222
PKG_REPLACE_CONFIG=FreeBSD

usage() {
Expand All @@ -34,7 +34,7 @@ usage: ${0##*/} [-habBcCddfFiJknNOpPPrRRuvVwW] [--automatic]
[-M make_env] [-x pkgname]
[[pkgname[=package]] [package] [pkgorigin] ...]
EOF
exit 1
exit 0
}

isempty() {
Expand Down Expand Up @@ -125,6 +125,7 @@ init_variables() {
: ${PKG_BACKUP_DIR=${PKGREPOSITORY}}
: ${PKG_TMPDIR=${TMPDIR:-"/var/tmp"}}
: ${PKGCOMPATDIR="%%PKGCOMPATDIR%%"}
: ${PKG_REPLACE_DB_DIR=${PKG_REPLACE_DB_DIR:-"/var/tmp/pkg_replace"}}
export PORTSDIR OVERLAYS PKG_DBDIR PKG_TMPDIR PKG_BINARY_SUFX PKGCOMPATDIR
tmpdir=
set_signal_int=
Expand Down Expand Up @@ -200,7 +201,7 @@ parse_options() {
esac
done

while getopts habBcCdfFiIJj:kl:L:m:M:nNOpPrRuvVwWx: X; do
while getopts abBcCdfFhiIJj:kl:L:m:M:nNOpPrRuvVwWx: X; do
case $X in
a) opt_all=1 ;;
b) opt_keep_backup=1 ;;
Expand All @@ -210,6 +211,7 @@ parse_options() {
d) opt_depends=$((opt_depends+1)) ;;
f) opt_force=1 ;;
F) opt_fetch=1 ;;
h) usage ;;
i) opt_interactive=1 ;;
J) opt_build=1 ;;
j) opt_maxjobs=$( [ ${OPTARG} -ge 1 ] 2> /dev/null && echo ${OPTARG} || sysctl -n hw.ncpu ) ;;
Expand Down Expand Up @@ -242,6 +244,7 @@ parse_options() {
istrue ${opt_omit_check} && opt_keep_going=1

optind=$((OPTIND+long_optind))

}

parse_args() {
Expand Down Expand Up @@ -282,8 +285,7 @@ parse_args() {
esac

if installed_pkg=$(get_installed_pkgname ${ARG}); then
if istrue ${opt_depends}; then
[ ${opt_depends} -ge 2 ] && info "'-dd' option is very slow!"
if ! istrue ${opt_all} && istrue ${opt_depends}; then
upgrade_pkgs="${upgrade_pkgs} $(get_depend_pkgnames "${installed_pkg}")"
fi
upgrade_pkgs="${upgrade_pkgs} ${installed_pkg}"
Expand Down Expand Up @@ -537,27 +539,58 @@ get_depend_pkgnames() {
done
else
deps=$(${PKG_QUERY} '%dn-%dv' $1 | sort -u)
[ ${opt_depends} -ge 2 ] &&
deps="${deps} $(get_strict_depend_pkgnames "$1" "${deps}")"
[ ${opt_depends} -ge 2 ] && {
load_make_vars;
deps=${deps}' '$(get_strict_depend_pkgnames "$1");
}
fi
echo ${deps} | tr ' ' '\n' | sort -u
return 0
}

get_strict_depend_pkgnames() {
local deps pkg origins
deps=$2
local deps pkg origins pkgdeps_file dels cut_deps

deps=
origins=
dels=
cut_deps=

for pkg in $1; do
case " ${deps} " in
*\ ${pkg}\ *) continue;;
pkgdeps_file=${PKG_REPLACE_DB_DIR}/${pkg}.deps
if [ -f ${pkgdeps_file} ]; then
if [ -s ${pkgdeps_file} ]; then
deps=${deps}' '$(cat ${pkgdeps_file})
deps=$(echo ${deps} | tr ' ' '\n' | sort -u)
else
dels=${dels}' '${pkg}
fi
else
#origins=$(cd $(get_portdir_from_origin $(get_origin_from_pkgname ${pkg})) && ${PKG_MAKE} -V BUILD_DEPENDS -V PATCH_DEPENDS -V FETCH_DEPENDS -V EXTRACT_DEPENDS -V LIB_DEPENDS -V RUN_DEPENDS -V PKG_DEPENDS | tr ' ' '\n' | cut -d: -f2 | sort -u)
origins=$(cd $(get_portdir_from_origin $(get_origin_from_pkgname ${pkg})) && ${PKG_MAKE} -V BUILD_DEPENDS -V PATCH_DEPENDS -V FETCH_DEPENDS -V EXTRACT_DEPENDS -V PKG_DEPENDS | tr ' ' '\n' | cut -d: -f2 | sort -u)
if [ -z "${origins}" ]; then
touch ${pkgdeps_file}
dels=${dels}' '${pkg}
else
deps=${deps}' '$(${PKG_QUERY} '%n-%v' ${origins} | sort -u | tee ${pkgdeps_file})
fi
fi
done

deps=$(echo ${deps} | tr ' ' '\n' | sort -u)
echo ${deps} > /tmp/deps
dels=$(echo ${dels} | tr ' ' '\n' | sort -u)
echo ${dels} > /tmp/dels

for pkg in ${deps}; do
case ' '${dels}' ' in
*\ ${pkg}\ *) continue ;;
*) cut_deps=${cut_deps}' '${pkg} ;;
esac
load_make_vars
origins=${origins}" "$(cd $(get_portdir_from_origin $(get_origin_from_pkgname ${pkg})) && ${PKG_MAKE} -V BUILD_DEPENDS -V PATCH_DEPENDS -V FETCH_DEPENDS -V EXTRACT_DEPENDS -V LIB_DEPENDS -V RUN_DEPENDS -V PKG_DEPENDS | tr ' ' '\n' | cut -d: -f2)
origins=$(echo ${origins} | tr ' ' '\n' | sort -u)
done
isempty ${origins} || deps=${deps}" "$(${PKG_QUERY} '%n-%v' $(echo ${origins} | tr ' ' '\n' | sort -u))
echo ${deps} | tr ' ' '\n' | sort -u

echo ${cut_deps} | tee /tmp/cut_deps

return 0
}

Expand Down Expand Up @@ -618,10 +651,11 @@ pkg_sort() {
done

# only pkgs
pkgs=$(echo $@ | tr '\n' ' ')
sorted_dep_list=${dep_list}
dep_list=
for pkg in ${sorted_dep_list}; do
case " $@ " in
case " ${pkgs} " in
*\ ${pkg}\ *) dep_list="${dep_list}${pkg} " ;;
*) continue ;;
esac
Expand Down Expand Up @@ -662,6 +696,15 @@ create_dir() {
fi
}

remove_dir() {
if [ -d "$1" ]; then
try rm -rf "$1" || {
warn "Couldn't remove the directory: $1";
return 1;
}
fi
}

expand_path() {
case "$1" in
[!/]*) echo "${PWD:-`pwd`}/${1#./}" ;;
Expand Down Expand Up @@ -1488,12 +1531,17 @@ main() {

if istrue ${opt_all} || { istrue ${opt_version} && ! istrue $#; }; then
set -- '*'
opt_depends=0
[ ${opt_depends} -eq 1 ] && opt_depends=0
opt_required_by=0
elif ! istrue $#; then
usage
fi

[ ${opt_depends} -ge 2 ] &&
warn "'-dd' or '-RR' option set, this mode is slow!" &&
create_dir ${PKG_REPLACE_DB_DIR}


parse_args ${1+"$@"}

if ! isempty ${opt_exclude}; then
Expand Down Expand Up @@ -1545,18 +1593,18 @@ main() {
create_tmpdir && init_result || exit 1

set_signal_int='set_result "${ARG:-XXX}" failed "aborted"'
set_signal_exit='show_result; write_result "${opt_result}"; clean_tmpdir'
set_signal_exit='show_result; write_result "${opt_result}"; remove_dir "${PKG_REPLACE_DB_DIR}"; clean_tmpdir'
set_signal_handlers

istrue ${opt_omit_check} || pkg_sort ${upgrade_pkgs}

# check installed package
for X in ${upgrade_pkgs}; do
get_installed_pkgname $X 2>&1 > /dev/null || {
install_pkgs="${install_pkgs} $X";
upgrade_pkgs=$(echo ${upgrade_pkgs} | sed "s|$X||g");
}
done
# check installed package
for X in ${upgrade_pkgs}; do
get_installed_pkgname $X 2>&1 > /dev/null || {
install_pkgs="${install_pkgs} $X";
upgrade_pkgs=$(echo ${upgrade_pkgs} | sed "s|$X||g");
}
done

# config
(istrue ${opt_config} || istrue ${opt_force_config}) && {
Expand Down Expand Up @@ -1620,6 +1668,8 @@ main() {
isempty ${failed_pkgs} || exit 1
fi

[ ${opt_depends} -ge 2 ] && remove_dir ${PKG_REPLACE_DB_DIR}

exit 0
}

Expand Down

0 comments on commit 18c311e

Please sign in to comment.