]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.factory/src/org/argeo/slc/rpmfactory/core/YumListParser.java
1b44dd494f9bca8227a120dd6292d7d36922c1be
1 package org
.argeo
.slc
.rpmfactory
.core
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.util
.StringTokenizer
;
7 import java
.util
.TreeSet
;
9 import org
.apache
.commons
.io
.FilenameUtils
;
10 import org
.apache
.commons
.io
.IOUtils
;
11 import org
.apache
.commons
.io
.LineIterator
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
14 import org
.argeo
.slc
.SlcException
;
15 import org
.springframework
.core
.io
.Resource
;
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 Log log
= LogFactory
.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 Resource yumListOutput
;
32 if (yumListOutput
!= null) {
33 load(yumListOutput
.getInputStream());
34 if (log
.isDebugEnabled())
35 log
.debug(installed
.size() + " installed, "
36 + installable
.size() + " installable, from "
39 } catch (IOException e
) {
40 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
) {
53 Boolean readingInstalled
= false;
54 Boolean readingAvailable
= false;
55 LineIterator it
= IOUtils
.lineIterator(in
, "UTF-8");
56 while (it
.hasNext()) {
57 String line
= it
.nextLine();
58 if (line
.trim().equals("Installed Packages")) {
59 readingInstalled
= true;
60 } else if (line
.trim().equals("Available Packages")) {
61 readingAvailable
= true;
62 readingInstalled
= false;
63 } else if (readingAvailable
) {
64 if (Character
.isLetterOrDigit(line
.charAt(0))) {
65 installable
.add(extractRpmName(line
));
67 } else if (readingInstalled
) {
68 if (Character
.isLetterOrDigit(line
.charAt(0))) {
69 installed
.add(extractRpmName(line
));
73 } catch (IOException e
) {
74 throw new SlcException("Cannot load yum list output", e
);
76 IOUtils
.closeQuietly(in
);
80 protected String
extractRpmName(String line
) {
81 StringTokenizer st
= new StringTokenizer(line
, " \t");
82 String packageName
= st
.nextToken();
83 // consider the arch as an extension
84 return FilenameUtils
.getBaseName(packageName
);
85 //return packageName.split("\\.")[0];
88 public Set
<String
> getInstalled() {
92 public Set
<String
> getInstallable() {
96 public void setYumListOutput(Resource yumListOutput
) {
97 this.yumListOutput
= yumListOutput
;