From bf02db6f94eadadcda6984a10d98c325822add54 Mon Sep 17 00:00:00 2001 From: "David M. Carr" Date: Mon, 27 Dec 2010 01:21:57 -0500 Subject: [PATCH] Enhance WindowsHgInstaller to use Mercurial packages (source or binary) installed into a Python installation. --- .../scm/installer/AbstractHgInstaller.java | 68 +++++++++++------ .../scm/installer/WindowsHgInstaller.java | 74 ++++++++++++++----- 2 files changed, 102 insertions(+), 40 deletions(-) diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/AbstractHgInstaller.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/AbstractHgInstaller.java index 03935c4c3f..7a514e6031 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/AbstractHgInstaller.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/AbstractHgInstaller.java @@ -29,8 +29,6 @@ * */ - - package sonia.scm.installer; //~--- non-JDK imports -------------------------------------------------------- @@ -50,6 +48,8 @@ import sonia.scm.util.SystemUtil; import java.io.File; import java.io.IOException; +import java.util.Arrays; +import java.util.List; /** * @@ -62,8 +62,8 @@ public abstract class AbstractHgInstaller implements HgInstaller public static final String DIRECTORY_REPOSITORY = "repositories"; /** the logger for AbstractHgInstaller */ - private static final Logger logger = - LoggerFactory.getLogger(AbstractHgInstaller.class); + private static final Logger logger = LoggerFactory + .getLogger(AbstractHgInstaller.class); //~--- constructors --------------------------------------------------------- @@ -91,10 +91,8 @@ public abstract class AbstractHgInstaller implements HgInstaller @Override public void install(HgConfig config) throws IOException { - File repoDirectory = new File( - baseDirectory, - DIRECTORY_REPOSITORY.concat(File.separator).concat( - HgRepositoryHandler.TYPE_NAME)); + File repoDirectory = new File(baseDirectory, DIRECTORY_REPOSITORY.concat( + File.separator).concat(HgRepositoryHandler.TYPE_NAME)); IOUtil.mkdirs(repoDirectory); config.setRepositoryDirectory(repoDirectory); @@ -124,27 +122,18 @@ public abstract class AbstractHgInstaller implements HgInstaller cmdPath = cmd; } } - catch (IOException ex) {} + catch (IOException ex) + {} if (cmdPath == null) { for (String pathPart : path) { - File file = null; - - if (SystemUtil.isWindows()) - { - file = new File(pathPart, cmd.concat(".exe")); - } - else - { - file = new File(pathPart, cmd); - } - - if (file.exists()) + List extensions = getExecutableSearchExtensions(); + File file = findFileByExtension(pathPart, cmd, extensions); + if (file != null) { cmdPath = file.getAbsolutePath(); - break; } } @@ -165,6 +154,41 @@ public abstract class AbstractHgInstaller implements HgInstaller return cmdPath; } + /** + * Returns a list of file extensions to use when searching for executables. + * The list is in priority order, with the highest priority first. + */ + protected List getExecutableSearchExtensions() + { + List extensions; + if (SystemUtil.isWindows()) + { + extensions = Arrays.asList(".exe"); + } + else + { + extensions = Arrays.asList(""); + } + return extensions; + } + + private File findFileByExtension(String parentPath, String cmd, + List potentialExtensions) + { + File file = null; + for (String potentialExtension : potentialExtensions) + { + String fileName = cmd.concat(potentialExtension); + File potentialFile = new File(parentPath, fileName); + if (potentialFile.exists()) + { + file = potentialFile; + break; + } + } + return file; + } + //~--- fields --------------------------------------------------------------- /** Field description */ diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/WindowsHgInstaller.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/WindowsHgInstaller.java index 60dcc47128..84e9c52a21 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/WindowsHgInstaller.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/WindowsHgInstaller.java @@ -29,8 +29,6 @@ * */ - - package sonia.scm.installer; //~--- non-JDK imports -------------------------------------------------------- @@ -63,11 +61,20 @@ public class WindowsHgInstaller extends AbstractHgInstaller private static final String FILE_LIBRARY_ZIP = "library.zip"; /** Field description */ - private static final String FILE_MERCURIAL = "hg.exe"; + private static final String FILE_MERCURIAL_EXE = "hg.exe"; + + /** Field description */ + private static final String FILE_MERCURIAL_SCRIPT = "hg.bat"; /** Field description */ private static final String FILE_TEMPLATES = "templates"; + /** Field description */ + private static final String FILE_SCRIPTS = "Scripts"; + + /** Field description */ + private static final String FILE_LIB_MERCURIAL = "Lib\\site-packages\\mercurial"; + /** Field description */ private static final String[] REGISTRY_HG = new String[] { @@ -80,12 +87,11 @@ public class WindowsHgInstaller extends AbstractHgInstaller }; /** Field description */ - private static final String REGISTRY_PYTHON = - "HKEY_CLASSES_ROOT\\Python.File\\shell\\open\\command"; + private static final String REGISTRY_PYTHON = "HKEY_CLASSES_ROOT\\Python.File\\shell\\open\\command"; /** the logger for WindowsHgInstaller */ - private static final Logger logger = - LoggerFactory.getLogger(WindowsHgInstaller.class); + private static final Logger logger = LoggerFactory + .getLogger(WindowsHgInstaller.class); //~--- constructors --------------------------------------------------------- @@ -115,15 +121,21 @@ public class WindowsHgInstaller extends AbstractHgInstaller { super.install(config); - File hgDirectory = getMercurialDirectory(); + String pythonBinary = getPythonBinary(); + config.setPythonBinary(pythonBinary); - if (hgDirectory != null) + File hgScript = getMercurialScript(pythonBinary); + File hgDirectory = getMercurialDirectory(); + if (hgScript != null) + { + config.setHgBinary(hgScript.getAbsolutePath()); + } + else if (hgDirectory != null) { installHg(config, hgDirectory); } checkForOptimizedByteCode(config); - config.setPythonBinary(getPythonBinary()); } /** @@ -133,7 +145,8 @@ public class WindowsHgInstaller extends AbstractHgInstaller * @param config */ @Override - public void update(HgConfig config) {} + public void update(HgConfig config) + {} /** * Method description @@ -220,7 +233,8 @@ public class WindowsHgInstaller extends AbstractHgInstaller IOUtil.copy(templateDirectory, new File(libDir, FILE_TEMPLATES)); } - config.setHgBinary(new File(hgDirectory, FILE_MERCURIAL).getAbsolutePath()); + config.setHgBinary(new File(hgDirectory, FILE_MERCURIAL_EXE) + .getAbsolutePath()); } //~--- get methods ---------------------------------------------------------- @@ -257,6 +271,32 @@ public class WindowsHgInstaller extends AbstractHgInstaller return directory; } + /** + * Returns the location of the script to run Mercurial, if Mercurial is + * installed as a Python package from source. Only packages that include a + * templates directory will be recognized. + */ + private File getMercurialScript(String pythonBinary) + { + File hgScript = null; + + if (pythonBinary != null) + { + File pythonBinaryFile = new File(pythonBinary); + if (pythonBinaryFile.exists()) + { + File pythonDir = pythonBinaryFile.getParentFile(); + File scriptsDir = new File(pythonDir, FILE_SCRIPTS); + File potentialHgScript = new File(scriptsDir, FILE_MERCURIAL_SCRIPT); + File mercurialPackageDir = new File(pythonDir, FILE_LIB_MERCURIAL); + File templatesDir = new File(mercurialPackageDir, FILE_TEMPLATES); + if (potentialHgScript.exists() && templatesDir.exists()) + hgScript = potentialHgScript; + } + } + return hgScript; + } + /** * Method description * @@ -285,8 +325,7 @@ public class WindowsHgInstaller extends AbstractHgInstaller * * @return */ - private String getRegistryValue(String key, String subKey, - String defaultValue) + private String getRegistryValue(String key, String subKey, String defaultValue) { String programDirectory = defaultValue; SimpleCommand command = null; @@ -316,14 +355,13 @@ public class WindowsHgInstaller extends AbstractHgInstaller if (index > 0) { - programDirectory = line.substring(index - + "REG_SZ".length()).trim(); + programDirectory = line.substring(index + "REG_SZ".length()).trim(); if (programDirectory.startsWith("\"")) { programDirectory = programDirectory.substring(1); - programDirectory = programDirectory.substring(0, - programDirectory.indexOf("\"")); + programDirectory = programDirectory.substring(0, programDirectory + .indexOf("\"")); } if (logger.isDebugEnabled())