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 arch
= NOARCH
;
41 private RpmBuildEnvironment buildEnvironment
;
42 private Executor executor
;
44 private String debuginfoDirName
= "debuginfo";
47 String mockCfg
= buildEnvironment
.getMockConfig(arch
);
48 File mockConfigFile
= buildEnvironment
.getMockConfigFile(arch
);
51 SystemCall mock
= new SystemCall();
53 mock
.arg("setarch").arg(arch
);
56 mock
.arg("--configdir=" + mockConfigFile
.getAbsoluteFile().getParent());
58 mock
.arg("--arch=" + arch
);
59 mock
.arg("-r").arg(mockCfg
);
60 mock
.arg("--scm-enable");
61 mock
.arg("--scm-option").arg("package=" + rpmPackage
);
63 mock
.setLogCommand(true);
64 mock
.setExecutor(executor
);
67 // mock command execution
72 File stagingDir
= buildEnvironment
.getStagingDir();
73 File srpmDir
= new File(stagingDir
, "SRPMS");
76 File debuginfoDir
= null;
77 if (!arch
.equals(NOARCH
)) {
78 archDir
= new File(stagingDir
, arch
);
79 debuginfoDir
= new File(archDir
, debuginfoDirName
);
80 debuginfoDir
.mkdirs();
84 Set
<File
> reposToRecreate
= new HashSet
<File
>();
85 File resultDir
= buildEnvironment
.getResultDir(arch
);
86 rpms
: for (File file
: resultDir
.listFiles()) {
87 if (file
.isDirectory())
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
);
106 if (log
.isTraceEnabled())
107 log
.trace("Skip " + file
);
111 reposToRecreate
.addAll(Arrays
.asList(targetDirs
));
112 copyToDirs(file
, targetDirs
);
115 // recreate changed repos
116 for (File repoToRecreate
: reposToRecreate
) {
117 SystemCall createrepo
= new SystemCall();
118 createrepo
.arg("createrepo");
120 createrepo
.arg("-d");
122 if (!repoToRecreate
.getName().equals(debuginfoDirName
))
123 createrepo
.arg("-x").arg(debuginfoDirName
+ "/*");
125 createrepo
.arg("-q");
126 createrepo
.arg(repoToRecreate
.getAbsolutePath());
128 createrepo
.setExecutor(executor
);
130 log
.info("Updated repo " + repoToRecreate
);
134 protected void copyToDirs(File file
, File
[] dirs
) {
135 for (File dir
: dirs
) {
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
);
146 public void setArch(String arch
) {
150 public void setRpmPackage(String rpmPackage
) {
151 this.rpmPackage
= rpmPackage
;
154 public void setBuildEnvironment(RpmBuildEnvironment buildEnvironment
) {
155 this.buildEnvironment
= buildEnvironment
;
158 public void setExecutor(Executor executor
) {
159 this.executor
= executor
;