]> git.argeo.org Git - gpl/argeo-jcr.git/blob - rpmfactory/core/YumListParser.java
Prepare next development cycle
[gpl/argeo-jcr.git] / 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.nio.file.Files;
6 import java.nio.file.Path;
7 import java.util.Set;
8 import java.util.StringTokenizer;
9 import java.util.TreeSet;
10
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;
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 CmsLog log = CmsLog.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 Path yumListOutput;
29
30 public void init() {
31 if (yumListOutput != null) {
32 try (InputStream in = Files.newInputStream(yumListOutput)) {
33 load(in);
34 if (log.isDebugEnabled())
35 log.debug(installed.size() + " installed, " + installable.size() + " installable, from "
36 + yumListOutput);
37 } catch (IOException e) {
38 throw new SlcException("Cannot initialize yum list parser", e);
39 }
40 }
41 }
42
43 public Boolean contains(String packageName) {
44 if (installed.contains(packageName))
45 return true;
46 else
47 return installable.contains(packageName);
48 }
49
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));
64 }
65 } else if (readingInstalled) {
66 if (Character.isLetterOrDigit(line.charAt(0))) {
67 installed.add(extractRpmName(line));
68 }
69 }
70 }
71 }
72
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];
79 }
80
81 public Set<String> getInstalled() {
82 return installed;
83 }
84
85 public Set<String> getInstallable() {
86 return installable;
87 }
88
89 public void setYumListOutput(Path yumListOutput) {
90 this.yumListOutput = yumListOutput;
91 }
92
93 }