]> git.argeo.org Git - gpl/argeo-slc.git/blob - core/CreateRpmDistribution.java
Prepare next development cycle
[gpl/argeo-slc.git] / core / CreateRpmDistribution.java
1 package org.argeo.slc.rpmfactory.core;
2
3 import java.io.File;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.util.StringTokenizer;
7
8 import javax.jcr.Node;
9 import javax.jcr.Session;
10 import javax.jcr.nodetype.NodeType;
11
12 import org.apache.commons.io.FilenameUtils;
13 import org.apache.commons.io.IOUtils;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.argeo.jcr.JcrUtils;
17 import org.argeo.slc.SlcException;
18 import org.argeo.slc.core.execution.tasks.SystemCall;
19 import org.springframework.core.io.ByteArrayResource;
20
21 /**
22 * Gather RPMs from various sources (local builds or third party) into a
23 * consistent distributable set (typically to be used to generate an ISO).
24 */
25 public class CreateRpmDistribution implements Runnable {
26 private final static Log log = LogFactory
27 .getLog(CreateRpmDistribution.class);
28
29 private RpmFactory rpmFactory;
30 private RpmDistribution rpmDistribution;
31
32 private String arch = "x86_64";
33
34 private String repoqueryExecutable = "/usr/bin/repoquery";
35
36 @Override
37 public void run() {
38 Session session = null;
39 // Reader reader = null;
40 try {
41 Node baseFolder = rpmFactory.newDistribution(rpmDistribution
42 .getId());
43 session = baseFolder.getSession();
44 Node targetFolder = baseFolder.addNode(arch, NodeType.NT_FOLDER);
45
46 SystemCall repoquery = new SystemCall();
47 repoquery.arg(repoqueryExecutable);
48
49 File yumConfigFile = rpmFactory.getYumConfigFile(arch);
50 repoquery.arg("-c", yumConfigFile.getAbsolutePath());
51 repoquery.arg("--requires");
52 repoquery.arg("--resolve");
53 repoquery.arg("--location");
54
55 for (String rpmPackage : rpmDistribution.getPackages())
56 repoquery.arg(rpmPackage);
57
58 if (log.isDebugEnabled())
59 log.debug("Command:\n" + repoquery.asCommand());
60
61 String output = repoquery.function();
62
63 if (log.isDebugEnabled())
64 log.debug(output + "\n");
65 // reader = new StringReader(output);
66 StringTokenizer lines = new StringTokenizer(output, "\n");
67 // List<String> dependencies = IOUtils.readLines(reader);
68 dependencies: while (lines.hasMoreTokens()) {
69 String urlStr = lines.nextToken();
70 InputStream in = null;
71 try {
72 URL url = new URL(urlStr);
73 String fileName = FilenameUtils.getName(url.getFile());
74 String[] tokens = fileName.split("-");
75 if (tokens.length < 3)
76 continue dependencies;
77 StringBuilder buf = new StringBuilder();
78 for (int i = 0; i < tokens.length - 2; i++) {
79 if (i != 0)
80 buf.append('-');
81 buf.append(tokens[i]);
82
83 }
84 String packageName = buf.toString();
85 for (RpmPackageSet excluded : rpmDistribution
86 .getExcludedPackages()) {
87 if (excluded.contains(packageName)) {
88 if (log.isDebugEnabled())
89 log.debug("Skipped " + packageName);
90 continue dependencies;// skip
91 }
92 }
93 in = url.openStream();
94 JcrUtils.copyStreamAsFile(targetFolder, fileName, in);
95 targetFolder.getSession().save();
96 if (log.isDebugEnabled())
97 log.debug("Copied " + packageName);
98 } catch (Exception e) {
99 log.error("Cannot copy " + urlStr, e);
100 } finally {
101 IOUtils.closeQuietly(in);
102 }
103 }
104
105 } catch (Exception e) {
106 throw new SlcException("Cannot generate distribution "
107 + rpmDistribution.getId(), e);
108 } finally {
109 JcrUtils.logoutQuietly(session);
110 // IOUtils.closeQuietly(reader);
111 }
112 }
113
114 public void setRpmDistribution(RpmDistribution rpmDistribution) {
115 this.rpmDistribution = rpmDistribution;
116 }
117
118 public void setRpmFactory(RpmFactory rpmFactory) {
119 this.rpmFactory = rpmFactory;
120 }
121
122 public void setArch(String arch) {
123 this.arch = arch;
124 }
125
126 public void setRepoqueryExecutable(String yumdownloaderExecutable) {
127 this.repoqueryExecutable = yumdownloaderExecutable;
128 }
129
130 }