]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.rpmfactory/src/main/java/org/argeo/slc/rpmfactory/core/BuildInMock.java
Add developer git base url
[gpl/argeo-slc.git] / runtime / org.argeo.slc.rpmfactory / src / main / java / org / argeo / slc / rpmfactory / core / 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 private String mockExecutable = "/usr/bin/mock";
47
48 public void run() {
49 String mockCfg = factory.getMockConfig(arch);
50 File mockConfigFile = factory.getMockConfigFile(arch, branch);
51
52 // prepare mock call
53 SystemCall mock = new SystemCall();
54 if (arch != null)
55 mock.arg("setarch").arg(arch);
56 mock.arg(mockExecutable);
57 mock.arg("-v");
58 mock.arg("--configdir=" + mockConfigFile.getAbsoluteFile().getParent());
59 if (arch != null)
60 mock.arg("--arch=" + arch);
61 mock.arg("-r").arg(mockCfg);
62 mock.arg("--scm-enable");
63 // mock.arg("--scm-option");
64 // mock.arg("git_get='git clone " + (branch != null ? "-b " + branch :
65 // "")
66 // + " " + factory.getGitBaseUrl() + "/SCM_PKG SCM_PKG'");
67 mock.arg("--scm-option").arg("package=" + rpmPackage);
68
69 mock.setLogCommand(true);
70 mock.setExecutor(executor);
71
72 //
73 // mock command execution
74 //
75 mock.run();
76 //
77
78 File stagingDir = factory
79 .getWorkspaceDir(factory.getStagingWorkspace());
80 File srpmDir = new File(stagingDir, "SRPMS");
81 srpmDir.mkdirs();
82 File archDir = null;
83 File debuginfoDir = null;
84 if (!arch.equals(NOARCH)) {
85 archDir = new File(stagingDir, arch);
86 debuginfoDir = new File(archDir, debuginfoDirName);
87 debuginfoDir.mkdirs();
88 }
89
90 // copy RPMs
91 Set<File> reposToRecreate = new HashSet<File>();
92 File resultDir = factory.getResultDir(arch);
93 if (resultDir.exists())
94 rpms: for (File file : resultDir.listFiles()) {
95 if (file.isDirectory())
96 continue rpms;
97
98 File[] targetDirs;
99 if (file.getName().contains(".src.rpm"))
100 targetDirs = new File[] { srpmDir };
101 else if (file.getName().contains("-debuginfo-"))
102 targetDirs = new File[] { debuginfoDir };
103 else if (!arch.equals(NOARCH)
104 && file.getName().contains("." + arch + ".rpm"))
105 targetDirs = new File[] { archDir };
106 else if (file.getName().contains(".noarch.rpm")) {
107 List<File> dirs = new ArrayList<File>();
108 for (String arch : factory.getArchs())
109 dirs.add(new File(stagingDir, arch));
110 targetDirs = dirs.toArray(new File[dirs.size()]);
111 } else if (file.getName().contains(".rpm"))
112 throw new SlcException("Don't know where to copy " + file);
113 else {
114 if (log.isTraceEnabled())
115 log.trace("Skip " + file);
116 continue rpms;
117 }
118
119 reposToRecreate.addAll(Arrays.asList(targetDirs));
120 copyToDirs(file, targetDirs);
121 }
122
123 // recreate changed repos
124 for (File repoToRecreate : reposToRecreate) {
125 SystemCall createrepo = new SystemCall();
126 createrepo.arg("createrepo");
127 // sqllite db
128 createrepo.arg("-d");
129 // debuginfo
130 if (!repoToRecreate.getName().equals(debuginfoDirName))
131 createrepo.arg("-x").arg(debuginfoDirName + "/*");
132 // quiet
133 createrepo.arg("-q");
134 createrepo.arg(repoToRecreate.getAbsolutePath());
135
136 createrepo.setExecutor(executor);
137 createrepo.run();
138 log.info("Updated repo " + repoToRecreate);
139 }
140
141 // index staging workspace
142 factory.indexWorkspace(factory.getStagingWorkspace());
143 }
144
145 protected void copyToDirs(File file, File[] dirs) {
146 for (File dir : dirs) {
147 try {
148 FileUtils.copyFileToDirectory(file, dir);
149 if (log.isDebugEnabled())
150 log.debug(file + " => " + dir);
151 } catch (IOException e) {
152 throw new SlcException("Cannot copy " + file + " to " + dir, e);
153 }
154 }
155 }
156
157 public void setArch(String arch) {
158 this.arch = arch;
159 }
160
161 public void setRpmPackage(String rpmPackage) {
162 this.rpmPackage = rpmPackage;
163 }
164
165 public void setBranch(String branch) {
166 this.branch = branch;
167 }
168
169 public void setFactory(RpmFactory env) {
170 this.factory = env;
171 }
172
173 public void setExecutor(Executor executor) {
174 this.executor = executor;
175 }
176
177 public void setMockExecutable(String mockExecutable) {
178 this.mockExecutable = mockExecutable;
179 }
180
181 }