1 package org
.argeo
.slc
.lib
.linux
.rpmfactory
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.io
.OutputStream
;
7 import java
.util
.ArrayList
;
10 import org
.apache
.commons
.exec
.Executor
;
11 import org
.apache
.commons
.io
.FileUtils
;
12 import org
.apache
.commons
.io
.IOUtils
;
13 import org
.argeo
.api
.cms
.CmsLog
;
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
;
19 /** Generates an SRPM from a spec file */
20 public class CreateSrpm
implements Runnable
{
21 private final static CmsLog log
= CmsLog
.getLog(CreateSrpm
.class);
25 /** Directory where to cache downloaded distributions. */
26 private File distributionCache
;
28 private Resource specFile
;
30 private RpmBuildEnvironment rpmBuildEnvironment
;
32 private Boolean overwriteSources
= false;
34 private File srpmFile
;
36 private Executor executor
;
39 File sourcesDir
= new File(topdir
, "SOURCES");
41 File specsDir
= new File(topdir
, "SPECS");
42 File srpmsDir
= new File(topdir
, "SRPMS");
45 // Parse spec file and copy required resources
46 RpmSpecFile spec
= new RpmSpecFile(specFile
);
47 copyToSources(spec
, sourcesDir
);
50 File targetFile
= new File(specsDir
, specFile
.getFilename())
52 copyResourceToFile(specFile
, targetFile
);
54 // Generate rpmbuild config files
55 rpmBuildEnvironment
.writeRpmbuildConfigFiles(topdir
);
59 SystemCall packageSrpm
= new SystemCall();
60 packageSrpm
.arg("rpmbuild");
61 packageSrpm
.arg("-bs").arg("--nodeps");
62 packageSrpm
.arg("--rcfile=rpmrc");
63 packageSrpm
.arg("--macros=" + RpmBuildEnvironment
.defaultMacroFiles
65 // buildSrpm.arg("-D", "_topdir " + topdir.getCanonicalPath() + "");
66 packageSrpm
.arg("SPECS/" + specFile
.getFilename());
67 packageSrpm
.setExecDir(topdir
.getCanonicalPath());
68 packageSrpm
.setLogCommand(true);
71 packageSrpm
.setExecutor(executor
);
72 String answer
= packageSrpm
.function();
74 // Extract generated SRPM path
75 // TODO: make it safer
76 String srpmPath
= answer
.split(":")[1].trim();
77 srpmFile
= new File(srpmPath
);
78 } catch (IOException e
) {
79 throw new SlcException("Cannot generate SRPM from " + specFile
, e
);
84 protected void copyToSources(RpmSpecFile spec
, File sourcesDir
) {
86 List
<Resource
> toCopyToSources
= new ArrayList
<Resource
>();
87 List
<Resource
> toDownload
= new ArrayList
<Resource
>();
88 for (String file
: spec
.getSources().values()) {
92 res
= specFile
.createRelative("../SOURCES/" + file
);
94 res
= new UrlResource(file
);
96 } catch (Exception e
) {
97 res
= new UrlResource(file
);
100 toCopyToSources
.add(res
);
101 } catch (Exception e
) {
102 log
.error("Cannot interpret " + file
, e
);
105 for (String file
: spec
.getPatches().values()) {
109 res
= specFile
.createRelative("../SOURCES/" + file
);
111 res
= new UrlResource(file
);
113 } catch (Exception e
) {
114 res
= new UrlResource(file
);
117 toCopyToSources
.add(res
);
118 } catch (Exception e
) {
119 log
.error("Cannot interpret " + file
, e
);
123 // FIXME: we may have missed some files here
124 for (Resource res
: toCopyToSources
) {
126 if (distributionCache
!= null && toDownload
.contains(res
)) {
127 if (!distributionCache
.exists())
128 distributionCache
.mkdirs();
129 targetDir
= distributionCache
;
130 if (log
.isDebugEnabled())
131 log
.debug("Cache " + res
+ " in " + targetDir
);
133 targetDir
= sourcesDir
;
134 File targetFile
= new File(targetDir
, res
.getFilename())
136 if (!targetFile
.exists() || overwriteSources
)
137 copyResourceToFile(res
, targetFile
);
138 if (!targetDir
.equals(sourcesDir
)) {
139 File fileInSourcesDir
= new File(sourcesDir
,
140 targetFile
.getName());
141 if (!fileInSourcesDir
.exists()
142 || !(fileInSourcesDir
.length() == targetFile
144 FileUtils
.copyFile(targetFile
, fileInSourcesDir
);
147 } catch (Exception e
) {
148 throw new SlcException("Cannot copy to " + sourcesDir
, e
);
152 private static void copyResourceToFile(Resource res
, File targetFile
) {
154 if (targetFile
.equals(res
.getFile())) {
155 if (log
.isDebugEnabled())
156 log
.debug("Target identical to source, skipping... "
157 + targetFile
+ " <=> " + res
);
160 } catch (IOException e1
) {
164 OutputStream out
= null;
165 InputStream in
= null;
167 out
= FileUtils
.openOutputStream(targetFile
);
168 in
= res
.getInputStream();
169 IOUtils
.copy(in
, out
);
170 if (log
.isDebugEnabled())
171 log
.debug("Copied " + targetFile
+ " from " + res
);
172 } catch (Exception e
) {
173 throw new SlcException("Cannot copy " + res
+ " to " + targetFile
,
176 IOUtils
.closeQuietly(in
);
177 IOUtils
.closeQuietly(out
);
182 public void setSpecFile(Resource specFile
) {
183 this.specFile
= specFile
;
186 public void setTopdir(File topdir
) {
187 this.topdir
= topdir
;
190 public void setOverwriteSources(Boolean overwriteSources
) {
191 this.overwriteSources
= overwriteSources
;
194 public File
getSrpmFile() {
198 public void setRpmBuildEnvironment(RpmBuildEnvironment rpmBuildEnvironment
) {
199 this.rpmBuildEnvironment
= rpmBuildEnvironment
;
202 public void setDistributionCache(File distributionCache
) {
203 this.distributionCache
= distributionCache
;
206 public void setExecutor(Executor executor
) {
207 this.executor
= executor
;