public inbox for development@lists.ipfire.org
 help / color / mirror / Atom feed
From: Michael Tremer <michael.tremer@ipfire.org>
To: development@lists.ipfire.org
Subject: Re: [PATCH 8/9] pakfire: Add getmetadata function
Date: Wed, 23 Mar 2022 11:28:41 +0000	[thread overview]
Message-ID: <7BA3A068-BD7C-4F3B-8D12-51ECA8A99EDD@ipfire.org> (raw)
In-Reply-To: <9c9b51236a4a2b13f01062dee6c001348cd2e387.camel@sicho.home>

[-- Attachment #1: Type: text/plain, Size: 15789 bytes --]

Hey,

> On 22 Mar 2022, at 13:28, Robin Roevens <robin.roevens(a)disroot.org> wrote:
> 
> 
> Michael Tremer schreef op ma 21-03-2022 om 16:32 [+0000]:
>> Hello,
>> 
>>> On 9 Mar 2022, at 22:56, Robin Roevens <robin.roevens(a)disroot.org>
>>> wrote:
>>> 
>>> - Added new getmetadata function for easy access to all available
>>>  metadata of a pak without knowledge about or need to parse
>>>  pakfire internal db files.
>>> - Added new 'pakfire info' functionality for displaying all
>>> available
>>>  metadata of (a) pak(s) to the user, using the new getmetadata.
>>> - Use getmetadata function in services.cgi to determine installed
>>>  addon services to display. Removing code duplication and intel
>>> that
>>>  should only be known by pakfire itself.
>>> 
>>> Signed-off-by: Robin Roevens <robin.roevens(a)disroot.org>
>>> ---
>>> html/cgi-bin/services.cgi    | 81 ++++++++++++++++++---------------
>>> ---
>>> src/pakfire/lib/functions.pl | 55 ++++++++++++++++++++++++
>>> src/pakfire/pakfire          | 58 ++++++++++++++++++++++++++
>>> 3 files changed, 154 insertions(+), 40 deletions(-)
>>> 
>>> diff --git a/html/cgi-bin/services.cgi b/html/cgi-bin/services.cgi
>>> index 237475735..896c95ec3 100644
>>> --- a/html/cgi-bin/services.cgi
>>> +++ b/html/cgi-bin/services.cgi
>>> @@ -29,6 +29,7 @@ require '/var/ipfire/general-functions.pl';
>>> require "${General::swroot}/lang.pl";
>>> require "${General::swroot}/header.pl";
>>> require "${General::swroot}/graphs.pl";
>>> +require "/opt/pakfire/lib/functions.pl";
>>> 
>>> my %color = ();
>>> my %mainsettings = ();
>>> @@ -160,51 +161,51 @@ END
>>> 
>>>         my $lines=0; # Used to count the outputlines to make
>>> different bgcolor
>>> 
>>> -       # Generate list of installed addon pak's
>>> -       opendir (DIR, "/opt/pakfire/db/installed") || die "Cannot
>>> opendir /opt/pakfire/db/installed/: $!";
>>> -       my @pak = sort readdir DIR;
>>> -       closedir(DIR);
>>> -
>>> -       foreach (@pak){
>>> -               chomp($_);
>>> -               next unless (m/^meta-/);
>>> -               s/^meta-//;
>>> -
>>> -               # Check which of the paks are services
>>> -               if (-e "/etc/init.d/$_") {
>>> -                       # blacklist some packages
>>> -                       #
>>> -                       # alsa has trouble with the volume saving
>>> and was not really stopped
>>> -                       # mdadm should not stopped with webif
>>> because this could crash the system
>>> -                       #
>>> -                       if ( $_ eq 'squid' ) {
>>> -                               next;
>>> -                       }
>>> -                       if ( ($_ ne "alsa") && ($_ ne "mdadm") ) {
>>> -                               $lines++;
>>> -                               if ($lines % 2){
>>> -                                       print "<tr>";
>>> -
>>>                                        $col="bgcolor='$color{'color2
>>> 2'}'";
>>> -                               }else{
>>> -                                       print "<tr>";
>>> -
>>>                                        $col="bgcolor='$color{'color2
>>> 0'}'";
>>> -                               }
>>> +       my @paks;
>>> +       my @addon_services;
>>> 
>>> -                               print "<td align='left' $col
>>> width='31%'>$_</td> ";
>>> -                               my $status = isautorun($_,$col);
>>> -                               print "$status ";
>>> -                               print "<td align='center' $col
>>> width='8%'><a href='services.cgi?$_!start'><img
>>> alt='$Lang::tr{'start'}' title='$Lang::tr{'start'}'
>>> src='/images/go-up.png' border='0' /></a></td>";
>>> -                               print "<td align='center' $col
>>> width='8%'><a href='services.cgi?$_!stop'><img
>>> alt='$Lang::tr{'stop'}' title='$Lang::tr{'stop'}' src='/images/go-
>>> down.png' border='0' /></a></td> ";
>>> -                               my $status =
>>> &isrunningaddon($_,$col);
>>> -                               $status =~ s/\^[\[[0-1]\;[0-9]+m//g;
>>> -
>>> -                               chomp($status);
>>> -                               print "$status";
>>> -                               print "</tr>";
>>> +       # Generate list of installed addon pak services
>>> +       my %paklist = &Pakfire::dblist("installed");
>>> +
>>> +       foreach my $pak (keys %paklist) {
>>> +               my %metadata = &Pakfire::getmetadata($pak,
>>> "installed");
>>> +                       
>>> +               if ("$metadata{'Services'}") {
>>> +                       foreach my $service (split(/ /,
>>> "$metadata{'Services'}")) {
>>> +                               push(@addon_services, $service);
>>>                         }
>>>                 }
>>>         }
>>> 
>>> +       foreach (@addon_services) {
>>> +               $lines++;
>>> +               if ($lines % 2){
>>> +                       print "<tr>";
>>> +                       $col="bgcolor='$color{'color22'}'";
>>> +               }else{
>>> +                       print "<tr>";
>>> +                       $col="bgcolor='$color{'color20'}'";
>>> +               }
>>> +               print "<td align='left' $col width='31%'>$_</td> ";
>>> +               my $status = isautorun($_,$col);
>>> +               print "$status ";
>>> +               # Don't allow user to start/stop folowing services
>>> from webui:
>>> +               #  - alsa has trouble with the volume saving and
>>> was not really stopped
>>> +               if ($_ eq "alsa") {
>>> +                       print "<td align='center' $col
>>> width='8%'></td>";
>>> +                       print "<td align='center' $col
>>> width='8%'></td> ";
>> 
>> Is this still a problem? Why do we even catch this here?
> Not sure.. Never tested it. I will try to set up a test env with a
> sound card .. not sure if I will succeed. never started a vm with sound
> here before .. So if someone else can test this more easily ?

VMs can easily emulate sound hardware, but I wouldn’t even bother here. This should just work and if it doesn’t that should be caught in the initscript and not in some random CGI file.

> 
>> 
>>> +               } else {
>>> +                       print "<td align='center' $col
>>> width='8%'><a href='services.cgi?$_!start'><img
>>> alt='$Lang::tr{'start'}' title='$Lang::tr{'start'}'
>>> src='/images/go-up.png' border='0' /></a></td>";
>>> +                       print "<td align='center' $col
>>> width='8%'><a href='services.cgi?$_!stop'><img
>>> alt='$Lang::tr{'stop'}' title='$Lang::tr{'stop'}' src='/images/go-
>>> down.png' border='0' /></a></td> ";
>>> +               }
>>> +               my $status = isrunningaddon($_,$col);
>>> +               $status =~ s/\^[\[[0-1]\;[0-9]+m//g;
>>> +
>>> +               chomp($status);
>>> +               print "$status";
>>> +               print "</tr>";
>>> +       }
>>> +
>>>         print "</table></div>\n";
>>>         &Header::closebox();
>>> 
>>> diff --git a/src/pakfire/lib/functions.pl
>>> b/src/pakfire/lib/functions.pl
>>> index 028a0277e..6dda8d688 100644
>>> --- a/src/pakfire/lib/functions.pl
>>> +++ b/src/pakfire/lib/functions.pl
>>> @@ -115,6 +115,7 @@ sub usage {
>>>   &Pakfire::message("               <update> - Contacts the servers
>>> for new lists of paks.");
>>>   &Pakfire::message("               <upgrade> - Installs the latest
>>> version of all paks.");
>>>   &Pakfire::message("               <list>
>>> [installed/notinstalled/upgrade] - Outputs a list with all,
>>> installed, available or upgradeable paks.");
>>> +  &Pakfire::message("               <info> <pak> [<pak> ...] -
>>> Output pak metadata.");
>>>   &Pakfire::message("               <status> - Outputs a summary
>>> about available core upgrades, updates and a required reboot");
>>>   &Pakfire::message("");
>>>   &Pakfire::message("       Global options:");
>>> @@ -674,6 +675,60 @@ sub parsemetafile {
>>>         return %metadata;
>>> }
>>> 
>>> +sub getmetadata {
>>> +       ### This subroutine returns a hash of available info for a
>>> package
>>> +       #   Pass package name and type of info as argument:
>>> Pakfire::getmetadata(package, type_of_info) 
>>> +       #       Type_of_info can be "latest" or "installed"
>>> +       #   Usage is always with two argument.
>>> +       my ($pak, $type) = @_;
>>> +
>>> +       my %metadata = (
>>> +               Name => $pak, 
>>> +               Installed => "no",
>>> +               Available => "no");
>>> +       my %installed_metadata = ();
>>> +
>>> +       my @templine;
>>> +       my @file;
>>> +
>>> +       ### Get available version information
>>> +       if ("$type" eq "latest") {
>>> +               ### Check if package is in packages_list and get
>>> latest available version
>>> +               my %db = Pakfire::dblist("all");
>>> +               
>>> +               if (defined $db{$pak}) {
>>> +                       ### Get and parse latest available metadata
>>> +                       if (getmetafile("$pak")) {
>>> +                               %metadata =
>>> parsemetafile("$Conf::dbdir/meta/meta-$pak");
>>> +
>>> +                               $metadata{'Available'} = "yes";
>>> +                               ### Rename version info fields
>>> +                               $metadata{'AvailableProgVersion'} =
>>> delete $metadata{'ProgVersion'};
>>> +                               $metadata{'AvailableRelease'} =
>>> delete $metadata{'Release'};
>> 
>> Why is the delete necessary?
> 
> This is actually a rename of the key from 'ProgVersion' to
> 'AvailableProgVersion'; delete removes the key from the hash, and
> returns its value, which is added to the hash as the new key
> 'Available...'. 
> If I would remove the delete, the same info would be in the hash, once
> in 'ProgVersion' and once in 'AvailableProgVersion'.
> And in case the pak is not installed, it would be returned as such from
> this function; while existence of 'ProgVersion'/'Release' sort of
> implicates that the pak is installed. (Checks should be based on
> 'Installed' = yes/no but it could lead to confusion to keep them)

Hmm, okay.

> 
>> 
>>> +                       }
>>> +               }
>>> +       }
>>> +       
>>> +       ### Parse installed pak metadata
>>> +       if (&isinstalled($pak) == 0) {
>>> +           %installed_metadata =
>>> parsemetafile("$Conf::dbdir/installed/meta-$pak");
>>> +
>>> +               if ("$type" eq "latest" &&
>>> exists($metadata{'AvailableProgVersion'})) {
>>> +                       ### Add installed version info to latest
>>> metadata
>>> +                       $metadata{'ProgVersion'} =
>>> $installed_metadata{'ProgVersion'};
>>> +                       $metadata{'Release'} =
>>> $installed_metadata{'Release'};
>>> +               } else {
>>> +                       ### Use metadata of installed pak
>>> +                       %metadata = %installed_metadata;
>>> +               }
>>> +               $metadata{'Installed'} = 'yes';
>>> +       } else {
>>> +               $metadata{'Installed'} = 'no';
>>> +       }
>>> +
>>> +       return %metadata;
>>> +}
>>> +
>>> sub decryptpak {
>>>         my $pak = shift;
>>> 
>>> diff --git a/src/pakfire/pakfire b/src/pakfire/pakfire
>>> index 0ed8aacd4..9935481a5 100644
>>> --- a/src/pakfire/pakfire
>>> +++ b/src/pakfire/pakfire
>>> @@ -387,6 +387,64 @@
>>>                 } else {
>>>                         &Pakfire::message("PAKFIRE WARN: No
>>> packages where found using filter $filter.");
>>>                 }
>>> +       } elsif ("$ARGV[0]" eq "info") {
>>> +               shift;
>>> +
>>> +               my @paks;
>>> +               my $pak;
>>> +               foreach $pak (@ARGV) {
>>> +                       unless ("$pak" =~ "^-") {
>>> +                               push(@paks,$pak);
>>> +                       }
>>> +               }
>>> +
>>> +               unless ("@paks") {
>>> +                       Pakfire::message("PAKFIRE ERROR: missing
>>> package name");
>>> +                       Pakfire::usage;
>>> +                       exit 1;
>>> +               }
>>> +
>>> +               foreach $pak (@paks) {
>>> +                       my %metadata = Pakfire::getmetadata($pak,
>>> "latest");
>>> +
>>> +                       ### Check if pakfile was actually found
>>> +                       if ($metadata{'Installed'} eq "no" &&
>>> $metadata{'Available'} eq "no") {
>>> +                               Pakfire::message("PAKFIRE WARN: Pak
>>> '$pak' not found.");
>>> +                               last;
>>> +                       }
>>> +
>>> +                       unless (defined $metadata{'Available'}) {
>>> +                               Pakfire::message("PAKFIRE WARN:
>>> Unable to retrieve latest metadata for $pak. Information may be
>>> outdated.")
>>> +                       }
>>> +
>>> +                       ### Printout metadata in a user friendly
>>> format
>>> +                       print "Name: $metadata{'Name'}\n";
>>> +                       print "Summary: $metadata{'Summary'}\n";
>>> +                       if ($metadata{'Available'} eq "yes") {
>>> +                               print "Version:
>>> $metadata{'AvailableProgVersion'}-$metadata{'AvailableRelease'}\n";
>>> +                       } else {
>>> +                               print "Version:
>>> $metadata{'ProgVersion'}-$metadata{'Release'}\n";
>>> +                       }
>>> +                       print "Size: " .
>>> Pakfire::beautifysize("$metadata{'Size'}") . "\n";
>>> +                       print "Dependencies:
>>> $metadata{'Dependencies'}\n";
>>> +                       print "Pakfile: $metadata{'File'}\n";
>>> +                       print "Service InitScripts:
>>> $metadata{'Services'}\n";
>>> +                       print "Installed:
>>> $metadata{'Installed'}\n";
>>> +                       ### Generate a pak status message
>>> +                       if (! defined $metadata{'Available'}) {
>>> +                               print "Status: unknown (an error
>>> occured retrieving latest pak metadata)";
>>> +                       } elsif ($metadata{'Available'} eq "no") {
>>> +                               print "Status: obsolete (version
>>> $metadata{'ProgVersion'}-$metadata{'Release'} is installed)\n";
>>> +                       } elsif ($metadata{'Installed'} eq "yes" &&
>>> "$metadata{'Release'}" < "$metadata{'AvailableRelease'}") {
>>> +                               print "Status: outdated (version
>>> $metadata{'ProgVersion'}-$metadata{'Release'} is installed)\n";
>>> +                       } elsif ($metadata{'Installed'} eq "yes") {
>>> +                               print "Status: up-to-date\n";
>>> +                       } else {
>>> +                               print "Status: not installed\n";
>>> +                       }
>>> +                       print "\n";
>>> +               }
>>> +
>>>         } elsif ("$ARGV[0]" eq "resolvedeps") {
>>>                 foreach (@ARGV) {
>>>                         next if ("$_" eq "resolvedeps");
>> 
>> This looks very good.
>> 
>>> -- 
>>> 2.34.1
>>> 
>>> 
>>> -- 
>>> Dit bericht is gescanned op virussen en andere gevaarlijke
>>> inhoud door MailScanner en lijkt schoon te zijn.
>>> 
>> 
>> 
> 
> -- 
> Dit bericht is gescanned op virussen en andere gevaarlijke
> inhoud door MailScanner en lijkt schoon te zijn.


  reply	other threads:[~2022-03-23 11:28 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-09 22:56 [PATCH 0/9] pakfire: remove dup. code + seperate ui/logic Robin Roevens
2022-03-09 22:56 ` [PATCH 1/9] pakfire: Refactor dblist seperating UI and logic Robin Roevens
2022-03-21 16:18   ` Michael Tremer
2022-03-22 12:39     ` Robin Roevens
2022-03-23 19:18       ` Robin Roevens
2022-03-23 20:49         ` Michael Tremer
2022-03-09 22:56 ` [PATCH 2/9] pakfire: Replace duplicate code with dblist functioncall Robin Roevens
2022-03-21 16:20   ` Michael Tremer
2022-03-09 22:56 ` [PATCH 3/9] pakfire: Replace dbgetlist duplicate code Robin Roevens
2022-03-21 16:21   ` Michael Tremer
2022-03-09 22:56 ` [PATCH 4/9] pakfire: Replace coreupdate_available " Robin Roevens
2022-03-21 16:21   ` Michael Tremer
2022-03-22 12:42     ` Robin Roevens
2022-03-23 21:50       ` Robin Roevens
2022-03-09 22:56 ` [PATCH 5/9] pakfire: Optimize upgradecore function Robin Roevens
2022-03-21 16:24   ` Michael Tremer
2022-03-22 12:58     ` Robin Roevens
2022-03-22 15:16       ` Michael Tremer
2022-03-09 22:56 ` [PATCH 6/9] pakfire: Add list upgrade functionality Robin Roevens
2022-03-21 16:33   ` Michael Tremer
2022-03-22 12:59     ` Robin Roevens
2022-03-09 22:56 ` [PATCH 7/9] pakfire: Refactor status function separating UI and logic Robin Roevens
2022-03-21 16:28   ` Michael Tremer
2022-03-23 19:56     ` Robin Roevens
2022-03-23 20:48       ` Michael Tremer
2022-03-09 22:56 ` [PATCH 8/9] pakfire: Add getmetadata function Robin Roevens
2022-03-21 16:32   ` Michael Tremer
2022-03-22 13:28     ` Robin Roevens
2022-03-23 11:28       ` Michael Tremer [this message]
2022-03-09 22:56 ` [PATCH 9/9] pakfire: Redesign update output and workflow Robin Roevens
2022-03-21 16:36   ` Michael Tremer
2022-03-22 18:32     ` Robin Roevens
2022-03-23 10:30       ` Michael Tremer
2022-03-09 23:46 ` [PATCH 0/9] pakfire: remove dup. code + seperate ui/logic Tom Rymes
2022-03-09 23:56   ` Paul Simmons

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7BA3A068-BD7C-4F3B-8D12-51ECA8A99EDD@ipfire.org \
    --to=michael.tremer@ipfire.org \
    --cc=development@lists.ipfire.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox