Re: Script to set/unset automatic status in PKGNG database

2012-08-31 Thread John Nielsen
On Aug 30, 2012, at 11:56 PM, Matthew Seaman matt...@freebsd.org wrote:

 On 30/08/2012 22:44, John Nielsen wrote:
 After dialog(1) exits the script has a list of packages to mark as
 automatic. Is there a non-SQL way to efficiently get the inverse?
 I.e. the set { all_packages - new_automatic_package_list } ?
 
 Use pkg query - it is really quite powerful.  This shows all
 non-autoremove packages as name-version:
 
 pkg query -e '%a == 0' '%n-%v'
 
 and this shows the port origin for all autoremove packages:
 
 pkg query -e '%a == 1' '%o'

Thanks. I know about pkg query (and in fact my script uses something very much 
like that to get the initial list of automatic packages). What I was trying to 
do was get a list of packages installed but not in another list. The other list 
represents _future_ automatic packages but not necessarily what is in the 
database now.

In any case, I worked around it but first unsetting all packages and then 
setting the user-selected list back to automatic.

JN

___
freebsd-current@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to freebsd-current-unsubscr...@freebsd.org


Script to set/unset automatic status in PKGNG database

2012-08-30 Thread John Nielsen
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
#!/bin/sh

# Copyright (c) 2012 John Nielsen j...@jnielsen.net

# 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=/var/db/pkg/local.sqlite

# Terminal geometry
sz=`stty size`
rows=`echo ${sz} | cut -d ' ' -f 1`
cols=`echo ${sz} | cut -d ' ' -f 2`
drows=$(( ${rows} - 3 ))
dcols=$(( ${cols} - 6 ))

# Dialog results are stored here
tmpfile=`mktemp -t set_pkg_auto`

# We always want the same style checklist
export DIALOGOPTS=--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=`sed -e s/^,//g -e s/\/'/g ${1}`
sqlite3 ${DB} update packages set automatic=1 where name in 
(${autopkgs}); \
|| die SQlite error.
sqlite3 ${DB} update packages set automatic=0 where name not in 
(${autopkgs}); \
|| die SQlite error.
}

# Run select_current for the first checklist
pkgset=current

# 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=all
;;
1) # Cancel (Deselect all), show the checklist again
pkgset=none
;;
*) # 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.


___
freebsd-current@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to freebsd-current-unsubscr...@freebsd.org

Re: Script to set/unset automatic status in PKGNG database

2012-08-30 Thread Julien Laffaye

On 8/30/2012 11:19 PM, John Nielsen wrote:

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


You want to use `pkg set -A` :)
We make zero promises concerning the SQL schema in pkgng so it can 
change at every time and break your script.

___
freebsd-current@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to freebsd-current-unsubscr...@freebsd.org


Re: Script to set/unset automatic status in PKGNG database

2012-08-30 Thread Baptiste Daroussin
Thank you,

Would you mind adding create a patch against the git tree of pkgng so that we
can include your script into the scripts subdirectory, so that we provide your
script along with the next pkg 1.0.1 as a contributed script?

regards,
Bapt

On Thu, Aug 30, 2012 at 03:19:59PM -0600, John Nielsen wrote:
 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

 #!/bin/sh
 
 # Copyright (c) 2012 John Nielsen j...@jnielsen.net
 
 # 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=/var/db/pkg/local.sqlite
 
 # Terminal geometry
 sz=`stty size`
 rows=`echo ${sz} | cut -d ' ' -f 1`
 cols=`echo ${sz} | cut -d ' ' -f 2`
 drows=$(( ${rows} - 3 ))
 dcols=$(( ${cols} - 6 ))
 
 # Dialog results are stored here
 tmpfile=`mktemp -t set_pkg_auto`
 
 # We always want the same style checklist
 export DIALOGOPTS=--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=`sed -e s/^,//g -e s/\/'/g ${1}`
   sqlite3 ${DB} update packages set automatic=1 where name in 
 (${autopkgs}); \
   || die SQlite error.
   sqlite3 ${DB} update packages set automatic=0 where name not in 
 (${autopkgs}); \
   || die SQlite error.
 }
 
 # Run select_current for the first checklist
 pkgset=current
 
 # 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=all
   ;;
   1) # Cancel (Deselect all), show the checklist again
   pkgset=none
   ;;
   *) # 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.

 
 

 ___
 freebsd-po...@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/freebsd-ports
 To unsubscribe, send any mail to freebsd-ports-unsubscr...@freebsd.org



pgp48ieUrCnUz.pgp
Description: PGP signature


Re: Script to set/unset automatic status in PKGNG database

