]>
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
;
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";
38 private String rpmPackage
= null;
39 private String branch
= null;
40 private String arch
= NOARCH
;
42 private RpmFactory factory
;
43 private Executor executor
;
45 private String debuginfoDirName
= "debuginfo";
46 private String mockExecutable
= "/usr/bin/mock";
48 private List
<String
> preBuildCommands
= new ArrayList
<String
>();
51 if (!factory
.isDeveloperInstance()) {
53 SystemCall mockClean
= createBaseMockCall();
54 mockClean
.arg("--init");
59 for (String preBuildCmd
: preBuildCommands
) {
60 SystemCall mockClean
= createBaseMockCall();
61 mockClean
.arg("--chroot").arg(preBuildCmd
);
66 SystemCall mockBuild
= createBaseMockCall();
67 mockBuild
.arg("--scm-enable");
68 mockBuild
.arg("--scm-option").arg("package=" + rpmPackage
);
69 mockBuild
.arg("--no-clean");
75 // copy RPMs to target directories
76 File stagingDir
= factory
77 .getWorkspaceDir(factory
.getStagingWorkspace());
78 File srpmDir
= new File(stagingDir
, "SRPMS");
81 File debuginfoDir
= null;
82 if (!arch
.equals(NOARCH
)) {
83 archDir
= new File(stagingDir
, arch
);
84 debuginfoDir
= new File(archDir
, debuginfoDirName
);
85 debuginfoDir
.mkdirs();
88 Set
<File
> reposToRecreate
= new HashSet
<File
>();
89 File resultDir
= factory
.getResultDir(arch
);
90 if (resultDir
.exists())
91 rpms
: for (File file
: resultDir
.listFiles()) {
92 if (file
.isDirectory())
96 if (file
.getName().contains(".src.rpm"))
97 targetDirs
= new File
[] { srpmDir
};
98 else if (file
.getName().contains("-debuginfo-"))
99 targetDirs
= new File
[] { debuginfoDir
};
100 else if (!arch
.equals(NOARCH
)
101 && file
.getName().contains("." + arch
+ ".rpm"))
102 targetDirs
= new File
[] { archDir
};
103 else if (file
.getName().contains(".noarch.rpm")) {
104 List
<File
> dirs
= new ArrayList
<File
>();
105 for (String arch
: factory
.getArchs())
106 dirs
.add(new File(stagingDir
, arch
));
107 targetDirs
= dirs
.toArray(new File
[dirs
.size()]);
108 } else if (file
.getName().contains(".rpm"))
109 throw new SlcException("Don't know where to copy " + file
);
111 if (log
.isTraceEnabled())
112 log
.trace("Skip " + file
);
116 reposToRecreate
.addAll(Arrays
.asList(targetDirs
));
117 copyToDirs(file
, targetDirs
);
120 // recreate changed repos
121 for (File repoToRecreate
: reposToRecreate
) {
122 SystemCall createrepo
= new SystemCall();
123 createrepo
.arg("createrepo");
125 createrepo
.arg("-d");
127 if (!repoToRecreate
.getName().equals(debuginfoDirName
))
128 createrepo
.arg("-x").arg(debuginfoDirName
+ "/*");
130 createrepo
.arg("-q");
131 createrepo
.arg(repoToRecreate
.getAbsolutePath());
133 createrepo
.setExecutor(executor
);
135 log
.info("Updated repo " + repoToRecreate
);
138 // index staging workspace
139 factory
.indexWorkspace(factory
.getStagingWorkspace());
142 /** Creates a mock call with all the common options such as config file etc. */
143 protected SystemCall
createBaseMockCall() {
144 String mockCfg
= factory
.getMockConfig(arch
);
145 File mockConfigFile
= factory
.getMockConfigFile(arch
, branch
);
148 SystemCall mock
= new SystemCall();
151 mock
.arg("setarch").arg(arch
);
152 mock
.arg(mockExecutable
);
154 mock
.arg("--configdir=" + mockConfigFile
.getAbsoluteFile().getParent());
156 mock
.arg("--arch=" + arch
);
157 mock
.arg("-r").arg(mockCfg
);
159 mock
.setLogCommand(true);
160 mock
.setExecutor(executor
);
165 protected void copyToDirs(File file
, File
[] dirs
) {
166 for (File dir
: dirs
) {
168 FileUtils
.copyFileToDirectory(file
, dir
);
169 if (log
.isDebugEnabled())
170 log
.debug(file
+ " => " + dir
);
171 } catch (IOException e
) {
172 throw new SlcException("Cannot copy " + file
+ " to " + dir
, e
);
177 public void setArch(String arch
) {
181 public void setRpmPackage(String rpmPackage
) {
182 this.rpmPackage
= rpmPackage
;
185 public void setBranch(String branch
) {
186 this.branch
= branch
;
189 public void setFactory(RpmFactory env
) {
193 public void setExecutor(Executor executor
) {
194 this.executor
= executor
;
197 public void setMockExecutable(String mockExecutable
) {
198 this.mockExecutable
= mockExecutable
;
201 public void setPreBuildCommands(List
<String
> preBuildCommands
) {
202 this.preBuildCommands
= preBuildCommands
;