Bug#930447: nvidia-detect: [PATCH] Add option to only output package name if possible

Markus Lindberg markus.lindberg at axis.com
Wed Jun 12 21:35:11 BST 2019


Package: nvidia-detect
Version: 390.116-1
Severity: wishlist

Dear Maintainer,

The reason why I want to implement this option is to make it easier to
parse the output if wanted.

This is quite a big diff since it's was tricky to adapt the new option
while not changing to much. Please let me know if this can be done
better or should be avoided. The possibility to use long options (eg.
--help) was removed since getopts only supports short options (eg. -h).

I used the source package nvidia-graphics-drivers (418.74-1) while
creating the patch.

*** nvidia-detect.in.patch
--- debian/detect/nvidia-detect.in.orig	2019-05-11 13:52:38.000000000 +0200
+++ debian/detect/nvidia-detect.in	2019-06-12 21:37:07.762797572 +0200
@@ -18,12 +18,40 @@
 # along with this program. If not, see <https://www.gnu.org/licenses/>
 #
 
-if [ "$1" = "-h" -o "$1" = "--help" ]; then
-	echo "Usage: nvidia-detect [PCIID]..."
-	echo "       Reports the Debian packages supporting the NVIDIA GPU that is"
-	echo "       installed on the local system (or given as a PCIID parameter)."
-	exit 0
-fi
+usage(){
+printf '%b' "$(basename "$0") [OPTION]... PCIID
+Reports the Debian packages supporting the NVIDIA GPU that is
+installed on the local system (or given as a PCIID parameter).
+
+Options:
+
+	-h\tShow this help text and exit.
+	-s\tOnly output package name (if possible to do so).
+
+Optional arguments:
+
+	PCIID\tOne or more PCIID to check against.
+"
+}
+
+# Handle flags and operators
+while getopts hs FLAG; do
+	case $FLAG in
+	h)
+		usage
+		exit 0
+		;;
+	s)
+		OUTPUT_PACKAGE_NAME_ONLY=true
+		;;
+	\?)
+		usage
+		exit 1
+		;;
+	esac
+done
+
+shift $((OPTIND-1))
 
 shopt -s compat31 nocasematch 2>/dev/null || { echo "Error: this script only works with bash." && exit; } # Avoid cryptic failure when running dash on this script
 
@@ -102,19 +130,25 @@
 	elif grep -q "jessie\|^8" /etc/debian_version
 	then
 		if [[ -n ${VERSIONS[340]} ]]; then
-			if [[ -n ${VERSIONS[304]} ]]; then
-				echo "Your card is supported by the default drivers and legacy driver series 304."
-			else
-				echo "Your card is supported by the default drivers."
-			fi
 			PACKAGE="nvidia-driver"
