* corrected issue in reinstall that prevented reinstall of package files * added version command * updated files() in Package.pm to accept both quiet and verbose options * added sha() to Snap.pm which will be used for verify function in future * corrected missing '=' in output for search() in Sources.pm * removed duplicate file (SRC/snapinstall)
359 lines
8.3 KiB
Perl
359 lines
8.3 KiB
Perl
package Snap::Sources;
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
use Compress::Zlib;
|
|
use Data::Dumper;
|
|
|
|
use parent 'Snap';
|
|
|
|
sub new {
|
|
my $class = shift;
|
|
my $srcconf = shift;
|
|
my $sources = {};
|
|
|
|
foreach my $source ( @$srcconf ) {
|
|
if ( $source =~ /^(\S+)\s*=\s*(\S+?)\/*\s+(\S.*)$/ ) {
|
|
$sources->{'config'}{$1}{'url'} = "$2/" . Snap->SNAPVER;
|
|
$sources->{'config'}{$1}{'order'} =
|
|
keys( %{$sources->{'config'}} );
|
|
|
|
foreach ( split( /\s+/, $3 ) ) {
|
|
$sources->{'config'}{$1}{'repos'}{$_} = {};
|
|
}
|
|
}
|
|
else {
|
|
Snap->error( int( $! ), "Snap::Sources->new():"
|
|
. " Invalid source format: $source" );
|
|
}
|
|
}
|
|
|
|
return( bless( $sources, $class ) );
|
|
}
|
|
|
|
sub readpkgs {
|
|
my $self = shift;
|
|
$self->{'installed'} = {};
|
|
|
|
foreach my $source ( sort { $self->{'config'}{$a}{'order'} <=>
|
|
$self->{'config'}{$b}{'order'} } ( keys( %{$self->{'config'}} ) ) ) {
|
|
my $repos = $self->{'config'}{$source}{'repos'};
|
|
|
|
foreach my $repo ( keys( %$repos ) ) {
|
|
my $repopath = "$self->{'config'}{$source}{'url'}"
|
|
. "/$repo";
|
|
my $file = Snap->SRCDIR . "/$source/$repo-packages.gz";
|
|
my $gz = gzopen( $file, 'r' ) ||
|
|
Snap->error( int( $! ), "gzopen: $file: $!" );
|
|
my $buff;
|
|
my $pkg = {};
|
|
|
|
while ( $gz->gzreadline( $buff ) ) {
|
|
if ( $buff =~ /^name:\s+(.*)$/ &&
|
|
! $self->{'pkgs'}{$1} ) {
|
|
$pkg->{'name'} = $1;
|
|
$self->{'pkgs'}{$1} = [];
|
|
}
|
|
elsif ( $buff =~ /^(\S+):\s+(.*)$/ ) {
|
|
$pkg->{$1} = $2;
|
|
}
|
|
elsif ( $buff =~ /^$/ && $pkg->{'name'} ) {
|
|
$pkg->{'source'} = $source;
|
|
$pkg->{'path'} = "$repopath"
|
|
. "/$pkg->{'path'}";
|
|
|
|
push( @{$self->{'pkgs'}{$pkg->{'name'}}}
|
|
,Snap::Package->new( $pkg )
|
|
);
|
|
}
|
|
else {
|
|
Snap->error( -1, "Snap::Source->new:"
|
|
. "$file: malformed package"
|
|
. " list" );
|
|
}
|
|
}
|
|
|
|
$gz->gzclose();
|
|
}
|
|
}
|
|
|
|
opendir( DIR, Snap->INSTDIR ) || Snap->error( -1, "opendir(); "
|
|
. Snap->INSTDIR . ": $!" );
|
|
|
|
foreach my $dir ( readdir( DIR ) ) {
|
|
my $snapinfo = Snap->INSTDIR . "/$dir/snapinfo";
|
|
my $package;
|
|
|
|
if ( $dir =~ /^\.{1,2}$/ || ! -f $snapinfo ) {
|
|
next;
|
|
}
|
|
|
|
$self->{'installed'}{$dir} = Snap::Package->new( $dir );
|
|
}
|
|
|
|
close( DIR );
|
|
|
|
return( 1 );
|
|
}
|
|
|
|
sub refresh {
|
|
my $self = shift;
|
|
my $cnt;
|
|
|
|
foreach my $srcname ( sort { $self->{'config'}{$a}{'order'} <=>
|
|
$self->{'config'}{$b}{'order'} }( keys( %{$self->{'config'}} ) ) ) {
|
|
my $source = $self->{'config'}{$srcname};
|
|
my $srcdir = Snap->SRCDIR . "/$srcname";
|
|
|
|
if ( ! -d $srcdir ) {
|
|
mkdir( $srcdir, 0755 ) || Snap->error( int( $! ),
|
|
"mkdir(): $srcdir: $!" );
|
|
}
|
|
|
|
if ( $cnt ) {
|
|
print "\n";
|
|
}
|
|
|
|
print "Refreshing $srcname\n";
|
|
|
|
foreach my $repo ( sort( keys( %{$source->{'repos'}} ) ) ) {
|
|
my $remotepkgs = "$source->{'url'}/"
|
|
. "/$repo-packages.gz";
|
|
my $remotesha256 = "$source->{'url'}/"
|
|
. "/$repo-packages.gz.sha256";
|
|
my $localpkgs = "$srcdir/$repo-packages.gz";
|
|
my $shaget = Snap->httpget( $remotesha256, 0, 0644 );
|
|
|
|
Snap->httpget( $remotepkgs, $localpkgs, 0644 );
|
|
|
|
if ( Snap->sha256( $localpkgs ) ne $shaget ) {
|
|
Snap->error( -1, "sha256(): incorrect SHA256"
|
|
. " calculated for $localpkgs!" );
|
|
}
|
|
}
|
|
|
|
$cnt++;
|
|
}
|
|
}
|
|
|
|
sub search {
|
|
my $self = shift;
|
|
my $opts = shift;
|
|
my $packages = [];
|
|
my $cnt;
|
|
|
|
foreach my $pkgname ( sort( keys( %{$self->{'pkgs'}} ) ) ) {
|
|
my $package;
|
|
|
|
if ( $opts->{'name'} && $pkgname ne $opts->{'name'} ) {
|
|
next;
|
|
}
|
|
|
|
if ( $opts->{'version'} && ! $opts->{'name'} &&
|
|
$opts->{'quiet'} ) {
|
|
Snap->error( -1, "$opts->{'version'}:"
|
|
. " missing package name" );
|
|
}
|
|
|
|
foreach ( sort { Snap->vercmp( $a->{'version'},
|
|
$b->{'version'} ) } ( @{$self->{'pkgs'}{$pkgname}} ) ) {
|
|
if ( $opts->{'version'} && $opts->{'version'} =~
|
|
/^((<|>)=?|=)\s*(.*)/ ) {
|
|
my $op = $1;
|
|
my $ver = $3;
|
|
my $chk = Snap->vercmp( $_->{'version'}, $ver );
|
|
|
|
if ( $op eq '<' && $chk != -1 ) {
|
|
next;
|
|
}
|
|
if ( $op eq '<=' && $chk > 0 ) {
|
|
next;
|
|
}
|
|
if ( $op eq '>' && $chk != 1 ) {
|
|
next;
|
|
}
|
|
if ( $op eq '>=' && $chk < 0 ) {
|
|
next;
|
|
}
|
|
if ( $op eq '=' && $chk != 0 ) {
|
|
next;
|
|
}
|
|
}
|
|
elsif ( $opts->{'version'} && $_->{'version'} ne
|
|
$opts->{'version'} ) {
|
|
next;
|
|
}
|
|
if ( $opts->{'depends'} && $_->{'depends'} !~
|
|
/$opts->{'depends'}/ ) {
|
|
next;
|
|
}
|
|
if ( $opts->{'source'} && $_->{'source'} ne
|
|
$opts->{'source'} ) {
|
|
next;
|
|
}
|
|
if ( $opts->{'repo'} && $_->{'repo'} ne
|
|
$opts->{'repo'} ) {
|
|
next;
|
|
}
|
|
if ( $opts->{'string'} && ( $_->{'name'} !~
|
|
/$opts->{'string'}/ && $_->{'description'} !~
|
|
/$opts->{'string'}/ ) ) {
|
|
next;
|
|
}
|
|
|
|
if ( $opts->{'all'} ) {
|
|
push( @$packages, $_ );
|
|
}
|
|
elsif ( ! $package || Snap->vercmp( $_->{'version'},
|
|
$package->{'version'} ) ) {
|
|
$package = $_;
|
|
}
|
|
}
|
|
|
|
if ( $package ) {
|
|
push( @$packages, $package );
|
|
}
|
|
}
|
|
|
|
if ( ! @$packages ) {
|
|
if ( $opts->{'name'} && $opts->{'version'} ) {
|
|
Snap->error( 0, "Snap::Sources::search():"
|
|
. " $opts->{'name'}=$opts->{'version'}:"
|
|
. " No such package" );
|
|
}
|
|
elsif ( $opts->{'name'} ) {
|
|
Snap->error( 0, "Snap::Sources::search():"
|
|
. " $opts->{'name'}: No such package" );
|
|
}
|
|
elsif ( $opts->{'string'} ) {
|
|
Snap->error( 0, "Snap::Sources::search():"
|
|
. " No package matching '$opts->{'string'}'" );
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if ( $opts->{'quiet'} ) {
|
|
if ( @$packages == 1 ) {
|
|
return( $packages->[0] );
|
|
}
|
|
else {
|
|
return( $packages );
|
|
}
|
|
}
|
|
|
|
foreach my $package ( @$packages ) {
|
|
if ( $opts->{'verbose'} ) {
|
|
if ( $cnt ) {
|
|
print "\n";
|
|
}
|
|
|
|
$package->printself();
|
|
|
|
$cnt++;
|
|
}
|
|
else {
|
|
$package->printbrief();
|
|
}
|
|
}
|
|
|
|
return( 1 );
|
|
}
|
|
|
|
1;
|
|
|
|
=head1 NAME
|
|
|
|
Snap::Sources - Interface for Snaplinux package sources
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
This module is not intended to be used directly, rather it is included with the parent Snap.pm module. It is separated into its own module only to logically separate the code.
|
|
|
|
Snap::Sources includes all functions for retrieving, parsing, and searching through package lists. The structure of package objects is defined in Snap::Package.
|
|
|
|
=head1 METHODS
|
|
|
|
=head2 new
|
|
|
|
$sources = Snap::Sources->new( $arrayref )
|
|
|
|
If $arrayref contains a list of valid sources the $sources object will be built. The $arrayref is intended to be populated with values parsed from /etc/snap.conf. The syntax for sources is as follows:
|
|
|
|
[sources]
|
|
source1 = http://packages.snaplinux.org core dev util
|
|
|
|
Each item listed under the [sources] section is added to $sources->{'config'}. The following describes the structure:
|
|
|
|
$sources => {
|
|
config => {
|
|
source1 => {
|
|
url => 'http://packages.snaplinux.org/0.1',
|
|
order => 1,
|
|
repos => {
|
|
dev => {},
|
|
core => {},
|
|
util => {}
|
|
|
|
=head2 readpkgs
|
|
|
|
$sources->readpkgs()
|
|
|
|
Parses all source/repo files and builds a list of packages which is available in $sources->{'pkgs'}. Also reads all installed packages and adds them to $sources->{'installed'}. The list is built with the following structure:
|
|
|
|
$sources => {
|
|
pkgs => {
|
|
<PKGNAME> => [
|
|
Snap::Package->{'version'} => 1
|
|
Snap::Package->{'version'} => 2
|
|
]
|
|
}
|
|
installed => {
|
|
<PKGNAME> => Snap::Package
|
|
|
|
=head2 search
|
|
|
|
$sources->search( $searchterms )
|
|
|
|
This will search all sources and repos in the $sources object for the search terms and either print the output, or if { quiet => 1 } is supplied as an arg it will return the highest version of the matched package. The quiet option is only intended for internal routines rather than for queries intended to display output on the command line.
|
|
|
|
Other available options that can be set (using key => value pairs):
|
|
|
|
=over 4
|
|
|
|
=item name
|
|
|
|
This will only return packages where the name matches exactly
|
|
|
|
=item version
|
|
|
|
The operators <, <=, >, >=, = can all be used to retrieve the desired package version. The operator should preface the version string.
|
|
|
|
=item depends
|
|
|
|
The supplied string will be used as a patter to match dependencies in packages.
|
|
|
|
=item source
|
|
|
|
Return only packages available from the specified source.
|
|
|
|
=item repo
|
|
|
|
Return only packages from the specified repo
|
|
|
|
=item string
|
|
|
|
The supplied string will be used to match against either the package name, or the package description.
|
|
|
|
=back
|
|
|
|
=head2 refresh
|
|
|
|
$sources->refresh()
|
|
|
|
This will download all package information for the repos defined in $sources->{'config'}. The data will be stored in /var/lib/snap/sources/<SOURCENAME>
|
|
|
|
=cut
|
|
|