]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.factory/src/org/argeo/slc/rpmfactory/core/BuildInMock.java
1 package org
.argeo
.slc
.rpmfactory
.core
;
4 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Arrays
;
7 import java
.util
.HashSet
;
11 import org
.apache
.commons
.exec
.Executor
;
12 import org
.apache
.commons
.io
.FileUtils
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.slc
.SlcException
;
16 import org
.argeo
.slc
.rpmfactory
.RpmFactory
;
17 import org
.argeo
.slc
.runtime
.tasks
.SystemCall
;
19 /** Build an RPM in mock. */
20 public class BuildInMock
implements Runnable
{
21 private final static Log log
= LogFactory
.getLog(BuildInMock
.class);
22 private final static String NOARCH
= "noarch";
24 private String rpmPackage
= null;
25 private String branch
= null;
26 private String arch
= NOARCH
;
28 private RpmFactory rpmFactory
;
29 private Executor executor
;
31 private String debuginfoDirName
= "debuginfo";
32 private String mockExecutable
= "/usr/bin/mock";
34 private List
<String
> preBuildCommands
= new ArrayList
<String
>();
37 if (!rpmFactory
.isDeveloperInstance()) {
39 SystemCall mockClean
= createBaseMockCall();
40 mockClean
.arg("--init");
45 for (String preBuildCmd
: preBuildCommands
) {
46 SystemCall mockClean
= createBaseMockCall();
47 mockClean
.arg("--chroot").arg(preBuildCmd
);
52 SystemCall mockBuild
= createBaseMockCall();
53 mockBuild
.arg("--scm-enable");
54 mockBuild
.arg("--scm-option").arg("package=" + rpmPackage
);
55 mockBuild
.arg("--no-clean");
61 // copy RPMs to target directories
62 File stagingDir
= rpmFactory
.getWorkspaceDir(rpmFactory
63 .getStagingWorkspace());
64 File srpmDir
= new File(stagingDir
, "SRPMS");
67 File debuginfoDir
= null;
68 if (!arch
.equals(NOARCH
)) {
69 archDir
= new File(stagingDir
, arch
);
70 debuginfoDir
= new File(archDir
, debuginfoDirName
);
71 debuginfoDir
.mkdirs();
74 Set
<File
> reposToRecreate
= new HashSet
<File
>();
75 File resultDir
= rpmFactory
.getResultDir(arch
);
76 if (resultDir
.exists())
77 rpms
: for (File file
: resultDir
.listFiles()) {
78 if (file
.isDirectory())
82 if (file
.getName().contains(".src.rpm"))
83 targetDirs
= new File
[] { srpmDir
};
84 else if (file
.getName().contains("-debuginfo-"))
85 targetDirs
= new File
[] { debuginfoDir
};
86 else if (!arch
.equals(NOARCH
)
87 && file
.getName().contains("." + arch
+ ".rpm"))
88 targetDirs
= new File
[] { archDir
};
89 else if (file
.getName().contains(".noarch.rpm")) {
90 List
<File
> dirs
= new ArrayList
<File
>();
91 for (String arch
: rpmFactory
.getArchs())
92 dirs
.add(new File(stagingDir
, arch
));
93 targetDirs
= dirs
.toArray(new File
[dirs
.size()]);
94 } else if (file
.getName().contains(".rpm"))
95 throw new SlcException("Don't know where to copy " + file
);
97 if (log
.isTraceEnabled())
98 log
.trace("Skip " + file
);
102 reposToRecreate
.addAll(Arrays
.asList(targetDirs
));
103 copyToDirs(file
, targetDirs
);
106 // recreate changed repos
107 for (File repoToRecreate
: reposToRecreate
) {
108 SystemCall createrepo
= new SystemCall();
109 createrepo
.arg("createrepo");
111 createrepo
.arg("-d");
113 if (!repoToRecreate
.getName().equals(debuginfoDirName
))
114 createrepo
.arg("-x").arg(debuginfoDirName
+ "/*");
116 createrepo
.arg("-q");
117 createrepo
.arg(repoToRecreate
.getAbsolutePath());
119 createrepo
.setExecutor(executor
);
121 log
.info("Updated repo " + repoToRecreate
);
124 // index staging workspace
125 rpmFactory
.indexWorkspace(rpmFactory
.getStagingWorkspace());
128 /** Creates a mock call with all the common options such as config file etc. */
129 protected SystemCall
createBaseMockCall() {
130 String mockCfg
= rpmFactory
.getMockConfig(arch
);
131 File mockConfigFile
= rpmFactory
.getMockConfigFile(arch
, branch
);
134 SystemCall mock
= new SystemCall();
137 mock
.arg("setarch").arg(arch
);
138 mock
.arg(mockExecutable
);
140 mock
.arg("--configdir=" + mockConfigFile
.getAbsoluteFile().getParent());
142 mock
.arg("--arch=" + arch
);
143 mock
.arg("-r").arg(mockCfg
);
145 mock
.setLogCommand(true);
146 mock
.setExecutor(executor
);
151 protected void copyToDirs(File file
, File
[] dirs
) {
152 for (File dir
: dirs
) {
154 FileUtils
.copyFileToDirectory(file
, dir
);
155 if (log
.isDebugEnabled())
156 log
.debug(file
+ " => " + dir
);
157 } catch (IOException e
) {
158 throw new SlcException("Cannot copy " + file
+ " to " + dir
, e
);
163 public void setArch(String arch
) {
167 public void setRpmPackage(String rpmPackage
) {
168 this.rpmPackage
= rpmPackage
;
171 public void setBranch(String branch
) {
172 this.branch
= branch
;
175 public void setRpmFactory(RpmFactory env
) {
176 this.rpmFactory
= env
;
179 public void setExecutor(Executor executor
) {
180 this.executor
= executor
;
183 public void setMockExecutable(String mockExecutable
) {
184 this.mockExecutable
= mockExecutable
;
187 public void setPreBuildCommands(List
<String
> preBuildCommands
) {
188 this.preBuildCommands
= preBuildCommands
;