]> git.argeo.org Git - gpl/argeo-jcr.git/blob - rpmfactory/core/BuildInMock.java
Prepare next development cycle
[gpl/argeo-jcr.git] / rpmfactory / core / BuildInMock.java
1 package org.argeo.slc.rpmfactory.core;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Set;
10
11 import org.apache.commons.exec.Executor;
12 import org.apache.commons.io.FileUtils;
13 import org.argeo.api.cms.CmsLog;
14 import org.argeo.slc.SlcException;
15 import org.argeo.slc.rpmfactory.RpmFactory;
16 import org.argeo.slc.runtime.tasks.SystemCall;
17
18 /** Build an RPM in mock. */
19 public class BuildInMock implements Runnable {
20 private final static CmsLog log = CmsLog.getLog(BuildInMock.class);
21 private final static String NOARCH = "noarch";
22
23 private String rpmPackage = null;
24 private String branch = null;
25 private String arch = NOARCH;
26
27 private RpmFactory rpmFactory;
28 private Executor executor;
29
30 private String debuginfoDirName = "debuginfo";
31 private String mockExecutable = "/usr/bin/mock";
32
33 private List<String> preBuildCommands = new ArrayList<String>();
34
35 public void run() {
36 if (!rpmFactory.isDeveloperInstance()) {
37 // clean/init
38 SystemCall mockClean = createBaseMockCall();
39 mockClean.arg("--init");
40 mockClean.run();
41 }
42
43 // pre build commands
44 for (String preBuildCmd : preBuildCommands) {
45 SystemCall mockClean = createBaseMockCall();
46 mockClean.arg("--chroot").arg(preBuildCmd);
47 mockClean.run();
48 }
49
50 // actual build
51 SystemCall mockBuild = createBaseMockCall();
52 mockBuild.arg("--scm-enable");
53 mockBuild.arg("--scm-option").arg("package=" + rpmPackage);
54 mockBuild.arg("--no-clean");
55 //
56 //
57 mockBuild.run();
58 //
59
60 // copy RPMs to target directories
61 File stagingDir = rpmFactory.getWorkspaceDir(rpmFactory
62 .getStagingWorkspace());
63 File srpmDir = new File(stagingDir, "SRPMS");
64 srpmDir.mkdirs();
65 File archDir = null;
66 File debuginfoDir = null;
67 if (!arch.equals(NOARCH)) {
68 archDir = new File(stagingDir, arch);
69 debuginfoDir = new File(archDir, debuginfoDirName);
70 debuginfoDir.mkdirs();
71 }
72
73 Set<File> reposToRecreate = new HashSet<File>();
74 File resultDir = rpmFactory.getResultDir(arch);
75 if (resultDir.exists())
76 rpms: for (File file : resultDir.listFiles()) {
77 if (file.isDirectory())
78 continue rpms;
79
80 File[] targetDirs;
81 if (file.getName().contains(".src.rpm"))
82 targetDirs = new File[] { srpmDir };
83 else if (file.getName().contains("-debuginfo-"))
84 targetDirs = new File[] { debuginfoDir };
85 else if (!arch.equals(NOARCH)
86 && file.getName().contains("." + arch + ".rpm"))
87 targetDirs = new File[] { archDir };
88 else if (file.getName().contains(".noarch.rpm")) {
89 List<File> dirs = new ArrayList<File>();
90 for (String arch : rpmFactory.getArchs())
91 dirs.add(new File(stagingDir, arch));
92 targetDirs = dirs.toArray(new File[dirs.size()]);
93 } else if (file.getName().contains(".rpm"))
94 throw new SlcException("Don't know where to copy " + file);
95 else {
96 if (log.isTraceEnabled())
97 log.trace("Skip " + file);
98 continue rpms;
99 }
100
101 reposToRecreate.addAll(Arrays.asList(targetDirs));
102 copyToDirs(file, targetDirs);
103 }
104
105 // recreate changed repos
106 for (File repoToRecreate : reposToRecreate) {
107 SystemCall createrepo = new SystemCall();
108 createrepo.arg("createrepo");
109 // sqllite db
110 createrepo.arg("-d");
111 // debuginfo
112 if (!repoToRecreate.getName().equals(debuginfoDirName))
113 createrepo.arg("-x").arg(debuginfoDirName + "/*");
114 // quiet
115 createrepo.arg("-q");
116 createrepo.arg(repoToRecreate.getAbsolutePath());
117
118 createrepo.setExecutor(executor);
119 createrepo.run();
120 log.info("Updated repo " + repoToRecreate);
121 }
122
123 // index staging workspace
124 rpmFactory.indexWorkspace(rpmFactory.getStagingWorkspace());
125 }
126
127 /** Creates a mock call with all the common options such as config file etc. */
128 protected SystemCall createBaseMockCall() {
129 String mockCfg = rpmFactory.getMockConfig(arch);
130 File mockConfigFile = rpmFactory.getMockConfigFile(arch, branch);
131
132 // prepare mock call
133 SystemCall mock = new SystemCall();
134
135 if (arch != null)
136 mock.arg("setarch").arg(arch);
137 mock.arg(mockExecutable);
138 mock.arg("-v");
139 mock.arg("--configdir=" + mockConfigFile.getAbsoluteFile().getParent());
140 if (arch != null)
141 mock.arg("--arch=" + arch);
142 mock.arg("-r").arg(mockCfg);
143
144 mock.setLogCommand(true);
145 mock.setExecutor(executor);
146
147 return mock;
148 }
149
150 protected void copyToDirs(File file, File[] dirs) {
151 for (File dir : dirs) {
152 try {
153 FileUtils.copyFileToDirectory(file, dir);
154 if (log.isDebugEnabled())
155 log.debug(file + " => " + dir);
156 } catch (IOException e) {
157 throw new SlcException("Cannot copy " + file + " to " + dir, e);
158 }
159 }
160 }
161
162 public void setArch(String arch) {
163 this.arch = arch;
164 }
165
166 public void setRpmPackage(String rpmPackage) {
167 this.rpmPackage = rpmPackage;
168 }
169
170 public void setBranch(String branch) {
171 this.branch = branch;
172 }
173
174 public void setRpmFactory(RpmFactory env) {
175 this.rpmFactory = env;
176 }
177
178 public void setExecutor(Executor executor) {
179 this.executor = executor;
180 }
181
182 public void setMockExecutable(String mockExecutable) {
183 this.mockExecutable = mockExecutable;
184 }
185
186 public void setPreBuildCommands(List<String> preBuildCommands) {
187 this.preBuildCommands = preBuildCommands;
188 }
189
190 }