]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.rpmfactory/src/main/java/org/argeo/slc/rpmfactory/core/YumListParser.java
Add developer git base url
[gpl/argeo-slc.git] / runtime / org.argeo.slc.rpmfactory / src / main / java / org / argeo / slc / rpmfactory / core / YumListParser.java
1 package org.argeo.slc.rpmfactory.core;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Set;
6 import java.util.StringTokenizer;
7 import java.util.TreeSet;
8
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;
16
17 /**
18 * Reads the output of a 'yum list all' command and interpret the list of
19 * packages.
20 */
21 public class YumListParser implements RpmPackageSet {
22 private final static Log log = LogFactory.getLog(YumListParser.class);
23
24 private Set<String> installed = new TreeSet<String>();
25 /** Not installed but available */
26 private Set<String> installable = new TreeSet<String>();
27
28 private Resource yumListOutput;
29
30 public void init() {
31 try {
32 if (yumListOutput != null) {
33 load(yumListOutput.getInputStream());
34 if (log.isDebugEnabled())
35 log.debug(installed.size() + " installed, "
36 + installable.size() + " installable, from "
37 + yumListOutput);
38 }
39 } catch (IOException e) {
40 throw new SlcException("Cannot initialize yum list parser", e);
41 }
42 }
43
44 public Boolean contains(String packageName) {
45 if (installed.contains(packageName))
46 return true;
47 else
48 return installable.contains(packageName);
49 }
50
51 protected void load(InputStream in) {
52 try {
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));
66 }
67 } else if (readingInstalled) {
68 if (Character.isLetterOrDigit(line.charAt(0))) {
69 installed.add(extractRpmName(line));
70 }
71 }
72 }
73 } catch (IOException e) {
74 throw new SlcException("Cannot load yum list output", e);
75 } finally {
76 IOUtils.closeQuietly(in);
77 }
78 }
79
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];
86 }
87
88 public Set<String> getInstalled() {
89 return installed;
90 }
91
92 public Set<String> getInstallable() {
93 return installable;
94 }
95
96 public void setYumListOutput(Resource yumListOutput) {
97 this.yumListOutput = yumListOutput;
98 }
99
100 }