]>
git.argeo.org Git - gpl/argeo-jcr.git/blob - rpmfactory/core/YumListParser.java
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
.argeo
.api
.cms
.CmsLog
;
15 import org
.argeo
.slc
.SlcException
;
18 * Reads the output of a 'yum list all' command and interpret the list of
21 public class YumListParser
implements RpmPackageSet
{
22 private final static CmsLog log
= CmsLog
.getLog(YumListParser
.class);
24 private Set
<String
> installed
= new TreeSet
<String
>();
25 /** Not installed but available */
26 private Set
<String
> installable
= new TreeSet
<String
>();
28 private Path yumListOutput
;
31 if (yumListOutput
!= null) {
32 try (InputStream in
= Files
.newInputStream(yumListOutput
)) {
34 if (log
.isDebugEnabled())
35 log
.debug(installed
.size() + " installed, " + installable
.size() + " installable, from "
37 } catch (IOException e
) {
38 throw new SlcException("Cannot initialize yum list parser", e
);
43 public Boolean
contains(String packageName
) {
44 if (installed
.contains(packageName
))
47 return installable
.contains(packageName
);
50 protected void load(InputStream in
) throws IOException
{
51 Boolean readingInstalled
= false;
52 Boolean readingAvailable
= false;
53 LineIterator it
= IOUtils
.lineIterator(in
, "UTF-8");
54 while (it
.hasNext()) {
55 String line
= it
.nextLine();
56 if (line
.trim().equals("Installed Packages")) {
57 readingInstalled
= true;
58 } else if (line
.trim().equals("Available Packages")) {
59 readingAvailable
= true;
60 readingInstalled
= false;
61 } else if (readingAvailable
) {
62 if (Character
.isLetterOrDigit(line
.charAt(0))) {
63 installable
.add(extractRpmName(line
));
65 } else if (readingInstalled
) {
66 if (Character
.isLetterOrDigit(line
.charAt(0))) {
67 installed
.add(extractRpmName(line
));
73 protected String
extractRpmName(String line
) {
74 StringTokenizer st
= new StringTokenizer(line
, " \t");
75 String packageName
= st
.nextToken();
76 // consider the arch as an extension
77 return FilenameUtils
.getBaseName(packageName
);
78 // return packageName.split("\\.")[0];
81 public Set
<String
> getInstalled() {
85 public Set
<String
> getInstallable() {
89 public void setYumListOutput(Path yumListOutput
) {
90 this.yumListOutput
= yumListOutput
;