Wednesday, October 10, 2012

Bulk package adder script for OpenBSD 5.1

Currently, I am in the process of learning OpenBSD 5.1 in addition to teaching myself Perl.  In particular, I want to play around with the Catalyst framework.  After doing some searching, I could not find an easy way to install a list of packages generated by redirecting pkg_info's output to a text file; so, I figured this is the perfect time to try my hand at writing a Perl script to do the job for me.  Yes, the script is a bit primitive, but I did learn a fair amount during its creation.

Naturally, after creating this script, I learned about 'pkg_add -l /path/to/listofpackages'.  That's OK - I learned some valuable skills in Perl that I can transfer to other applications.

Here is how I generated my list of packages to install for the Catalyst framework on Perl 5:

# pkg_info -Q p5-Catalyst > packages.txt

Here is the code for the script (which I plan to put on GitHub):

#!/usr/bin/perl -w

# Name:
# Version:      1.0
# Revision:     October 10, 2012
# By:           Ramon J. Long III

# This script will read in the contents of 'packages.txt'
# and use pkg_add to install each package.

# There are different ways to generate your package list.
# For exmple: `pkg_info -Q p5-Catalyst > packages.txt'
# This command will create a list of all Perl5 Catalyst packages.

# TO DO:
# Add command switch to add or remove packages.
# Add options for pkg_add and pkg_delete switches.

use strict;
use warnings;

# Open the package list to install:
open (FILE, "packages.txt") or die ("Unable to open file $!");

# Assign the file handle to an array:
my @openbsd_pkgs = <FILE>;

# Let the user know we are processing their package list:
print "Now processing the package list.  This may take a while...\n";

# Iterate over each package name in the array:
foreach (@openbsd_pkgs) {
        # Remove any new line characters and whitespace:
        chomp ($_);
        # Verbosely and interactively open the pkg_add command with progress meter
        # and pipe in our current package from the array:
        open my $fh, '-|', "pkg_add -v -i -m $_" or die "Can't open pipe: $!";
        # Execute the command:
        my @lines = <$fh>;
        # Close the command handle:
        close $fh or die "Can't close pipe $!";


# Close the file hand for packages.txt:
close (FILE);

# Return a true value in case this script eventually becomes a module:

No comments:

Post a Comment