1 package org
.argeo
.slc
.rpmfactory
.core
;
4 import java
.io
.InputStream
;
6 import java
.util
.StringTokenizer
;
9 import javax
.jcr
.Session
;
10 import javax
.jcr
.nodetype
.NodeType
;
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
;
21 * Gather RPMs from various sources (local builds or third party) into a
22 * consistent distributable set (typically to be used to generate an ISO).
24 public class CreateRpmDistribution
implements Runnable
{
25 private final static Log log
= LogFactory
26 .getLog(CreateRpmDistribution
.class);
28 private RpmFactory rpmFactory
;
29 private RpmDistribution rpmDistribution
;
31 private String arch
= "x86_64";
33 private String repoqueryExecutable
= "/usr/bin/repoquery";
37 Session session
= null;
38 // Reader reader = null;
40 Node baseFolder
= rpmFactory
.newDistribution(rpmDistribution
42 session
= baseFolder
.getSession();
43 Node targetFolder
= baseFolder
.addNode(arch
, NodeType
.NT_FOLDER
);
45 SystemCall repoquery
= new SystemCall();
46 repoquery
.arg(repoqueryExecutable
);
48 File yumConfigFile
= rpmFactory
.getYumRepoFile(arch
);
49 repoquery
.arg("-c", yumConfigFile
.getAbsolutePath());
50 repoquery
.arg("--requires");
51 repoquery
.arg("--resolve");
52 repoquery
.arg("--location");
54 for (String rpmPackage
: rpmDistribution
.getPackages())
55 repoquery
.arg(rpmPackage
);
57 if (log
.isDebugEnabled())
58 log
.debug("Command:\n" + repoquery
.asCommand());
60 String output
= repoquery
.function();
62 if (log
.isDebugEnabled())
63 log
.debug(output
+ "\n");
64 // reader = new StringReader(output);
65 StringTokenizer lines
= new StringTokenizer(output
, "\n");
66 // List<String> dependencies = IOUtils.readLines(reader);
67 dependencies
: while (lines
.hasMoreTokens()) {
68 String urlStr
= lines
.nextToken();
69 InputStream in
= null;
71 URL url
= new URL(urlStr
);
72 String fileName
= FilenameUtils
.getName(url
.getFile());
73 String
[] tokens
= fileName
.split("-");
74 if (tokens
.length
< 3)
75 continue dependencies
;
76 StringBuilder buf
= new StringBuilder();
77 for (int i
= 0; i
< tokens
.length
- 2; i
++) {
80 buf
.append(tokens
[i
]);
83 String packageName
= buf
.toString();
84 for (RpmPackageSet excluded
: rpmDistribution
85 .getExcludedPackages()) {
86 if (excluded
.contains(packageName
)) {
87 if (log
.isDebugEnabled())
88 log
.debug("Skipped " + packageName
);
89 continue dependencies
;// skip
92 in
= url
.openStream();
93 JcrUtils
.copyStreamAsFile(targetFolder
, fileName
, in
);
94 targetFolder
.getSession().save();
95 if (log
.isDebugEnabled())
96 log
.debug("Copied " + packageName
);
97 } catch (Exception e
) {
98 log
.error("Cannot copy " + urlStr
, e
);
100 IOUtils
.closeQuietly(in
);
105 File workspaceDir
= rpmFactory
.getWorkspaceDir(rpmDistribution
107 SystemCall createrepo
= new SystemCall();
108 createrepo
.arg("createrepo");
109 createrepo
.arg("-q");
110 createrepo
.arg("-d");
111 File archDir
= new File(workspaceDir
.getPath()
112 + targetFolder
.getPath());
113 createrepo
.arg(archDir
.getAbsolutePath());
115 } catch (Exception e
) {
116 throw new SlcException("Cannot generate distribution "
117 + rpmDistribution
.getId(), e
);
119 JcrUtils
.logoutQuietly(session
);
120 // IOUtils.closeQuietly(reader);
124 public void setRpmDistribution(RpmDistribution rpmDistribution
) {
125 this.rpmDistribution
= rpmDistribution
;
128 public void setRpmFactory(RpmFactory rpmFactory
) {
129 this.rpmFactory
= rpmFactory
;
132 public void setArch(String arch
) {
136 public void setRepoqueryExecutable(String yumdownloaderExecutable
) {
137 this.repoqueryExecutable
= yumdownloaderExecutable
;