+			if [ ! "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+				if [[ -n ${VERSIONS[304]} ]]; then
+					echo "Your card is supported by the default drivers and legacy driver series 304."
+				else
+					echo "Your card is supported by the default drivers."
+				fi
+			fi
 		elif [[ -n ${VERSIONS[999]} ]]; then
-			echo "Your card is only supported by the updated drivers from jessie-backports."
-			echo "See https://backports.debian.org for instructions how to use backports."
 			PACKAGE="nvidia-driver/jessie-backports"
+			if [ ! "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+				echo "Your card is only supported by the updated drivers from jessie-backports."
+				echo "See https://backports.debian.org for instructions how to use backports."
+			fi
 		elif [[ -n ${VERSIONS[304]} ]]; then
-			echo "Your card is only supported up to the 304 legacy drivers series."
 			PACKAGE="nvidia-legacy-304xx-driver"
+			if [ ! "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+				echo "Your card is only supported up to the 304 legacy drivers series."
+			fi
 		elif [[ -n ${VERSIONS[173]} ]]; then
 			echo "Uh oh. Your card is only supported by the 173.14 legacy drivers series, which is not in any current Debian suite."
 		elif [[ -n ${VERSIONS[96]} ]]; then
@@ -127,26 +161,34 @@
 	elif grep -q "stretch\|^9" /etc/debian_version
 	then
 		if [[ -n ${VERSIONS[390]} ]]; then
-			if [[ -n ${VERSIONS[340]} ]]; then
-				if [[ -n ${VERSIONS[304]} ]]; then
-					echo "Your card is supported by all driver versions."
+			PACKAGE="nvidia-driver"
+			if [ ! "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+				if [[ -n ${VERSIONS[340]} ]]; then
+					if [[ -n ${VERSIONS[304]} ]]; then
+						echo "Your card is supported by all driver versions."
+					else
+						echo "Your card is supported by the default drivers and legacy driver series 340."
+					fi
 				else
-					echo "Your card is supported by the default drivers and legacy driver series 340."
+					echo "Your card is supported by the default drivers."
 				fi
-			else
-				echo "Your card is supported by the default drivers."
 			fi
-			PACKAGE="nvidia-driver"
 		elif [[ -n ${VERSIONS[999]} ]]; then
-			echo "Your card is only supported by the updated drivers from stretch-backports."
-			echo "See https://backports.debian.org for instructions how to use backports."
 			PACKAGE="nvidia-driver/stretch-backports"
+			if [ ! "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+				echo "Your card is only supported by the updated drivers from stretch-backports."
+				echo "See https://backports.debian.org for instructions how to use backports."
+			fi
 		elif [[ -n ${VERSIONS[340]} ]]; then
-			echo "Your card is only supported up to the 340 legacy drivers series."
 			PACKAGE="nvidia-legacy-340xx-driver"
+			if [ ! "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+				echo "Your card is only supported up to the 340 legacy drivers series."
+			fi
 		elif [[ -n ${VERSIONS[304]} ]]; then
-			echo "Your card is only supported up to the 304 legacy drivers series."
 			PACKAGE="nvidia-legacy-304xx-driver"
+			if [ ! "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+				echo "Your card is only supported up to the 304 legacy drivers series."
+			fi
 		elif [[ -n ${VERSIONS[173]} ]]; then
 			echo "Uh oh. Your card is only supported by the 173.14 legacy drivers series, which is not in any current Debian suite."
 		elif [[ -n ${VERSIONS[96]} ]]; then
@@ -159,26 +201,34 @@
 	elif grep -q "buster\|^10\|bullseye|^11" /etc/debian_version
 	then
 		if [[ -n ${VERSIONS[999]} ]]; then
-			if [[ -n ${VERSIONS[390]} ]]; then
-				if [[ -n ${VERSIONS[340]} ]]; then
-					echo "Your card is supported by all driver versions."
+			PACKAGE="nvidia-driver"
+			if [ ! "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+				if [[ -n ${VERSIONS[390]} ]]; then
+					if [[ -n ${VERSIONS[340]} ]]; then
+						echo "Your card is supported by all driver versions."
+					else
+						echo "Your card is supported by the default drivers and legacy driver series 390."
+					fi
 				else
-					echo "Your card is supported by the default drivers and legacy driver series 390."
+					echo "Your card is supported by the default drivers."
 				fi
-			else
-				echo "Your card is supported by the default drivers."
 			fi
-			PACKAGE="nvidia-driver"
 		elif [[ -n ${VERSIONS[390]} ]]; then
-			echo "Your card is only supported up to the 390 legacy drivers series."
 			PACKAGE="nvidia-legacy-390xx-driver"
+			if [ ! "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+				echo "Your card is only supported up to the 390 legacy drivers series."
+			fi
 		elif [[ -n ${VERSIONS[391]} ]]; then
-			echo "Your card is only supported on the amd64 platform."
-			echo "Your card is only supported up to the 390 legacy drivers series."
 			PACKAGE="nvidia-legacy-390xx-driver:amd64"
+			if [ ! "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+				echo "Your card is only supported on the amd64 platform."
+				echo "Your card is only supported up to the 390 legacy drivers series."
+			fi
 		elif [[ -n ${VERSIONS[340]} ]]; then
-			echo "Your card is only supported up to the 340 legacy drivers series."
 			PACKAGE="nvidia-legacy-340xx-driver"
+			if [ ! "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+				echo "Your card is only supported up to the 340 legacy drivers series."
+			fi
 		elif [[ -n ${VERSIONS[304]} ]]; then
 			echo "Your card is only supported by the 304 legacy drivers series, which is only available up to stretch."
 		elif [[ -n ${VERSIONS[173]} ]]; then
@@ -195,9 +245,13 @@
 	fi;
 
 if [ -n "$PACKAGE" ]; then
-	echo "It is recommended to install the"
-	echo "    $PACKAGE"
-	echo "package."
+	if [ -n "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+		echo "$PACKAGE"
+	else
+		echo "It is recommended to install the"
+		echo "    $PACKAGE"
+		echo "package."
+	fi
 fi
 
 }
@@ -217,15 +271,21 @@
 		exit 0
 	fi
 
-	echo "Detected NVIDIA GPUs:"
-	for d in $NV_DEVICES ; do
-		lspci -nn -s $d
-	done
-
-	for d in $NV_DEVICES ; do
-		echo -e "\nChecking card: $(lspci -s $d | awk -F: '{print $3}')"
-		NV_DETECT "$(lspci -mn -s "$d" | awk '{ gsub("\"",""); print $3 $4 }')"
-	done
+	if [ -n "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+		for d in $NV_DEVICES ; do
+			NV_DETECT "$(lspci -mn -s "$d" | awk '{ gsub("\"",""); print $3 $4 }')"
+		done
+	else
+		echo "Detected NVIDIA GPUs:"
+		for d in $NV_DEVICES ; do
+			lspci -nn -s $d
+		done
+
+		for d in $NV_DEVICES ; do
+			echo -e "\nChecking card: $(lspci -s $d | awk -F: '{print $3}')"
+			NV_DETECT "$(lspci -mn -s "$d" | awk '{ gsub("\"",""); print $3 $4 }')"
+		done
+	fi
 
 else
 
@@ -236,8 +296,12 @@
 			exit 1
 		fi
 
-		echo "Checking driver support for PCI ID [$(echo $PCIID | sed -r 's/(....)(....)/\1:\2/')]"
-		NV_DETECT "$PCIID"
+		if [ -n "$OUTPUT_PACKAGE_NAME_ONLY" ]; then
+			NV_DETECT "$PCIID"
+		else
+			echo "Checking driver support for PCI ID [$(echo $PCIID | sed -r 's/(....)(....)/\1:\2/')]"
+			NV_DETECT "$PCIID"
+		fi
 	done
 
 fi

--
Markus



More information about the pkg-nvidia-devel mailing list