]> git.argeo.org Git - gpl/argeo-slc.git/blob - BuildInMock.java
50daabce3f3d0c1a5bb1e2cbdf77f8394ac55570
[gpl/argeo-slc.git] / BuildInMock.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.rpmfactory.core;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import org.apache.commons.exec.Executor;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.argeo.slc.SlcException;
31 import org.argeo.slc.core.execution.tasks.SystemCall;
32
33 /** Build an RPM in mock. */
34 public class BuildInMock implements Runnable {
35 private final static Log log = LogFactory.getLog(BuildInMock.class);
36 private final static String NOARCH = "noarch";
37
38 private String rpmPackage = null;
39 private String branch = null;
40 private String arch = NOARCH;
41
42 private RpmFactory factory;
43 private Executor executor;
44
45 private String debuginfoDirName = "debuginfo";
46
47 public void run() {
48 String mockCfg = factory.getMockConfig(arch);
49 File mockConfigFile = factory.getMockConfigFile(arch, branch);
50
51 // prepare mock call
52 SystemCall mock = new SystemCall();
53 if (arch != null)
54 mock.arg("setarch").arg(arch);
55 mock.arg("mock");
56 mock.arg("-v");
57 mock.arg("--configdir=" + mockConfigFile.getAbsoluteFile().getParent());
58 if (arch != null)
59 mock.arg("--arch=" + arch);
60 mock.arg("-r").arg(mockCfg);
61 mock.arg("--scm-enable");
62 // mock.arg("--scm-option");
63 // mock.arg("git_get='git clone " + (branch != null ? "-b " + branch :
64 // "")
65 // + " " + factory.getGitBaseUrl() + "/SCM_PKG SCM_PKG'");
66 mock.arg("--scm-option").arg("package=" + rpmPackage);
67
68 mock.setLogCommand(true);
69 mock.setExecutor(executor);
70
71 //
72 // mock command execution
73 //
74 mock.run();
75 //
76
77 File stagingDir = factory
78 .getWorkspaceDir(factory.getStagingWorkspace());
79 File srpmDir = new File(stagingDir, "SRPMS");
80 srpmDir.mkdirs();
81 File archDir = null;
82 File debuginfoDir = null;
83 if (!arch.equals(NOARCH)) {
84 archDir = new File(stagingDir, arch);
85 debuginfoDir = new File(archDir, debuginfoDirName);
86 debuginfoDir.mkdirs();
87 }
88
89 // copy RPMs
90 Set<File> reposToRecreate = new HashSet<File>();
91 File resultDir = factory.getResultDir(arch);
92 rpms: for (File file : resultDir.listFiles()) {
93 if (file.isDirectory())
94 continue rpms;
95
96 File[] targetDirs;
97 if (file.getName().contains(".src.rpm"))
98 targetDirs = new File[] { srpmDir };
99 else if (file.getName().contains("-debuginfo-"))
100 targetDirs = new File[] { debuginfoDir };
101 else if (!arch.equals(NOARCH)
102 && file.getName().contains("." + arch + ".rpm"))
103 targetDirs = new File[] { archDir };
104 else if (file.getName().contains(".noarch.rpm")) {
105 List<File> dirs = new ArrayList<File>();
106 for (String arch : factory.getArchs())
107 dirs.add(new File(stagingDir, arch));
108 targetDirs = dirs.toArray(new File[dirs.size()]);
109 } else if (file.getName().contains(".rpm"))
110 throw new SlcException("Don't know where to copy " + file);
111 else {
112 if (log.isTraceEnabled())
113 log.trace("Skip " + file);
114 continue rpms;
115 }
116
117 reposToRecreate.addAll(Arrays.asList(targetDirs));
118 copyToDirs(file, targetDirs);
119 }
120
121 // recreate changed repos
122 for (File repoToRecreate : reposToRecreate) {
123 SystemCall createrepo = new SystemCall();
124 createrepo.arg("createrepo");
125 // sqllite db
126 createrepo.arg("-d");
127 // debuginfo
128 if (!repoToRecreate.getName().equals(debuginfoDirName))
129 createrepo.arg("-x").arg(debuginfoDirName + "/*");
130 // quiet
131 createrepo.arg("-q");
132 createrepo.arg(repoToRecreate.getAbsolutePath());
133
134 createrepo.setExecutor(executor);
135 createrepo.run();
136 log.info("Updated repo " + repoToRecreate);
137 }
138
139 // index staging workspace
140 factory.indexWorkspace(factory.getStagingWorkspace());
141 }
142
143 protected void copyToDirs(File file, File[] dirs) {
144 for (File dir : dirs) {
145 try {
146 FileUtils.copyFileToDirectory(file, dir);
147 if (log.isDebugEnabled())
148 log.debug(file + " => " + dir);
149 } catch (IOException e) {
150 throw new SlcException("Cannot copy " + file + " to " + dir, e);
151 }
152 }
153 }
154
155 public void setArch(String arch) {
156 this.arch = arch;
157 }
158
159 public void setRpmPackage(String rpmPackage) {
160 this.rpmPackage = rpmPackage;
161 }
162
163 public void setBranch(String branch) {
164 this.branch = branch;
165 }
166
167 public void setFactory(RpmFactory env) {
168 this.factory = env;
169 }
170
171 public void setExecutor(Executor executor) {
172 this.executor = executor;
173 }
174 }