]>
git.argeo.org Git - gpl/argeo-slc.git/blob - YumListParser.java
ee89aa311cabdcab34e6e7fee4e74470443517ac
1 package org
.argeo
.slc
.rpmfactory
.core
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.nio
.file
.Files
;
6 import java
.nio
.file
.Path
;
8 import java
.util
.StringTokenizer
;
9 import java
.util
.TreeSet
;
11 import org
.apache
.commons
.io
.FilenameUtils
;
12 import org
.apache
.commons
.io
.IOUtils
;
13 import org
.apache
.commons
.io
.LineIterator
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.slc
.SlcException
;
19 * Reads the output of a 'yum list all' command and interpret the list of
22 public class YumListParser
implements RpmPackageSet
{
23 private final static Log log
= LogFactory
.getLog(YumListParser
.class);
25 private Set
<String
> installed
= new TreeSet
<String
>();
26 /** Not installed but available */
27 private Set
<String
> installable
= new TreeSet
<String
>();
29 private Path yumListOutput
;
32 if (yumListOutput
!= null) {
33 try (InputStream in
= Files
.newInputStream(yumListOutput
)) {
35 if (log
.isDebugEnabled())
36 log
.debug(installed
.size() + " installed, " + installable
.size() + " installable, from "
38 } catch (IOException e
) {
39 throw new SlcException("Cannot initialize yum list parser", e
);
44 public Boolean
contains(String packageName
) {
45 if (installed
.contains(packageName
))
48 return installable
.contains(packageName
);
51 protected void load(InputStream in
) throws IOException
{
52 Boolean readingInstalled
= false;
53 Boolean readingAvailable
= false;
54 LineIterator it
= IOUtils
.lineIterator(in
, "UTF-8");
55 while (it
.hasNext()) {
56 String line
= it
.nextLine();
57 if (line
.trim().equals("Installed Packages")) {
58 readingInstalled
= true;
59 } else if (line
.trim().equals("Available Packages")) {
60 readingAvailable
= true;
61 readingInstalled
= false;
62 } else if (readingAvailable
) {
63 if (Character
.isLetterOrDigit(line
.charAt(0))) {
64 installable
.add(extractRpmName(line
));
66 } else if (readingInstalled
) {
67 if (Character
.isLetterOrDigit(line
.charAt(0))) {
68 installed
.add(extractRpmName(line
));
74 protected String
extractRpmName(String line
) {
75 StringTokenizer st
= new StringTokenizer(line
, " \t");
76 String packageName
= st
.nextToken();
77 // consider the arch as an extension
78 return FilenameUtils
.getBaseName(packageName
);
79 // return packageName.split("\\.")[0];
82 public Set
<String
> getInstalled() {
86 public Set
<String
> getInstallable() {
90 public void setYumListOutput(Path yumListOutput
) {
91 this.yumListOutput
= yumListOutput
;