]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.rpmfactory/src/main/java/org/argeo/slc/rpmfactory/core/BuildInMock.java
Refactor RPM Factory
[gpl/argeo-slc.git] / runtime / org.argeo.slc.rpmfactory / src / main / java / org / argeo / slc / rpmfactory / core / BuildInMock.java
index 50daabce3f3d0c1a5bb1e2cbdf77f8394ac55570..1f815eac5a82ce26bbbf5497c02edb4b2306e349 100644 (file)
@@ -29,6 +29,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.core.execution.tasks.SystemCall;
+import org.argeo.slc.rpmfactory.RpmFactory;
 
 /** Build an RPM in mock. */
 public class BuildInMock implements Runnable {
@@ -39,43 +40,42 @@ public class BuildInMock implements Runnable {
        private String branch = null;
        private String arch = NOARCH;
 
-       private RpmFactory factory;
+       private RpmFactory rpmFactory;
        private Executor executor;
 
        private String debuginfoDirName = "debuginfo";
+       private String mockExecutable = "/usr/bin/mock";
 
-       public void run() {
-               String mockCfg = factory.getMockConfig(arch);
-               File mockConfigFile = factory.getMockConfigFile(arch, branch);
+       private List<String> preBuildCommands = new ArrayList<String>();
 
-               // prepare mock call
-               SystemCall mock = new SystemCall();
-               if (arch != null)
-                       mock.arg("setarch").arg(arch);
-               mock.arg("mock");
-               mock.arg("-v");
-               mock.arg("--configdir=" + mockConfigFile.getAbsoluteFile().getParent());
-               if (arch != null)
-                       mock.arg("--arch=" + arch);
-               mock.arg("-r").arg(mockCfg);
-               mock.arg("--scm-enable");
-               // mock.arg("--scm-option");
-               // mock.arg("git_get='git clone " + (branch != null ? "-b " + branch :
-               // "")
-               // + " " + factory.getGitBaseUrl() + "/SCM_PKG SCM_PKG'");
-               mock.arg("--scm-option").arg("package=" + rpmPackage);
+       public void run() {
+               if (!rpmFactory.isDeveloperInstance()) {
+                       // clean/init
+                       SystemCall mockClean = createBaseMockCall();
+                       mockClean.arg("--init");
+                       mockClean.run();
+               }
 
-               mock.setLogCommand(true);
-               mock.setExecutor(executor);
+               // pre build commands
+               for (String preBuildCmd : preBuildCommands) {
+                       SystemCall mockClean = createBaseMockCall();
+                       mockClean.arg("--chroot").arg(preBuildCmd);
+                       mockClean.run();
+               }
 
+               // actual build
+               SystemCall mockBuild = createBaseMockCall();
+               mockBuild.arg("--scm-enable");
+               mockBuild.arg("--scm-option").arg("package=" + rpmPackage);
+               mockBuild.arg("--no-clean");
                //
-               // mock command execution
                //
-               mock.run();
+               mockBuild.run();
                //
 
-               File stagingDir = factory
-                               .getWorkspaceDir(factory.getStagingWorkspace());
+               // copy RPMs to target directories
+               File stagingDir = rpmFactory.getWorkspaceDir(rpmFactory
+                               .getStagingWorkspace());
                File srpmDir = new File(stagingDir, "SRPMS");
                srpmDir.mkdirs();
                File archDir = null;
@@ -86,38 +86,38 @@ public class BuildInMock implements Runnable {
                        debuginfoDir.mkdirs();
                }
 
-               // copy RPMs
                Set<File> reposToRecreate = new HashSet<File>();
-               File resultDir = factory.getResultDir(arch);
-               rpms: for (File file : resultDir.listFiles()) {
-                       if (file.isDirectory())
-                               continue rpms;
-
-                       File[] targetDirs;
-                       if (file.getName().contains(".src.rpm"))
-                               targetDirs = new File[] { srpmDir };
-                       else if (file.getName().contains("-debuginfo-"))
-                               targetDirs = new File[] { debuginfoDir };
-                       else if (!arch.equals(NOARCH)
-                                       && file.getName().contains("." + arch + ".rpm"))
-                               targetDirs = new File[] { archDir };
-                       else if (file.getName().contains(".noarch.rpm")) {
-                               List<File> dirs = new ArrayList<File>();
-                               for (String arch : factory.getArchs())
-                                       dirs.add(new File(stagingDir, arch));
-                               targetDirs = dirs.toArray(new File[dirs.size()]);
-                       } else if (file.getName().contains(".rpm"))
-                               throw new SlcException("Don't know where to copy " + file);
-                       else {
-                               if (log.isTraceEnabled())
-                                       log.trace("Skip " + file);
-                               continue rpms;
+               File resultDir = rpmFactory.getResultDir(arch);
+               if (resultDir.exists())
+                       rpms: for (File file : resultDir.listFiles()) {
+                               if (file.isDirectory())
+                                       continue rpms;
+
+                               File[] targetDirs;
+                               if (file.getName().contains(".src.rpm"))
+                                       targetDirs = new File[] { srpmDir };
+                               else if (file.getName().contains("-debuginfo-"))
+                                       targetDirs = new File[] { debuginfoDir };
+                               else if (!arch.equals(NOARCH)
+                                               && file.getName().contains("." + arch + ".rpm"))
+                                       targetDirs = new File[] { archDir };
+                               else if (file.getName().contains(".noarch.rpm")) {
+                                       List<File> dirs = new ArrayList<File>();
+                                       for (String arch : rpmFactory.getArchs())
+                                               dirs.add(new File(stagingDir, arch));
+                                       targetDirs = dirs.toArray(new File[dirs.size()]);
+                               } else if (file.getName().contains(".rpm"))
+                                       throw new SlcException("Don't know where to copy " + file);
+                               else {
+                                       if (log.isTraceEnabled())
+                                               log.trace("Skip " + file);
+                                       continue rpms;
+                               }
+
+                               reposToRecreate.addAll(Arrays.asList(targetDirs));
+                               copyToDirs(file, targetDirs);
                        }
 
-                       reposToRecreate.addAll(Arrays.asList(targetDirs));
-                       copyToDirs(file, targetDirs);
-               }
-
                // recreate changed repos
                for (File repoToRecreate : reposToRecreate) {
                        SystemCall createrepo = new SystemCall();
@@ -137,7 +137,30 @@ public class BuildInMock implements Runnable {
                }
 
                // index staging workspace
-               factory.indexWorkspace(factory.getStagingWorkspace());
+               rpmFactory.indexWorkspace(rpmFactory.getStagingWorkspace());
+       }
+
+       /** Creates a mock call with all the common options such as config file etc. */
+       protected SystemCall createBaseMockCall() {
+               String mockCfg = rpmFactory.getMockConfig(arch);
+               File mockConfigFile = rpmFactory.getMockConfigFile(arch, branch);
+
+               // prepare mock call
+               SystemCall mock = new SystemCall();
+
+               if (arch != null)
+                       mock.arg("setarch").arg(arch);
+               mock.arg(mockExecutable);
+               mock.arg("-v");
+               mock.arg("--configdir=" + mockConfigFile.getAbsoluteFile().getParent());
+               if (arch != null)
+                       mock.arg("--arch=" + arch);
+               mock.arg("-r").arg(mockCfg);
+
+               mock.setLogCommand(true);
+               mock.setExecutor(executor);
+
+               return mock;
        }
 
        protected void copyToDirs(File file, File[] dirs) {
@@ -164,11 +187,20 @@ public class BuildInMock implements Runnable {
                this.branch = branch;
        }
 
-       public void setFactory(RpmFactory env) {
-               this.factory = env;
+       public void setRpmFactory(RpmFactory env) {
+               this.rpmFactory = env;
        }
 
        public void setExecutor(Executor executor) {
                this.executor = executor;
        }
+
+       public void setMockExecutable(String mockExecutable) {
+               this.mockExecutable = mockExecutable;
+       }
+
+       public void setPreBuildCommands(List<String> preBuildCommands) {
+               this.preBuildCommands = preBuildCommands;
+       }
+
 }
\ No newline at end of file