2012-08-30 Thread Baptiste Daroussin
On Thu, Aug 30, 2012 at 11:29:14PM +0200, Julien Laffaye wrote:
 On 8/30/2012 11:19 PM, John Nielsen wrote:
  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
 
 You want to use `pkg set -A` :)
 We make zero promises concerning the SQL schema in pkgng so it can 
 change at every time and break your script.
 ___
 freebsd-current@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/freebsd-current
 To unsubscribe, send any mail to freebsd-current-unsubscr...@freebsd.org

Oh right I missed the sql part :D


pgpcEMu7yt1ZG.pgp
Description: PGP signature


Re: Script to set/unset automatic status in PKGNG database

2012-08-30 Thread John Nielsen
On Aug 30, 2012, at 3:29 PM, Julien Laffaye jlaff...@freebsd.org wrote:

 On 8/30/2012 11:19 PM, John Nielsen wrote:
 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.
 
 You want to use `pkg set -A` :)
 We make zero promises concerning the SQL schema in pkgng so it can change at 
 every time and break your script.

Thanks. I looked for something like that but not hard enough obviously. I'll 
change it.

After dialog(1) exits the script has a list of packages to mark as automatic. 
Is there a non-SQL way to efficiently get the inverse? I.e. the set { 
all_packages - new_automatic_package_list } ?

JN

___
freebsd-current@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to freebsd-current-unsubscr...@freebsd.org


Re: Script to set/unset automatic status in PKGNG database

2012-08-30 Thread John Nielsen
On Aug 30, 2012, at 3:28 PM, Baptiste Daroussin b...@freebsd.org wrote:

 On Thu, Aug 30, 2012 at 03:19:59PM -0600, John Nielsen wrote:
 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.
 
 Would you mind adding create a patch against the git tree of pkgng so that we
 can include your script into the scripts subdirectory, so that we provide your
 script along with the next pkg 1.0.1 as a contributed script?

No problem. Attached is the output of git diff origin after dropping my 
script in to my local tree. Let me know if you need something else.

Changes between this and the version I originally posted:
Added 2-clause license and disclaimer
Replaced SQL with 'pkg set' commands. Since I didn't come up with a 
fast way to list the packages not in the 'automatic' list, I first set all 
packages to 0 (not automatic), then set the ones in the list to 1. This is 
likely slower than the SQL variant was, but it's not bad and not something 
likely to be run frequently.

JN


set_pkg_auto.sh.diff
Description: Binary data


___
freebsd-current@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to freebsd-current-unsubscr...@freebsd.org

Re: Script to set/unset automatic status in PKGNG database

2012-08-30 Thread Baptiste Daroussin
On Thu, Aug 30, 2012 at 04:33:09PM -0600, John Nielsen wrote:
 On Aug 30, 2012, at 3:28 PM, Baptiste Daroussin b...@freebsd.org wrote:
 
  On Thu, Aug 30, 2012 at 03:19:59PM -0600, John Nielsen wrote:
  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.
  
  Would you mind adding create a patch against the git tree of pkgng so that 
  we
  can include your script into the scripts subdirectory, so that we provide 
  your
  script along with the next pkg 1.0.1 as a contributed script?
 
 No problem. Attached is the output of git diff origin after dropping my 
 script in to my local tree. Let me know if you need something else.
 
 Changes between this and the version I originally posted:
   Added 2-clause license and disclaimer
   Replaced SQL with 'pkg set' commands. Since I didn't come up with a 
 fast way to list the packages not in the 'automatic' list, I first set all 
 packages to 0 (not automatic), then set the ones in the list to 1. This is 
 likely slower than the SQL variant was, but it's not bad and not something 
 likely to be run frequently.
 
 JN


Thanks you should be enough, can you provide a git format-patch patch so that
you get your name in the logs :D

regards,
Bapt


pgpcBpLZ6oxqW.pgp
Description: PGP signature


Re: Script to set/unset automatic status in PKGNG database

2012-08-30 Thread John Nielsen
On Aug 30, 2012, at 4:40 PM, Baptiste Daroussin b...@freebsd.org wrote:

 Thanks you should be enough, can you provide a git format-patch patch so that
 you get your name in the logs :D

Here you go.



0001-Add-script-to-interactively-set-un-set-automatic-sta.patch
Description: Binary data


___
freebsd-current@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to freebsd-current-unsubscr...@freebsd.org

Re: Script to set/unset automatic status in PKGNG database

2012-08-30 Thread Matthew Seaman
On 30/08/2012 22:44, John Nielsen wrote:
 After dialog(1) exits the script has a list of packages to mark as
 automatic. Is there a non-SQL way to efficiently get the inverse?
 I.e. the set { all_packages - new_automatic_package_list } ?

Use pkg query - it is really quite powerful.  This shows all
non-autoremove packages as name-version:

pkg query -e '%a == 0' '%n-%v'

and this shows the port origin for all autoremove packages:

pkg query -e '%a == 1' '%o'

Cheers,

Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.
PGP: http://www.infracaninophile.co.uk/pgpkey




signature.asc
Description: OpenPGP digital signature