Hi Monks!
I have a utility which iterates over a set of OS packages and finds the Zypper repo's name (using zypper info) and then it's URI and name (using zypper repos). The code looks like:
while(my ($package) = keys(%found_packages)) { delete($found_packages{$package}); # Ignore already handled packages next if (defined($checked_packages{$package})); $checked_packages{$package} = 1; # Find the zypper information of the OS package my $cmd = "$ZYPPER info $package"; my ($stdout, $stderr, $exit_status) = capture { system($cmd); }; if ($exit_status == 0 && $stdout =~ /^Repository\s*:\s*(\S+)\s*$/m) +{ my $repository_name = $1; if ($repository_name eq '@System') { if (defined($packages_href->{$repository_name})) { $packages_href->{$repository_name}{'packages'}{$package}++; } else { $packages_href->{$repository_name} = { 'packages' => { $package => 1 } }; } } else { if (defined($packages_href->{$repository_name})) { $packages_href->{$repository_name}{'packages'}{$package}++; } else { $cmd = "$ZYPPER repos -u $repository_name 2> $DEV_NULL"; ($stdout, $stderr, $exit_status) = capture { system($cmd); }; if ($exit_status) { print("Failed to find the information of the zypper reposito +ry: $repository_name"); } else { my $repository_alias = $1 if ($stdout =~ /^Alias\s*:\s*(\S+) +\s*$/m); my $repository_uri = $1 if ($stdout =~ /^URI\s*:\s*(\S+)\s +*$/m); if (is_empty_str($repository_alias) || is_empty_str($reposit +ory_uri)) { print("Failed to find the uri or alias of the zypper repos +itory: $repository_name"); next; } $packages_href->{$repository_name} = { 'alias' => $repository_alias, 'uri' => $repository_uri, 'packages' => { $package => 1 } }; } } } } }
I noticed that the invokation of the "zyyper info" command is really slow. I also noticed that you can provide a list of packages and it will print the output for each one. For example:
$ /usr/bin/zypper info python-rpm-macros Mesa-libEGL1 libedit0 fontcon +fig-devel Loading repository data... Reading installed packages... Information for package python-rpm-macros: ------------------------------------------ Repository : prod-sdk12-sp5-pool-x86_64-hpc Name : python-rpm-macros Version : 20200207.5feb6c1-3.19.1 Arch : noarch Vendor : SUSE LLC <https://www.suse.com/> Support Level : unknown Installed Size : 51.6 KiB Installed : Yes (automatically) Status : up-to-date Source package : python-rpm-macros-20200207.5feb6c1-3.19.1.src Summary : RPM macros for building of Python modules Description : This package contains SUSE RPM macros for Python build automation. You should BuildRequire this package unless you are sure that you are only building for distros newer than Leap 42.2 Information for package Mesa-libEGL1: ------------------------------------- Repository : prod-sp5-pool-x86_64-hpc Name : Mesa-libEGL1 Version : 18.3.2-14.3.2 Arch : x86_64 Vendor : SUSE LLC <https://www.suse.com/> Support Level : unknown Installed Size : 196.9 KiB Installed : Yes (automatically) Status : up-to-date Source package : Mesa-18.3.2-14.3.2.src Summary : EGL API implementation Description : This package contains the EGL native platform graphics interface library. EGL provides a platform-agnostic mechanism for creating rendering surfaces for use with other graphics libraries, such as OpenGL|ES and OpenVG. This package contains modules to interface with the existing syste +m GLX or DRI2 drivers to provide OpenGL via EGL. The Mesa main packa +ge provides drivers to provide hardware-accelerated OpenGL|ES and Ope +nVG support. Information for package libedit0: --------------------------------- Repository : prod-sp5-pool-x86_64-hpc Name : libedit0 Version : 3.1.snap20140620-1.13 Arch : x86_64 Vendor : SUSE LLC <https://www.suse.com/> Support Level : unknown Installed Size : 248.8 KiB Installed : Yes (automatically) Status : up-to-date Source package : libedit-3.1.snap20140620-1.13.src Summary : Command Line Editing and History Library Description : libedit is a command line editing and history library. It is desig +ned to be used by interactive programs that allow the user to type com +mands at a terminal prompt. Information for package fontconfig-devel: ----------------------------------------- Repository : prod-sdk12-sp5-pool-x86_64-hpc Name : fontconfig-devel Version : 2.11.1-7.1 Arch : x86_64 Vendor : SUSE LLC <https://www.suse.com/> Support Level : unknown Installed Size : 896.1 KiB Installed : Yes (automatically) Status : up-to-date Source package : fontconfig-2.11.1-7.1.src Summary : Include Files and Libraries mandatory for Development Description : This package countains all include files, libraries, configuration files needed for compiling applications which use the fontconfig library. In addition, it contains extensive documentation and manual pages +for developers using the library. Fontconfig is a library for configuring and customizing font acces +s. It contains two essential modules, the configuration module which bui +lds an internal configuration from XML files and the matching module w +hich accepts font patterns and returns the nearest matching font.
I'm trying to add the whole list of packages into the zypper info command and then iterate over each such block and extract the repository name and on each such name get the alias and URI. I'm struggling with the parsing part. Is it possible to get a suggestion on a how to parse the output of zypper info?

In reply to How to parse the zypper info output? by ovedpo15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.