1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package helper; 28 29 import java.io.File; 30 import java.io.PrintWriter; 31 import lib.TestParameters; 32 import share.LogWriter; 33 import util.*; 34 35 /** 36 * This class support you to execute some shell commands in a buld environment. At ervery call of commands 37 * a build environment was created and the commands will be executed. 38 * 39 */ 40 public class BuildEnvTools { 41 42 private final TestParameters param; 43 private final LogWriter log; 44 private final boolean mDebug; 45 private final String mPlatform; 46 private final String mShell; 47 private boolean mCygwin; 48 49 /** 50 * This constructor creates an instance of BuildEncTools. It is verifying for all neccesarry 51 * parameters in <CODE>TestParameters</CODE> This must be: 52 * <ul> 53 * <li>OperatingSystem: Fill this parameter with an operating system like unxsols, unxsoli, unxlngi or wntmsci. 54 * </li> 55 * <li> Shell: Fill this parameter with a shell f.e '/bin/tcsh' 56 * or 'c:\\myShell\\myShell.exe' 57 * </li> 58 * @param param 59 * @param log 60 * @throws helper.ParameterNotFoundException 61 */ 62 public BuildEnvTools(TestParameters param, LogWriter log) throws ParameterNotFoundException { 63 this.param = param; 64 this.log = log; 65 mDebug = param.getBool(PropertyName.DEBUG_IS_ACTIVE); 66 67 boolean error = false; 68 69 String msg = "\nERROR: the following parameter must be set before executing the test:\n\n"; 70 71 mPlatform = (String) param.get(PropertyName.OPERATING_SYSTEM); 72 if (mDebug) { 73 log.println("### " + mPlatform); 74 } 75 if (mPlatform == null){ 76 msg += PropertyName.OPERATING_SYSTEM + "\nFill this parameter with an operating system like unxsols," + 77 " unxsoli, unxlngi, unxmacxi or wntmsci. \n\n"; 78 } 79 if( 80 (!mPlatform.equalsIgnoreCase(PropertyName.UNXSOLS)) && 81 (!mPlatform.equalsIgnoreCase(PropertyName.UNXSOLI)) && 82 (!mPlatform.equalsIgnoreCase(PropertyName.UNXLNGI)) && 83 (!mPlatform.equalsIgnoreCase(PropertyName.UNXMACXI))&& 84 (!mPlatform.equalsIgnoreCase(PropertyName.WNTMSCI)) ){ 85 86 msg += PropertyName.OPERATING_SYSTEM + ":" + mPlatform + "\nFill this parameter with an operating system like unxsols," + 87 " unxsoli, unxlngi, unxmacxi or wntmsci. \n\n"; 88 error = true; 89 } 90 91 mShell = (String) param.get(PropertyName.SHELL); 92 if (mShell == null) { 93 msg += PropertyName.SHELL + "\nFill this parameter with a shell" + 94 "\n\t/bin/tcsh c:\\myShell\\myShell.exe\n\n"; 95 error = true; 96 } 97 98 mCygwin = (param.getBool(PropertyName.CYGWIN)); 99 100 if (error) { 101 throw new ParameterNotFoundException(msg); 102 } 103 } 104 105 /** 106 * Executes the given commands in OOo-Environment shell. 107 * @param commands 108 * @param workDir 109 * @param shortWait 110 * @return the processHandler of the commands 111 * @see helper.ProcessHandler 112 */ 113 public ProcessHandler runCommandsInEnvironmentShell(String[] commands, File workDir, int shortWait) { 114 115 final String[] cmdLines = getCmdLinesWithCommand(commands); 116 final ProcessHandler pHdl = new ProcessHandler(cmdLines, (PrintWriter) log, workDir, shortWait, param); 117 pHdl.runCommand(); 118 return pHdl; 119 } 120 121 public String getSrcRoot() { 122 123 String sSrcRoot = (String) param.get(PropertyName.SRC_ROOT); 124 125 if (sSrcRoot == null) { 126 String[] cmdLines = null; 127 if (mPlatform.equals(PropertyName.WNTMSCI) && ! mCygwin) { 128 cmdLines = new String[]{mShell, "/C", "echo SRC_ROOT=%SRC_ROOT"}; 129 } else { 130 cmdLines = new String[]{mShell, "--login ", "-c ", "echo \"SRC_ROOT=$SRC_ROOT\""}; 131 } 132 133 final ProcessHandler procHdl = new ProcessHandler(cmdLines, (PrintWriter) log, null, 5000, param); 134 procHdl.runCommand(); 135 136 if (mDebug) { 137 log.println("---> Output of command:"); 138 log.println(procHdl.getOutputText()); 139 log.println("<--- Output of command:"); 140 log.println("---> Error output of command"); 141 log.println(procHdl.getErrorText()); 142 log.println("<--- Error output of command"); 143 } 144 final String output = procHdl.getOutputText(); 145 final String[] outs = output.split("\n"); 146 147 for (int i = 0; i < outs.length; i++) { 148 final String line = outs[i]; 149 if (line.startsWith("SRC_ROOT")) { 150 sSrcRoot = getEnvValue(line); 151 } 152 } 153 } 154 return sSrcRoot; 155 } 156 157 private String[] getCmdLinesWithCommand(String[] commands) { 158 String[] cmdLines = null; 159 log.println("prepare command for platform " + mPlatform); 160 161 String seperator = ""; 162 if (mPlatform.equals(PropertyName.WNTMSCI)) { 163 seperator = mCygwin ? ";" : "^"; 164 } else { 165 seperator = ";"; 166 } 167 168 String command = ""; 169 for (int i = 0; i < commands.length; i++) { 170 if (i != 0) { 171 command += seperator; 172 } 173 command += commands[i]; 174 } 175 176 if (mPlatform.equals(PropertyName.WNTMSCI)){ 177 if (mCygwin){ 178 String srcRoot = (String) param.get(PropertyName.SRC_ROOT); 179 String envSet = "export cyg_src_root=`cygpath '" + srcRoot.replaceAll("\\\\", "\\\\\\\\")+ "'`; source $cyg_src_root/winenv.set.sh;"; 180 command = envSet + command; 181 cmdLines = new String[]{mShell, "--login", "-c", "\"" + command + "\""}; 182 } else { 183 cmdLines = new String[]{mShell, "/C", "\"" + command + "\""}; 184 } 185 } else { 186 cmdLines = new String[]{mShell, "-c", command}; 187 } 188 return cmdLines; 189 } 190 191 private String getEnvValue(String line) { 192 final String[] split = line.split("="); 193 return split[1]; 194 } 195 } 196