From e0bfe4a8a07aeacc87f5611122026ff4f4586607 Mon Sep 17 00:00:00 2001 From: Mike Dacre Date: Wed, 7 Mar 2018 15:44:59 -0800 Subject: [PATCH 1/6] Experiment with default system python --- careful_rm.alias.sh | 52 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/careful_rm.alias.sh b/careful_rm.alias.sh index 5e33cd3..ff22d2c 100644 --- a/careful_rm.alias.sh +++ b/careful_rm.alias.sh @@ -2,7 +2,31 @@ # careful_rm aliases for sh, bash, and zsh # ####################################################################### -# Get PATH to the python script +# First get the *system* python, the python script should work +# everywhere, even on old systems, by using system python by +# default, we can avoid possible issues with faulty python installs. +# This is not used if we end up using a pip installed version +if hash python 2>/dev/null; then + _PY=$(command -pv python) + declare -i _pyver + _pyver=$(${_PY} --version | sed 's/.* \([0-9]\).\([0-9]\).*/\1\2/') + # Try to get another version if the system version is ancient + if [[ _pyver -lt 26 ]]; then + _PY=$(command -v python) + _pyver=$(${_PY} --version | sed 's/.* \([0-9]\).\([0-9]\).*/\1\2/') + fi + if [[ _pyver -lt 26 ]]; then + echo "Python too old for careful_rm, need python 2.6+" + return 1 + exit 1 + fi +else + echo "No python found!! careful_rm will not work" + return 1 + exit 1 +fi + +# Get PATH to the careful_rm.py python script SOURCE="$0" # resolve $SOURCE until the file is no longer a symlink while [ -h "$SOURCE" ]; do @@ -14,25 +38,37 @@ while [ -h "$SOURCE" ]; do done CAREFUL_RM_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" +# Try to use our version first CAREFUL_RM="${CAREFUL_RM_DIR}/careful_rm.py" # Only use our careful_rm if it exists, if not, try for a version on the # PATH, failing that, fall back to rm -I -if [ ! -x "${CAREFUL_RM}" ]; then - if hash careful_rm.py 2>/dev/null; then - CAREFUL_RM="$(command -v careful_rm.py)" - elif hash careful_rm 2>/dev/null; then +_USE_PIP=0 +if [ ! -f "${CAREFUL_RM}" ]; then + # Installed by pip + if hash careful_rm 2>/dev/null; then + _USE_PIP=1 CAREFUL_RM="$(command -v careful_rm)" + # Installed directly + elif hash careful_rm.py 2>/dev/null; then + CAREFUL_RM="$(command -v careful_rm.py)" else CAREFUL_RM="" fi fi -# Set the alias -if [ -x "${CAREFUL_RM}" ]; then +# Set the aliases +if [ -z "${_USE_PIP}" ]; then + echo "PIP!" alias rm="${CAREFUL_RM}" - alias careful_rm="${CAREFUL_RM}" alias current_trash="${CAREFUL_RM} --get-trash \${PWD}" +elif [ -f "${CAREFUL_RM}" ]; then + alias rm="${_PY} ${CAREFUL_RM}" + # Alias careful_rm if it isn't installed via pip already + if ! hash careful_rm 2>/dev/null; then + alias careful_rm="${_PY} ${CAREFUL_RM}" + fi + alias current_trash="${_PY} ${CAREFUL_RM} --get-trash \${PWD}" else echo "careful_rm.py is not available, using regular rm" alias rm="rm -I" From c1c3965f387bc9e644dfb317ae4af251109b6988 Mon Sep 17 00:00:00 2001 From: Mike Dacre Date: Wed, 7 Mar 2018 16:36:28 -0800 Subject: [PATCH 2/6] Add explicit python and better shell handling --- README.rst | 4 ++-- careful_rm.alias.sh | 47 +++++++++++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/README.rst b/README.rst index 1c4c9b7..2173462 100644 --- a/README.rst +++ b/README.rst @@ -67,12 +67,12 @@ Install as a plugin Requirements ~~~~~~~~~~~~ -- An ``sh`` style shell, preferably ``zsh``, ``fish``, or ``bash`` +- An ``sh`` style shell, preferably ``zsh``, ``dash``, or ``bash`` - Python version 2.6+, no additional modules required *It should work almost everywhere* -(**Note**: Windows maintainer wanted, it doesn't work there) +**Note**: If anyone can help with a FISH and/or Windows version, that would be great General Install ~~~~~~~~~~~~~~~ diff --git a/careful_rm.alias.sh b/careful_rm.alias.sh index ff22d2c..f4e8380 100644 --- a/careful_rm.alias.sh +++ b/careful_rm.alias.sh @@ -2,10 +2,34 @@ # careful_rm aliases for sh, bash, and zsh # ####################################################################### -# First get the *system* python, the python script should work -# everywhere, even on old systems, by using system python by -# default, we can avoid possible issues with faulty python installs. -# This is not used if we end up using a pip installed version +# Get PATH to the careful_rm.py python script, works with sh, bash, zsh, +# and dash +if [ -n "${ZSH_VERSION}" ]; then + SOURCE="$0:A" +elif [ -n "${BASH_SOURCE}" ]; then + SOURCE="${BASH_SOURCE}" +elif [ -f "$0" ]; then + SOURCE="$0" +else + SOURCE="$_" +fi +# resolve $SOURCE until the file is no longer a symlink +while [ -h "$SOURCE" ]; do + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + # if $SOURCE was a relative symlink, we need to resolve it relative to the + # path where the symlink file was located + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" +done +CAREFUL_RM_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + +# Try to use our version first +CAREFUL_RM="${CAREFUL_RM_DIR}/careful_rm.py" + +# Get the *system* python, the python script should work everywhere, even on +# old systems, by using system python by default, we can avoid possible issues +# with faulty python installs. This is not used if we end up using a pip +# installed version if hash python 2>/dev/null; then _PY=$(command -pv python) declare -i _pyver @@ -26,21 +50,6 @@ else exit 1 fi -# Get PATH to the careful_rm.py python script -SOURCE="$0" -# resolve $SOURCE until the file is no longer a symlink -while [ -h "$SOURCE" ]; do - DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" - SOURCE="$(readlink "$SOURCE")" - # if $SOURCE was a relative symlink, we need to resolve it relative to the - # path where the symlink file was located - [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" -done -CAREFUL_RM_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" - -# Try to use our version first -CAREFUL_RM="${CAREFUL_RM_DIR}/careful_rm.py" - # Only use our careful_rm if it exists, if not, try for a version on the # PATH, failing that, fall back to rm -I _USE_PIP=0 From c14c6d15291b0a6444d7702e916b4d36592cda29 Mon Sep 17 00:00:00 2001 From: Mike Dacre Date: Wed, 7 Mar 2018 16:38:38 -0800 Subject: [PATCH 3/6] Change trash dir alias --- careful_rm.alias.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/careful_rm.alias.sh b/careful_rm.alias.sh index f4e8380..98252c7 100644 --- a/careful_rm.alias.sh +++ b/careful_rm.alias.sh @@ -70,14 +70,14 @@ fi if [ -z "${_USE_PIP}" ]; then echo "PIP!" alias rm="${CAREFUL_RM}" - alias current_trash="${CAREFUL_RM} --get-trash \${PWD}" + alias trash_dir="${CAREFUL_RM} --get-trash \${PWD}" elif [ -f "${CAREFUL_RM}" ]; then alias rm="${_PY} ${CAREFUL_RM}" # Alias careful_rm if it isn't installed via pip already if ! hash careful_rm 2>/dev/null; then alias careful_rm="${_PY} ${CAREFUL_RM}" fi - alias current_trash="${_PY} ${CAREFUL_RM} --get-trash \${PWD}" + alias trash_dir="${_PY} ${CAREFUL_RM} --get-trash \${PWD}" else echo "careful_rm.py is not available, using regular rm" alias rm="rm -I" From d24f2d3bf56004eae1a70e265fe1fe2af816846b Mon Sep 17 00:00:00 2001 From: Mike Dacre Date: Fri, 9 Mar 2018 15:16:05 -0800 Subject: [PATCH 4/6] Make python detection more robust --- careful_rm.alias.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/careful_rm.alias.sh b/careful_rm.alias.sh index 98252c7..3f88881 100644 --- a/careful_rm.alias.sh +++ b/careful_rm.alias.sh @@ -31,13 +31,13 @@ CAREFUL_RM="${CAREFUL_RM_DIR}/careful_rm.py" # with faulty python installs. This is not used if we end up using a pip # installed version if hash python 2>/dev/null; then - _PY=$(command -pv python) + _PY=$(sh command -pv python) declare -i _pyver - _pyver=$(${_PY} --version | sed 's/.* \([0-9]\).\([0-9]\).*/\1\2/') + _pyver=$(${_PY} --version 2>&1 | sed 's/.* \([0-9]\).\([0-9]\).*/\1\2/') # Try to get another version if the system version is ancient if [[ _pyver -lt 26 ]]; then _PY=$(command -v python) - _pyver=$(${_PY} --version | sed 's/.* \([0-9]\).\([0-9]\).*/\1\2/') + _pyver=$(${_PY} --version 2>&1 | sed 's/.* \([0-9]\).\([0-9]\).*/\1\2/') fi if [[ _pyver -lt 26 ]]; then echo "Python too old for careful_rm, need python 2.6+" From 0ba2429bd17d24d59647916624850822dc6917e8 Mon Sep 17 00:00:00 2001 From: Mike Dacre Date: Sat, 10 Mar 2018 19:27:18 -0800 Subject: [PATCH 5/6] Attempt to make alias work more generally with annoying shells --- careful_rm.alias.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/careful_rm.alias.sh b/careful_rm.alias.sh index 3f88881..60a356c 100644 --- a/careful_rm.alias.sh +++ b/careful_rm.alias.sh @@ -31,7 +31,7 @@ CAREFUL_RM="${CAREFUL_RM_DIR}/careful_rm.py" # with faulty python installs. This is not used if we end up using a pip # installed version if hash python 2>/dev/null; then - _PY=$(sh command -pv python) + _PY=$(sh -c "command -pv python") declare -i _pyver _pyver=$(${_PY} --version 2>&1 | sed 's/.* \([0-9]\).\([0-9]\).*/\1\2/') # Try to get another version if the system version is ancient From fe49c6937e9b88d0eff6ca2e49a5669e2b42b125 Mon Sep 17 00:00:00 2001 From: Mike Dacre Date: Sat, 10 Mar 2018 19:35:32 -0800 Subject: [PATCH 6/6] Increment version to beta-8 --- README.rst | 2 +- careful_rm.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 2173462..6720964 100644 --- a/README.rst +++ b/README.rst @@ -2,7 +2,7 @@ Careful rm ########## -Version: 1.0-beta7 +Version: 1.0-beta8 A wrapper for rm that adds more useful warnings and an optional recycle/trash mode diff --git a/careful_rm.py b/careful_rm.py index 9ccd27f..1d5e22d 100755 --- a/careful_rm.py +++ b/careful_rm.py @@ -69,7 +69,7 @@ # For old versions of python 2 input = raw_input -__version__ = '1.0b7' +__version__ = '1.0b8' # Don't ask if fewer than this number of files deleted CUTOFF = 3