]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.rpmfactory/src/main/java/org/argeo/slc/rpmfactory/core/BuildInMock.java
2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.rpmfactory
.core
;
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
;
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 import org
.argeo
.slc
.rpmfactory
.RpmFactory
;
34 /** Build an RPM in mock. */
35 public class BuildInMock
implements Runnable
{
36 private final static Log log
= LogFactory
.getLog(BuildInMock
.class);
37 private final static String NOARCH
= "noarch";
39 private String rpmPackage
= null;
40 private String branch
= null;
41 private String arch
= NOARCH
;
43 private RpmFactory rpmFactory
;
44 private Executor executor
;
46 private String debuginfoDirName
= "debuginfo";
47 private String mockExecutable
= "/usr/bin/mock";
49 private List
<String
> preBuildCommands
= new ArrayList
<String
>();
52 if (!rpmFactory
.isDeveloperInstance()) {
54 SystemCall mockClean
= createBaseMockCall();
55 mockClean
.arg("--init");
60 for (String preBuildCmd
: preBuildCommands
) {
61 SystemCall mockClean
= createBaseMockCall();
62 mockClean
.arg("--chroot").arg(preBuildCmd
);
67 SystemCall mockBuild
= createBaseMockCall();
68 mockBuild
.arg("--scm-enable");
69 mockBuild
.arg("--scm-option").arg("package=" + rpmPackage
);
70 mockBuild
.arg("--no-clean");
76 // copy RPMs to target directories
77 File stagingDir
= rpmFactory
.getWorkspaceDir(rpmFactory
78 .getStagingWorkspace());
79 File srpmDir
= new File(stagingDir
, "SRPMS");
82 File debuginfoDir
= null;
83 if (!arch
.equals(NOARCH
)) {
84 archDir
= new File(stagingDir
, arch
);
85 debuginfoDir
= new File(archDir
, debuginfoDirName
);
86 debuginfoDir
.mkdirs();
89 Set
<File
> reposToRecreate
= new HashSet
<File
>();
90 File resultDir
= rpmFactory
.getResultDir(arch
);
91 if (resultDir
.exists())
92 rpms
: for (File file
: resultDir
.listFiles()) {
93 if (file
.isDirectory())
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
: rpmFactory
.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
);
112 if (log
.isTraceEnabled())
113 log
.trace("Skip " + file
);
117 reposToRecreate
.addAll(Arrays
.asList(targetDirs
));
118 copyToDirs(file
, targetDirs
);
121 // recreate changed repos
122 for (File repoToRecreate
: reposToRecreate
) {
123 SystemCall createrepo
= new SystemCall();
124 createrepo
.arg("createrepo");
126 createrepo
.arg("-d");
128 if (!repoToRecreate
.getName().equals(debuginfoDirName
))
129 createrepo
.arg("-x").arg(debuginfoDirName
+ "/*");
131 createrepo
.arg("-q");
132 createrepo
.arg(repoToRecreate
.getAbsolutePath());
134 createrepo
.setExecutor(executor
);
136 log
.info("Updated repo " + repoToRecreate
);
139 // index staging workspace
140 rpmFactory
.indexWorkspace(rpmFactory
.getStagingWorkspace());
143 /** Creates a mock call with all the common options such as config file etc. */
144 protected SystemCall
createBaseMockCall() {
145 String mockCfg
= rpmFactory
.getMockConfig(arch
);
146 File mockConfigFile
= rpmFactory
.getMockConfigFile(arch
, branch
);
149 SystemCall mock
= new SystemCall();
152 mock
.arg("setarch").arg(arch
);
153 mock
.arg(mockExecutable
);
155 mock
.arg("--configdir=" + mockConfigFile
.getAbsoluteFile().getParent());
157 mock
.arg("--arch=" + arch
);
158 mock
.arg("-r").arg(mockCfg
);
160 mock
.setLogCommand(true);
161 mock
.setExecutor(executor
);
166 protected void copyToDirs(File file
, File
[] dirs
) {
167 for (File dir
: dirs
) {
169 FileUtils
.copyFileToDirectory(file
, dir
);
170 if (log
.isDebugEnabled())
171 log
.debug(file
+ " => " + dir
);
172 } catch (IOException e
) {
173 throw new SlcException("Cannot copy " + file
+ " to " + dir
, e
);
178 public void setArch(String arch
) {
182 public void setRpmPackage(String rpmPackage
) {
183 this.rpmPackage
= rpmPackage
;
186 public void setBranch(String branch
) {
187 this.branch
= branch
;
190 public void setRpmFactory(RpmFactory env
) {
191 this.rpmFactory
= env
;
194 public void setExecutor(Executor executor
) {
195 this.executor
= executor
;
198 public void setMockExecutable(String mockExecutable
) {
199 this.mockExecutable
= mockExecutable
;
202 public void setPreBuildCommands(List
<String
> preBuildCommands
) {
203 this.preBuildCommands
= preBuildCommands
;