File Coverage

blib/lib/Slackware/SBoKeeper/System.pm
Criterion Covered Total %
statement 22 38 57.8
branch 1 8 12.5
condition n/a
subroutine 10 12 83.3
pod 7 7 100.0
total 40 65 61.5


line stmt bran cond sub pod time code
1             package Slackware::SBoKeeper::System;
2             our $VERSION = '2.06';
3 3     3   112253 use 5.016;
  3         11  
4 3     3   15 use strict;
  3         5  
  3         83  
5 3     3   11 use warnings;
  3         14  
  3         163  
6              
7 3     3   16 use File::Basename;
  3         7  
  3         1943  
8              
9             my $SLACKWARE_VERSION_FILE = "/etc/slackware-version";
10              
11             sub _slackware_version {
12              
13             # If Slackware version file does not exist, we're probably not on a
14             # Slackware system.
15 3 50   3   135 unless (-e $SLACKWARE_VERSION_FILE) {
16 3         11 return undef;
17             }
18              
19 0 0       0 open my $fh, '<', $SLACKWARE_VERSION_FILE
20             or die "Failed to open $SLACKWARE_VERSION_FILE for reading: $!\n";
21              
22 0         0 my $l = readline $fh;
23 0         0 chomp $l;
24              
25             # Some Slackware version numbers will have a third number, but we'll ignore
26             # it. Most software uses the major.minor numbering scheme.
27 0         0 my ($ver) = $l =~ /^Slackware (\d+\.\d+)/;
28              
29 0 0       0 unless ($ver) {
30 0         0 warn "Bad $SLACKWARE_VERSION_FILE?\n";
31 0         0 return undef;
32             }
33              
34 0         0 return $ver;
35              
36             }
37              
38             my $SLACKWARE_VERSION = _slackware_version();
39             my $IS_SLACKWARE = defined $SLACKWARE_VERSION;
40              
41             my $PKGTOOL_LOGS = undef;
42             my %PACKAGES;
43              
44             if ($IS_SLACKWARE) {
45              
46             $PKGTOOL_LOGS = $SLACKWARE_VERSION < 15
47             ? '/var/log/packages'
48             : '/var/lib/pkgtools/packages'
49             ;
50              
51             __PACKAGE__->load;
52              
53             }
54              
55             sub load {
56              
57 0     0 1 0 %PACKAGES = ();
58              
59 0         0 foreach my $pkgf (glob "$PKGTOOL_LOGS/*") {
60 0         0 $pkgf = basename($pkgf);
61             # Copied this regex from sbopkg
62 0 0       0 $pkgf =~ /(.*)-([^-]*)-([^-]*)-([0-9]*)(.*)/ or next;
63 0         0 $PACKAGES{$1} = {
64             Version => $2,
65             Arch => $3,
66             Build => $4,
67             Tag => $5,
68             };
69             }
70              
71 0         0 return 1;
72              
73             }
74              
75 2     2 1 144570 sub is_slackware { $IS_SLACKWARE };
76              
77 2     2 1 7 sub version { $SLACKWARE_VERSION };
78              
79 0     0 1 0 sub pkgtool_logs { $PKGTOOL_LOGS };
80              
81 3     3 1 44 sub packages { sort keys %PACKAGES };
82              
83             sub packages_by_tag {
84              
85 3     3 1 8 my $self = shift;
86 3         8 my $tag = shift;
87              
88 3         17 return grep { $PACKAGES{$_}->{Tag} eq $tag } $self->packages();
  0         0  
89              
90             }
91              
92             sub installed {
93              
94 36     36 1 56 my $self = shift;
95 36         57 my $pkg = shift;
96              
97 36         113 return defined $PACKAGES{$pkg};
98              
99             }
100              
101             1;
102              
103             =head1 NAME
104              
105             Slackware::SBoKeeper::System - Slackware system information
106              
107             =head1 SYNOPSIS
108              
109             use Slackware::SBoKeeper::System;
110             ...
111              
112             =head1 DESCRIPTION
113              
114             Slackware::SBoKeeper::System is a module that provides miscellaneous
115             information about the Slackware system. This module is not meant to be used
116             outside of L. If you are looking for L user documentation,
117             please consult its manual.
118              
119             Slackware::SBoKeeper::System works similarly to L, it is meant to
120             be used like an object. That means the following subroutines should be invoked
121             as methods of C.
122              
123             =head1 METHODS
124              
125             =over 4
126              
127             =item load()
128              
129             Loads the Slackware package database. Automatically called when the module is
130             first loaded (if we're on a Slackware system). Can be be called again to reload
131             the database.
132              
133             =item is_slackware()
134              
135             Returns true if we're on a Slackware system, false if we're not.
136              
137             =item version()
138              
139             Returns Slackware system's version number, following the major.minor numbering
140             scheme. Returns C on non-Slackware systems.
141              
142             =item pkgtool_logs()
143              
144             Returns pkgtool log directory. C on non-Slackware systems.
145              
146             =item packages()
147              
148             Returns list of installed packages.
149              
150             =item packages_by_tag($tag)
151              
152             Returns list of installed packages with the tag C<$tag>. For example, to get
153             all SlackBuilds.org packages installed on your system:
154              
155             Slackware::SBoKeeper::System->packages_by_tag('_SBo');
156              
157             =item installed($pkg)
158              
159             Returns true if C<$pkg> is installed, false if not.
160              
161             =back
162              
163             =head1 FILES
164              
165             =over 4
166              
167             =item F
168              
169             File present on all Slackware systems that contains the Slackware version
170             number.
171              
172             =back
173              
174             =head1 AUTHOR
175              
176             Written by Samuel Young, Esamyoung12788@gmail.comE.
177              
178             =head1 COPYRIGHT
179              
180             Copyright (C) 2024-2025, Samuel Young
181              
182             This program is free software; you can redistribute it and/or modify it under
183             the terms of either: the GNU General Public License as published by the Free
184             Software Foundation; or the Artistic License.
185              
186             =head1 SEE ALSO
187              
188             L
189              
190             =cut