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
.lib
.linux
.rpmfactory
;
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 /** Rebuild an SRPM in mock. (Historical) Replaces the build-mock.sh script. */
34 public class BuildInMock
implements Runnable
{
35 private final static Log log
= LogFactory
.getLog(BuildInMock
.class);
37 /** Mock flavour provided by the EPEL repository */
38 public final static String EPEL
= "EPEL";
39 /** Mock flavour provided by CentOS until v5 */
40 public final static String CENTOS
= "CENTOS";
42 public final static String NOARCH
= "noarch";
44 private String mockVar
= "/var/lib/mock";
46 private String mockFlavour
= EPEL
;
47 private String mockConfig
= null;
49 private String repository
;
50 private String release
= null;
51 private String level
= null;
52 private String arch
= NOARCH
;
54 private String rpmPackage
= null;
56 private Boolean mkdirs
= true;
58 private RpmBuildEnvironment buildEnvironment
;
59 private Executor executor
;
61 private String debuginfoDirName
= "debuginfo";
64 // TODO check if caller is in mock group
66 String cfgId
= repository
+ "-" + release
+ "-" + arch
;
67 String cfg
= mockConfig
!= null ? mockConfig
: "slc/" + cfgId
;
70 SystemCall mock
= new SystemCall();
72 mock
.arg("setarch").arg(arch
);
74 if (mockFlavour
.equals(EPEL
))
76 else if (mockFlavour
.equals(CENTOS
))
79 mock
.arg("--arch=" + arch
);
80 mock
.arg("-r").arg(cfg
);
82 mock
.arg("--scm-enable");
83 mock
.arg("--scm-option").arg("package=" + rpmPackage
);
85 mock
.setLogCommand(true);
87 // mock command execution
88 mock
.setExecutor(executor
);
91 // File repoDir = new File(buildEnvironment.getStagingBase() + "/"
92 // + repository + "/" + level + "/" + release);
93 File repoDir
= new File(buildEnvironment
.getStagingBase() + "/"
94 + repository
+ "-" + release
+ "-staging");
95 File srpmDir
= new File(repoDir
, "SRPMS");
99 File debuginfoDir
= null;
100 if (!arch
.equals(NOARCH
)) {
101 archDir
= new File(repoDir
, arch
);
102 debuginfoDir
= new File(archDir
, debuginfoDirName
);
103 debuginfoDir
.mkdirs();
107 Set
<File
> reposToRecreate
= new HashSet
<File
>();
108 File resultDir
= new File(mockVar
+ "/" + cfgId
+ "/result");
109 rpms
: for (File file
: resultDir
.listFiles()) {
110 if (file
.isDirectory())
114 if (file
.getName().contains(".src.rpm"))
115 targetDirs
= new File
[] { srpmDir
};
116 else if (file
.getName().contains("-debuginfo-"))
117 targetDirs
= new File
[] { debuginfoDir
};
118 else if (!arch
.equals(NOARCH
)
119 && file
.getName().contains("." + arch
+ ".rpm"))
120 targetDirs
= new File
[] { archDir
};
121 else if (file
.getName().contains(".noarch.rpm")) {
122 List
<File
> dirs
= new ArrayList
<File
>();
123 for (String arch
: buildEnvironment
.getArchs())
124 dirs
.add(new File(repoDir
, arch
));
125 targetDirs
= dirs
.toArray(new File
[dirs
.size()]);
126 } else if (file
.getName().contains(".rpm"))
127 throw new SlcException("Don't know where to copy " + file
);
129 if (log
.isTraceEnabled())
130 log
.trace("Skip " + file
);
134 reposToRecreate
.addAll(Arrays
.asList(targetDirs
));
135 copyToDirs(file
, targetDirs
);
138 // recreate changed repos
139 for (File repoToRecreate
: reposToRecreate
) {
140 SystemCall createrepo
= new SystemCall();
141 createrepo
.arg("createrepo");
143 createrepo
.arg("-d");
145 if (!repoToRecreate
.getName().equals(debuginfoDirName
))
146 createrepo
.arg("-x").arg(debuginfoDirName
+ "/*");
148 createrepo
.arg("-q");
149 createrepo
.arg(repoToRecreate
.getAbsolutePath());
151 createrepo
.setExecutor(executor
);
153 log
.info("Updated repo " + repoToRecreate
);
157 protected void copyToDirs(File file
, File
[] dirs
) {
158 for (File dir
: dirs
) {
160 FileUtils
.copyFileToDirectory(file
, dir
);
161 if (log
.isDebugEnabled())
162 log
.debug(file
+ " => " + dir
);
163 } catch (IOException e
) {
164 throw new SlcException("Cannot copy " + file
+ " to " + dir
, e
);
169 public void setMockFlavour(String mockFlavour
) {
170 this.mockFlavour
= mockFlavour
;
173 public void setMockConfig(String mockConfig
) {
174 this.mockConfig
= mockConfig
;
177 public void setRepository(String repo
) {
178 this.repository
= repo
;
181 public void setRelease(String release
) {
182 this.release
= release
;
185 public void setLevel(String level
) {
189 public void setArch(String arch
) {
193 public void setRpmPackage(String rpmPackage
) {
194 this.rpmPackage
= rpmPackage
;
197 public void setMockVar(String mockVar
) {
198 this.mockVar
= mockVar
;
201 public void setMkdirs(Boolean mkdirs
) {
202 this.mkdirs
= mkdirs
;
205 public void setBuildEnvironment(RpmBuildEnvironment buildEnvironment
) {
206 this.buildEnvironment
= buildEnvironment
;
209 public void setExecutor(Executor executor
) {
210 this.executor
= executor
;