]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.rpmfactory/src/org/argeo/slc/rpmfactory/core/BuildInMock.java
1 package org
.argeo
.slc
.rpmfactory
.core
;
4 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Arrays
;
7 import java
.util
.HashSet
;
11 import org
.apache
.commons
.exec
.Executor
;
12 import org
.apache
.commons
.io
.FileUtils
;
13 import org
.argeo
.api
.cms
.CmsLog
;
14 import org
.argeo
.slc
.SlcException
;
15 import org
.argeo
.slc
.rpmfactory
.RpmFactory
;
16 import org
.argeo
.slc
.runtime
.tasks
.SystemCall
;
18 /** Build an RPM in mock. */
19 public class BuildInMock
implements Runnable
{
20 private final static CmsLog log
= CmsLog
.getLog(BuildInMock
.class);
21 private final static String NOARCH
= "noarch";
23 private String rpmPackage
= null;
24 private String branch
= null;
25 private String arch
= NOARCH
;
27 private RpmFactory rpmFactory
;
28 private Executor executor
;
30 private String debuginfoDirName
= "debuginfo";
31 private String mockExecutable
= "/usr/bin/mock";
33 private List
<String
> preBuildCommands
= new ArrayList
<String
>();
36 if (!rpmFactory
.isDeveloperInstance()) {
38 SystemCall mockClean
= createBaseMockCall();
39 mockClean
.arg("--init");
44 for (String preBuildCmd
: preBuildCommands
) {
45 SystemCall mockClean
= createBaseMockCall();
46 mockClean
.arg("--chroot").arg(preBuildCmd
);
51 SystemCall mockBuild
= createBaseMockCall();
52 mockBuild
.arg("--scm-enable");
53 mockBuild
.arg("--scm-option").arg("package=" + rpmPackage
);
54 mockBuild
.arg("--no-clean");
60 // copy RPMs to target directories
61 File stagingDir
= rpmFactory
.getWorkspaceDir(rpmFactory
62 .getStagingWorkspace());
63 File srpmDir
= new File(stagingDir
, "SRPMS");
66 File debuginfoDir
= null;
67 if (!arch
.equals(NOARCH
)) {
68 archDir
= new File(stagingDir
, arch
);
69 debuginfoDir
= new File(archDir
, debuginfoDirName
);
70 debuginfoDir
.mkdirs();
73 Set
<File
> reposToRecreate
= new HashSet
<File
>();
74 File resultDir
= rpmFactory
.getResultDir(arch
);
75 if (resultDir
.exists())
76 rpms
: for (File file
: resultDir
.listFiles()) {
77 if (file
.isDirectory())
81 if (file
.getName().contains(".src.rpm"))
82 targetDirs
= new File
[] { srpmDir
};
83 else if (file
.getName().contains("-debuginfo-"))
84 targetDirs
= new File
[] { debuginfoDir
};
85 else if (!arch
.equals(NOARCH
)
86 && file
.getName().contains("." + arch
+ ".rpm"))
87 targetDirs
= new File
[] { archDir
};
88 else if (file
.getName().contains(".noarch.rpm")) {
89 List
<File
> dirs
= new ArrayList
<File
>();
90 for (String arch
: rpmFactory
.getArchs())
91 dirs
.add(new File(stagingDir
, arch
));
92 targetDirs
= dirs
.toArray(new File
[dirs
.size()]);
93 } else if (file
.getName().contains(".rpm"))
94 throw new SlcException("Don't know where to copy " + file
);
96 if (log
.isTraceEnabled())
97 log
.trace("Skip " + file
);
101 reposToRecreate
.addAll(Arrays
.asList(targetDirs
));
102 copyToDirs(file
, targetDirs
);
105 // recreate changed repos
106 for (File repoToRecreate
: reposToRecreate
) {
107 SystemCall createrepo
= new SystemCall();
108 createrepo
.arg("createrepo");
110 createrepo
.arg("-d");
112 if (!repoToRecreate
.getName().equals(debuginfoDirName
))
113 createrepo
.arg("-x").arg(debuginfoDirName
+ "/*");
115 createrepo
.arg("-q");
116 createrepo
.arg(repoToRecreate
.getAbsolutePath());
118 createrepo
.setExecutor(executor
);
120 log
.info("Updated repo " + repoToRecreate
);
123 // index staging workspace
124 rpmFactory
.indexWorkspace(rpmFactory
.getStagingWorkspace());
127 /** Creates a mock call with all the common options such as config file etc. */
128 protected SystemCall
createBaseMockCall() {
129 String mockCfg
= rpmFactory
.getMockConfig(arch
);
130 File mockConfigFile
= rpmFactory
.getMockConfigFile(arch
, branch
);
133 SystemCall mock
= new SystemCall();
136 mock
.arg("setarch").arg(arch
);
137 mock
.arg(mockExecutable
);
139 mock
.arg("--configdir=" + mockConfigFile
.getAbsoluteFile().getParent());
141 mock
.arg("--arch=" + arch
);
142 mock
.arg("-r").arg(mockCfg
);
144 mock
.setLogCommand(true);
145 mock
.setExecutor(executor
);
150 protected void copyToDirs(File file
, File
[] dirs
) {
151 for (File dir
: dirs
) {
153 FileUtils
.copyFileToDirectory(file
, dir
);
154 if (log
.isDebugEnabled())
155 log
.debug(file
+ " => " + dir
);
156 } catch (IOException e
) {
157 throw new SlcException("Cannot copy " + file
+ " to " + dir
, e
);
162 public void setArch(String arch
) {
166 public void setRpmPackage(String rpmPackage
) {
167 this.rpmPackage
= rpmPackage
;
170 public void setBranch(String branch
) {
171 this.branch
= branch
;
174 public void setRpmFactory(RpmFactory env
) {
175 this.rpmFactory
= env
;
178 public void setExecutor(Executor executor
) {
179 this.executor
= executor
;
182 public void setMockExecutable(String mockExecutable
) {
183 this.mockExecutable
= mockExecutable
;
186 public void setPreBuildCommands(List
<String
> preBuildCommands
) {
187 this.preBuildCommands
= preBuildCommands
;