python script to backup installed packages

2010-02-11 Thread Adam Vande More
Sometimes you have need to backup installed packages.  I realize most port
management tools do this automatically, but if you're on a system with a lot
of packages installed and one port management tool fails and you use another
to fix it, /usr/ports/packages can become jumbled.  Anyways, I've written a
simple python script which will create a fresh snapshot of all installed
packages.  These are convenient for backups or installing on a new system.

For anyone interested:

from subprocess import Popen, PIPE
import os
s = Popen('pkg_info -a | grep : | grep
Information',shell=True,stdout=PIPE).communicate()[0]

pkg_location = '/data/packages'

packages = []
for line in s.split('\n'):
info = line.replace('Information for ', '').replace(':','')
packages.append(info)

os.chdir(pkg_location)

for package in packages:
s = Popen('pkg_create -b ' +
package,shell=True,stdout=PIPE).communicate()[0]

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


Re: python script to backup installed packages

2010-02-11 Thread Giorgos Keramidas
On Thu, 11 Feb 2010 10:15:12 -0600, Adam Vande More amvandem...@gmail.com 
wrote:
 Sometimes you have need to backup installed packages.  I realize most
 port management tools do this automatically, but if you're on a system
 with a lot of packages installed and one port management tool fails
 and you use another to fix it, /usr/ports/packages can become jumbled.

 Anyways, I've written a simple python script which will create a fresh
 snapshot of all installed packages.  These are convenient for backups
 or installing on a new system.

 For anyone interested:

 from subprocess import Popen, PIPE
 import os
 s = Popen('pkg_info -a | grep : | grep
 Information',shell=True,stdout=PIPE).communicate()[0]

 pkg_location = '/data/packages'

 packages = []
 for line in s.split('\n'):
 info = line.replace('Information for ', '').replace(':','')
 packages.append(info)

 os.chdir(pkg_location)

 for package in packages:
 s = Popen('pkg_create -b ' +
 package,shell=True,stdout=PIPE).communicate()[0]

Nice script!

My own version was initially written in sh(1) to avoid having a Python
dependency.  Then I rewrote it in Python too.  FWIW, you can probably
save a few replace() calls and avoid the need for a full array of all
the packages by yielding the package names:

from subprocess import PIPE, Popen as popen

def listpackages():
for l in iter(popen(pkg_info, shell=True, 
stdout=PIPE).stdout.readline, ):
yield l.split()[0]

for p in listpackages():
dostuff(p)

My own version groks an os.environ['EXTRA_PKG_CREATE_ARGS'] option too
and inserts the extra options before the [-b, package] arguments of
pkg_create, so that I can run the script for example with:

env EXTRA_PKG_CREATE_ARGS='-Rvn' ./savepkg.py

This way package dependencies are saved too (-R option), the output of
the `pkg_create -b' command is slightly more verbose, and saving the
same package multiple times doesn't overwrite existing packages of the
same version (-n option).

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


Re: python script to backup installed packages

2010-02-11 Thread Adam Vande More
On Thu, Feb 11, 2010 at 1:31 PM, Giorgos Keramidas keram...@ceid.upatras.gr
 wrote:

 My own version groks an os.environ['EXTRA_PKG_CREATE_ARGS'] option too
 and inserts the extra options before the [-b, package] arguments of
 pkg_create, so that I can run the script for example with:

env EXTRA_PKG_CREATE_ARGS='-Rvn' ./savepkg.py

 This way package dependencies are saved too (-R option), the output of
 the `pkg_create -b' command is slightly more verbose, and saving the
 same package multiple times doesn't overwrite existing packages of the
 same version (-n option).


Thanks for the tips, I may add some of your functionality to my own.  I
think I might add a couple more features like accepting a backup path from
the command line and an auto create for the dir if it doesn't exist.  One
other thing that might be useful is for it to automatically create an
install script for the backed up packages which takes into account
dependencies.  Almost have a package management system like some other
unnamed OS's then. ;)


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


Re: python script to backup installed packages

2010-02-11 Thread Giorgos Keramidas
On Thu, 11 Feb 2010 20:35:16 -0600, Adam Vande More amvandem...@gmail.com 
wrote:
 Thanks for the tips, I may add some of your functionality to my own.
 I think I might add a couple more features like accepting a backup
 path from the command line and an auto create for the dir if it
 doesn't exist.  One other thing that might be useful is for it to
 automatically create an install script for the backed up packages
 which takes into account dependencies.  Almost have a package
 management system like some other unnamed OS's then. ;)

This is bordering on creeping featuritis, so I would probably avoid
going there.  If you *have* a backup of the packages, you can start by
installing portupgrade.  Then you can point portupgrade at the package
archive and use the -PP option to install all the dependencies.

Then you can avoid all that and just use something like:

cd /mnt/backup/packages/All
pkg_add vim-lite\*tbz

This will automatically install any packages vim-lite needs, as long as
they are available in the current working directory.

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