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