]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/linux/rpmfactory/CreateSrpm.java
Fix parameters not properly passed
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / lib / linux / rpmfactory / CreateSrpm.java
1 package org.argeo.slc.lib.linux.rpmfactory;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import org.apache.commons.io.FileUtils;
11 import org.apache.commons.io.IOUtils;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.argeo.slc.SlcException;
15 import org.argeo.slc.core.execution.tasks.SystemCall;
16 import org.springframework.core.io.Resource;
17 import org.springframework.core.io.UrlResource;
18
19 /** Generates an SRPM from a spec file */
20 public class CreateSrpm implements Runnable {
21 private final static Log log = LogFactory.getLog(CreateSrpm.class);
22
23 private File topdir;
24
25 /** Directory where to cache downloaded dsitributions. */
26 private File distributionCache;
27
28 private Resource specFile;
29
30 private RpmBuildEnvironment rpmBuildEnvironment;
31
32 private Boolean overwriteSources = false;
33
34 private File srpmFile;
35
36 public void run() {
37 File sourcesDir = new File(topdir, "SOURCES");
38 sourcesDir.mkdirs();
39 File specsDir = new File(topdir, "SPECS");
40 File srpmsDir = new File(topdir, "SRPMS");
41
42 try {
43 // Parse spec file and copy required resources
44 RpmSpecFile spec = new RpmSpecFile(specFile);
45 copyToSources(spec, sourcesDir);
46
47 // Copy spec file
48 File targetFile = new File(specsDir, specFile.getFilename())
49 .getCanonicalFile();
50 copyResourceToFile(specFile, targetFile);
51
52 // Generate rpmbuild config files
53 rpmBuildEnvironment.writeRpmbuildConfigFiles(topdir);
54
55 // Build SRPM
56 srpmsDir.mkdirs();
57 SystemCall packageSrpm = new SystemCall();
58 packageSrpm.arg("rpmbuild");
59 packageSrpm.arg("-bs").arg("--nodeps");
60 packageSrpm.arg("--rcfile=rpmrc");
61 // buildSrpm.arg("-D", "_topdir " + topdir.getCanonicalPath() + "");
62 packageSrpm.arg("SPECS/" + specFile.getFilename());
63 packageSrpm.setExecDir(topdir.getCanonicalPath());
64 packageSrpm.setLogCommand(true);
65
66 // Execute
67 String answer = packageSrpm.function();
68
69 // Extract generated SRPM path
70 // TODO: make it safer
71 String srpmPath = answer.split(":")[1].trim();
72 srpmFile = new File(srpmPath);
73 } catch (IOException e) {
74 throw new SlcException("Cannot generate SRPM from " + specFile, e);
75 }
76
77 }
78
79 protected void copyToSources(RpmSpecFile spec, File sourcesDir) {
80 try {
81 List<Resource> toCopyToSources = new ArrayList<Resource>();
82 List<Resource> toDownload = new ArrayList<Resource>();
83 for (String file : spec.getSources().values()) {
84 try {
85 Resource res;
86 try {
87 res = specFile.createRelative("../SOURCES/" + file);
88 if (!res.exists())
89 res = new UrlResource(file);
90
91 } catch (Exception e) {
92 res = new UrlResource(file);
93 toDownload.add(res);
94 }
95 toCopyToSources.add(res);
96 } catch (Exception e) {
97 log.error("Cannot interpret " + file, e);
98 }
99 }
100 for (String file : spec.getPatches().values()) {
101 try {
102 Resource res;
103 try {
104 res = specFile.createRelative("../SOURCES/" + file);
105 if (!res.exists()) {
106 res = new UrlResource(file);
107 }
108 } catch (Exception e) {
109 res = new UrlResource(file);
110 toDownload.add(res);
111 }
112 toCopyToSources.add(res);
113 } catch (Exception e) {
114 log.error("Cannot interpret " + file, e);
115 }
116 }
117
118 // FIXME: we may have missed some files here
119 for (Resource res : toCopyToSources) {
120 File targetDir;
121 if (distributionCache != null && toDownload.contains(res)) {
122 if (!distributionCache.exists())
123 distributionCache.mkdirs();
124 targetDir = distributionCache;
125 if (log.isDebugEnabled())
126 log.debug("Cache " + res + " in " + targetDir);
127 } else
128 targetDir = sourcesDir;
129 File targetFile = new File(targetDir, res.getFilename())
130 .getCanonicalFile();
131 if (!targetFile.exists() || overwriteSources)
132 copyResourceToFile(res, targetFile);
133 if (!targetDir.equals(sourcesDir)) {
134 File fileInSourcesDir = new File(sourcesDir, targetFile
135 .getName());
136 if (!fileInSourcesDir.exists()
137 || !(fileInSourcesDir.length() == targetFile
138 .length()))
139 FileUtils.copyFile(targetFile, fileInSourcesDir);
140 }
141 }
142 } catch (Exception e) {
143 throw new SlcException("Cannot copy to " + sourcesDir, e);
144 }
145 }
146
147 private static void copyResourceToFile(Resource res, File targetFile) {
148 try {
149 if (targetFile.equals(res.getFile())) {
150 if (log.isDebugEnabled())
151 log.debug("Target identical to source, skipping... "
152 + targetFile + " <=> " + res);
153 return;
154 }
155 } catch (IOException e1) {
156 // silent
157 }
158
159 OutputStream out = null;
160 InputStream in = null;
161 try {
162 out = FileUtils.openOutputStream(targetFile);
163 in = res.getInputStream();
164 IOUtils.copy(in, out);
165 if (log.isDebugEnabled())
166 log.debug("Copied " + targetFile + " from " + res);
167 } catch (Exception e) {
168 throw new SlcException("Cannot copy " + res + " to " + targetFile,
169 e);
170 } finally {
171 IOUtils.closeQuietly(in);
172 IOUtils.closeQuietly(out);
173 }
174
175 }
176
177 public void setSpecFile(Resource specFile) {
178 this.specFile = specFile;
179 }
180
181 public void setTopdir(File topdir) {
182 this.topdir = topdir;
183 }
184
185 public void setOverwriteSources(Boolean overwriteSources) {
186 this.overwriteSources = overwriteSources;
187 }
188
189 public File getSrpmFile() {
190 return srpmFile;
191 }
192
193 public void setRpmBuildEnvironment(RpmBuildEnvironment rpmBuildEnvironment) {
194 this.rpmBuildEnvironment = rpmBuildEnvironment;
195 }
196
197 public void setDistributionCache(File distributionCache) {
198 this.distributionCache = distributionCache;
199 }
200
201 }