From owner-freebsd-current@FreeBSD.ORG Thu Aug 30 21:19:48 2012 Return-Path: Delivered-To: current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id C5BB1106566C; Thu, 30 Aug 2012 21:19:48 +0000 (UTC) (envelope-from lists@jnielsen.net) Received: from ns1.jnielsen.net (secure.freebsdsolutions.net [69.55.234.48]) by mx1.freebsd.org (Postfix) with ESMTP id A46B38FC15; Thu, 30 Aug 2012 21:19:48 +0000 (UTC) Received: from [10.10.1.32] (office.betterlinux.com [199.58.199.60]) (authenticated bits=0) by ns1.jnielsen.net (8.14.4/8.14.4) with ESMTP id q7ULJjpo006536 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Thu, 30 Aug 2012 17:19:45 -0400 (EDT) (envelope-from lists@jnielsen.net) From: John Nielsen Content-Type: multipart/mixed; boundary="Apple-Mail=_730A9CF5-445A-45DA-ADDA-BCD1351C8486" Message-Id: Date: Thu, 30 Aug 2012 15:19:59 -0600 To: "ports@freebsd.org" , "current@freebsd.org" Mime-Version: 1.0 (Mac OS X Mail 6.0 \(1486\)) X-Mailer: Apple Mail (2.1486) X-DCC-sonic.net-Metrics: ns1.jnielsen.net 1117; Body=2 Fuz1=2 Fuz2=2 X-Virus-Scanned: clamav-milter 0.97.5 at ns1.jnielsen.net X-Virus-Status: Clean Cc: Subject: Script to set/unset "automatic" status in PKGNG database X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Aug 2012 21:19:49 -0000 --Apple-Mail=_730A9CF5-445A-45DA-ADDA-BCD1351C8486 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii I today noticed the "pkg autoremove" command for the first time, which = does much the same thing as pkg_cutleaves but relies on the "automatic" = flag in the pkgng database rather than user input to determine which = "leaf" ports can be removed. Unfortunately, the pkg2ng utility has no = way of knowing which old-style packages it converts were installed = automatically as dependencies, so they are all marked as non-automatic = (i.e. user-requested). In my case, this was not true for the majority of = installed ports. Since I really like this functionality, I decided to = update my local package database to match my preferences. Having succeeded, I decided a tool to make doing so easy could well = benefit others (as well as my future self). (Plus I wanted an excuse to = play with dialog(1) and "pkg query" a bit.) So here's the result. I'm = not too attached to the name. It shouldn't eat your package database or = steal your lunch money, but I'm not responsible if it does. Other than = that, feedback is welcome. JN --Apple-Mail=_730A9CF5-445A-45DA-ADDA-BCD1351C8486 Content-Disposition: attachment; filename=set_pkg_auto.sh.txt Content-Type: text/plain; name="set_pkg_auto.sh.txt" Content-Transfer-Encoding: quoted-printable #!/bin/sh # Copyright (c) 2012 John Nielsen # This script presents a checklist of all PKGNG packages registered on # the system, showing for each whether or not it is marked as = "automatic" # (i.e. not explicitly requested by the user). Any changes are recorded # in the PKGNG database. I wrote it to make "pkg autoremove" useful # following a pkg2ng migration, but other uses are conceivable. # The PKGNG database file to use DB=3D/var/db/pkg/local.sqlite # Terminal geometry sz=3D`stty size` rows=3D`echo ${sz} | cut -d ' ' -f 1` cols=3D`echo ${sz} | cut -d ' ' -f 2` drows=3D$(( ${rows} - 3 )) dcols=3D$(( ${cols} - 6 )) # Dialog results are stored here tmpfile=3D`mktemp -t set_pkg_auto` # We always want the same style checklist export DIALOGOPTS=3D"--extra-button --extra-label \"Select All\" = --cancel-label \"Deselect All\" --help-button --help-label Exit = --separator ," # Exit with an error message die() { rm -f ${tmpfile} echo "${1}" exit 1 } # Don't leave tmpfile behind even if we are killed/interrupted trap "die \"Interrupt received.\"" 2 15 # Run dialog to present the checklist and save the results in tmpfile run_dialog() { dialog --checklist "Select packages to consider for = auto-removal" ${drows} ${dcols} ${drows} $* 2>${tmpfile} return $? } # Show the current status from the package database in the checklist select_current() { run_dialog `pkg query '%n %o %a' | sed -e 's/1$/on/g' -e = 's/0$/off/g'` return $? } # Select all packages in the checklist select_all() { run_dialog `pkg query '%n %o' | sed -e 's/$/ on/g'` return $? } # De-select all packages in the checklist select_none() { run_dialog `pkg query '%n %o' | sed -e 's/$/ off/g'` return $? } # Update the package database to match selections in the specified file do_update() { autopkgs=3D`sed -e "s/^,//g" -e "s/\"/'/g" ${1}` sqlite3 ${DB} "update packages set automatic=3D1 where name in = (${autopkgs});" \ || die "SQlite error." sqlite3 ${DB} "update packages set automatic=3D0 where name not = in (${autopkgs});" \ || die "SQlite error." } # Run select_current for the first checklist pkgset=3Dcurrent # Show the checklist until "OK" or "Exit" is selected while : ; do select_${pkgset} case $? in 0) # OK, continue with updates break; ;; 3) # Extra (Select all), show the checklist again pkgset=3Dall ;; 1) # Cancel (Deselect all), show the checklist again pkgset=3Dnone ;; *) # 4-Help (Exit) or ESC or error, exit. die "No changes made." ;; esac done # If we got this far then tmpfile has a list of 'automatic' packages do_update ${tmpfile} # Clean up rm -f ${tmpfile} echo "Updated successfully." --Apple-Mail=_730A9CF5-445A-45DA-ADDA-BCD1351C8486 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii --Apple-Mail=_730A9CF5-445A-45DA-ADDA-BCD1351C8486--