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
.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
;
19 /** Generates an SRPM from a spec file */
20 public class CreateSrpm
implements Runnable
{
21 private final static Log log
= LogFactory
.getLog(CreateSrpm
.class);
25 /** Directory where to cache downloaded dsitributions. */
26 private File distributionCache
;
28 private Resource specFile
;
30 private RpmBuildEnvironment rpmBuildEnvironment
;
32 private Boolean overwriteSources
= false;
34 private File srpmFile
;
37 File sourcesDir
= new File(topdir
, "SOURCES");
39 File specsDir
= new File(topdir
, "SPECS");
40 File srpmsDir
= new File(topdir
, "SRPMS");
43 // Parse spec file and copy required resources
44 RpmSpecFile spec
= new RpmSpecFile(specFile
);
45 copyToSources(spec
, sourcesDir
);
48 File targetFile
= new File(specsDir
, specFile
.getFilename())
50 copyResourceToFile(specFile
, targetFile
);
52 // Generate rpmbuild config files
53 rpmBuildEnvironment
.writeRpmbuildConfigFiles(topdir
);
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);
67 String answer
= packageSrpm
.function();
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
);
79 protected void copyToSources(RpmSpecFile spec
, File sourcesDir
) {
81 List
<Resource
> toCopyToSources
= new ArrayList
<Resource
>();
82 List
<Resource
> toDownload
= new ArrayList
<Resource
>();
83 for (String file
: spec
.getSources().values()) {
87 res
= specFile
.createRelative("../SOURCES/" + file
);
89 res
= new UrlResource(file
);
91 } catch (Exception e
) {
92 res
= new UrlResource(file
);
95 toCopyToSources
.add(res
);
96 } catch (Exception e
) {
97 log
.error("Cannot interpret " + file
, e
);
100 for (String file
: spec
.getPatches().values()) {
104 res
= specFile
.createRelative("../SOURCES/" + file
);
106 res
= new UrlResource(file
);
108 } catch (Exception e
) {
109 res
= new UrlResource(file
);
112 toCopyToSources
.add(res
);
113 } catch (Exception e
) {
114 log
.error("Cannot interpret " + file
, e
);
118 // FIXME: we may have missed some files here
119 for (Resource res
: toCopyToSources
) {
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
);
128 targetDir
= sourcesDir
;
129 File targetFile
= new File(targetDir
, res
.getFilename())
131 if (!targetFile
.exists() || overwriteSources
)
132 copyResourceToFile(res
, targetFile
);
133 if (!targetDir
.equals(sourcesDir
)) {
134 File fileInSourcesDir
= new File(sourcesDir
, targetFile
136 if (!fileInSourcesDir
.exists()
137 || !(fileInSourcesDir
.length() == targetFile
139 FileUtils
.copyFile(targetFile
, fileInSourcesDir
);
142 } catch (Exception e
) {
143 throw new SlcException("Cannot copy to " + sourcesDir
, e
);
147 private static void copyResourceToFile(Resource res
, File targetFile
) {
149 if (targetFile
.equals(res
.getFile())) {
150 if (log
.isDebugEnabled())
151 log
.debug("Target identical to source, skipping... "
152 + targetFile
+ " <=> " + res
);
155 } catch (IOException e1
) {
159 OutputStream out
= null;
160 InputStream in
= null;
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
,
171 IOUtils
.closeQuietly(in
);
172 IOUtils
.closeQuietly(out
);
177 public void setSpecFile(Resource specFile
) {
178 this.specFile
= specFile
;
181 public void setTopdir(File topdir
) {
182 this.topdir
= topdir
;
185 public void setOverwriteSources(Boolean overwriteSources
) {
186 this.overwriteSources
= overwriteSources
;
189 public File
getSrpmFile() {
193 public void setRpmBuildEnvironment(RpmBuildEnvironment rpmBuildEnvironment
) {
194 this.rpmBuildEnvironment
= rpmBuildEnvironment
;
197 public void setDistributionCache(File distributionCache
) {
198 this.distributionCache
= distributionCache
;