1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package helper;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import java.io.File;
26cdf0e10cSrcweir import java.io.PrintWriter;
27cdf0e10cSrcweir import lib.TestParameters;
28cdf0e10cSrcweir import share.LogWriter;
29cdf0e10cSrcweir import util.*;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /**
32cdf0e10cSrcweir  * This class support you to execute some shell commands in a buld environment. At ervery call of commands
33cdf0e10cSrcweir  * a build environment was created and the commands will be executed.
34cdf0e10cSrcweir  *
35cdf0e10cSrcweir  */
36cdf0e10cSrcweir public class BuildEnvTools {
37cdf0e10cSrcweir 
38cdf0e10cSrcweir     private final TestParameters param;
39cdf0e10cSrcweir     private final LogWriter log;
40cdf0e10cSrcweir     private final boolean mDebug;
41cdf0e10cSrcweir     private final String mPlatform;
42cdf0e10cSrcweir     private final String mShell;
43cdf0e10cSrcweir     private boolean mCygwin;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     /**
46cdf0e10cSrcweir      * This constructor creates an instance of BuildEncTools. It is verifying for all neccesarry
47cdf0e10cSrcweir      * parameters in <CODE>TestParameters</CODE> This must be:
48cdf0e10cSrcweir      * <ul>
49cdf0e10cSrcweir      * <li>OperatingSystem: Fill this parameter with an operating system like unxsols, unxsoli, unxlngi or wntmsci.
50cdf0e10cSrcweir      * </li>
51cdf0e10cSrcweir      * <li> Shell: Fill this parameter with a shell f.e '/bin/tcsh'
52cdf0e10cSrcweir      *      or 'c:\\myShell\\myShell.exe'
53cdf0e10cSrcweir      * </li>
54cdf0e10cSrcweir      * @param param
55cdf0e10cSrcweir      * @param log
56cdf0e10cSrcweir      * @throws helper.ParameterNotFoundException
57cdf0e10cSrcweir      */
BuildEnvTools(TestParameters param, LogWriter log)58cdf0e10cSrcweir     public BuildEnvTools(TestParameters param, LogWriter log) throws ParameterNotFoundException {
59cdf0e10cSrcweir         this.param = param;
60cdf0e10cSrcweir         this.log = log;
61cdf0e10cSrcweir         mDebug = param.getBool(PropertyName.DEBUG_IS_ACTIVE);
62cdf0e10cSrcweir 
63cdf0e10cSrcweir         boolean error = false;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         String msg = "\nERROR: the following parameter must be set before executing the test:\n\n";
66cdf0e10cSrcweir 
67cdf0e10cSrcweir         mPlatform = (String) param.get(PropertyName.OPERATING_SYSTEM);
68cdf0e10cSrcweir         if (mDebug) {
69cdf0e10cSrcweir             log.println("### " + mPlatform);
70cdf0e10cSrcweir         }
71cdf0e10cSrcweir         if (mPlatform == null){
72cdf0e10cSrcweir             msg += PropertyName.OPERATING_SYSTEM + "\nFill this parameter with an operating system like unxsols," +
73cdf0e10cSrcweir                 " unxsoli, unxlngi, unxmacxi or wntmsci.  \n\n";
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir         if(
76cdf0e10cSrcweir             (!mPlatform.equalsIgnoreCase(PropertyName.UNXSOLS)) &&
77cdf0e10cSrcweir             (!mPlatform.equalsIgnoreCase(PropertyName.UNXSOLI)) &&
78cdf0e10cSrcweir             (!mPlatform.equalsIgnoreCase(PropertyName.UNXLNGI)) &&
79cdf0e10cSrcweir             (!mPlatform.equalsIgnoreCase(PropertyName.UNXMACXI))&&
80*82f06cbcSdamjan             (!mPlatform.equalsIgnoreCase(PropertyName.UNXFBSDX))&&
81cdf0e10cSrcweir             (!mPlatform.equalsIgnoreCase(PropertyName.WNTMSCI)) ){
82cdf0e10cSrcweir 
83cdf0e10cSrcweir             msg += PropertyName.OPERATING_SYSTEM + ":" + mPlatform + "\nFill this parameter with an operating system like unxsols," +
84*82f06cbcSdamjan                 " unxsoli, unxlngi, unxfbsdx, unxmacxi or wntmsci.  \n\n";
85cdf0e10cSrcweir             error = true;
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         mShell = (String) param.get(PropertyName.SHELL);
89cdf0e10cSrcweir         if (mShell == null) {
90cdf0e10cSrcweir             msg += PropertyName.SHELL + "\nFill this parameter with a shell" +
91cdf0e10cSrcweir                 "\n\t/bin/tcsh c:\\myShell\\myShell.exe\n\n";
92cdf0e10cSrcweir             error = true;
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         mCygwin = (param.getBool(PropertyName.CYGWIN));
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         if (error) {
98cdf0e10cSrcweir             throw new ParameterNotFoundException(msg);
99cdf0e10cSrcweir         }
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     /**
103cdf0e10cSrcweir      * Executes the given commands in OOo-Environment shell.
104cdf0e10cSrcweir      * @param commands
105cdf0e10cSrcweir      * @param workDir
106cdf0e10cSrcweir      * @param shortWait
107cdf0e10cSrcweir      * @return the processHandler of the commands
108cdf0e10cSrcweir      * @see helper.ProcessHandler
109cdf0e10cSrcweir      */
runCommandsInEnvironmentShell(String[] commands, File workDir, int shortWait)110cdf0e10cSrcweir     public ProcessHandler runCommandsInEnvironmentShell(String[] commands, File workDir, int shortWait) {
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         final String[] cmdLines = getCmdLinesWithCommand(commands);
113cdf0e10cSrcweir         final ProcessHandler pHdl = new ProcessHandler(cmdLines, (PrintWriter) log, workDir, shortWait, param);
114cdf0e10cSrcweir         pHdl.runCommand();
115cdf0e10cSrcweir         return pHdl;
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir 
getSrcRoot()118cdf0e10cSrcweir     public String getSrcRoot() {
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         String sSrcRoot = (String) param.get(PropertyName.SRC_ROOT);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir         if (sSrcRoot == null) {
123cdf0e10cSrcweir             String[] cmdLines = null;
124cdf0e10cSrcweir             if (mPlatform.equals(PropertyName.WNTMSCI) && ! mCygwin) {
125cdf0e10cSrcweir                 cmdLines = new String[]{mShell, "/C", "echo SRC_ROOT=%SRC_ROOT"};
126cdf0e10cSrcweir             } else {
127cdf0e10cSrcweir                 cmdLines = new String[]{mShell, "--login ", "-c ", "echo \"SRC_ROOT=$SRC_ROOT\""};
128cdf0e10cSrcweir             }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir             final ProcessHandler procHdl = new ProcessHandler(cmdLines, (PrintWriter) log, null, 5000, param);
131cdf0e10cSrcweir             procHdl.runCommand();
132cdf0e10cSrcweir 
133cdf0e10cSrcweir             if (mDebug) {
134cdf0e10cSrcweir                 log.println("---> Output of command:");
135cdf0e10cSrcweir                 log.println(procHdl.getOutputText());
136cdf0e10cSrcweir                 log.println("<--- Output of command:");
137cdf0e10cSrcweir                 log.println("---> Error output of command");
138cdf0e10cSrcweir                 log.println(procHdl.getErrorText());
139cdf0e10cSrcweir                 log.println("<--- Error output of command");
140cdf0e10cSrcweir             }
141cdf0e10cSrcweir             final String output = procHdl.getOutputText();
142cdf0e10cSrcweir             final String[] outs = output.split("\n");
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             for (int i = 0; i < outs.length; i++) {
145cdf0e10cSrcweir                 final String line = outs[i];
146cdf0e10cSrcweir                 if (line.startsWith("SRC_ROOT")) {
147cdf0e10cSrcweir                     sSrcRoot = getEnvValue(line);
148cdf0e10cSrcweir                 }
149cdf0e10cSrcweir             }
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir         return sSrcRoot;
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
getCmdLinesWithCommand(String[] commands)154cdf0e10cSrcweir     private String[] getCmdLinesWithCommand(String[] commands) {
155cdf0e10cSrcweir         String[] cmdLines = null;
156cdf0e10cSrcweir         log.println("prepare command for platform " + mPlatform);
157cdf0e10cSrcweir 
158cdf0e10cSrcweir         String seperator = "";
159cdf0e10cSrcweir         if (mPlatform.equals(PropertyName.WNTMSCI)) {
160cdf0e10cSrcweir             seperator = mCygwin ? ";" : "^";
161cdf0e10cSrcweir         } else {
162cdf0e10cSrcweir             seperator = ";";
163cdf0e10cSrcweir         }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         String command = "";
166cdf0e10cSrcweir         for (int i = 0; i < commands.length; i++) {
167cdf0e10cSrcweir             if (i != 0) {
168cdf0e10cSrcweir                 command += seperator;
169cdf0e10cSrcweir             }
170cdf0e10cSrcweir             command += commands[i];
171cdf0e10cSrcweir         }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir         if (mPlatform.equals(PropertyName.WNTMSCI)){
174cdf0e10cSrcweir             if (mCygwin){
175cdf0e10cSrcweir                 String srcRoot = (String) param.get(PropertyName.SRC_ROOT);
176cdf0e10cSrcweir                 String envSet = "export cyg_src_root=`cygpath '" + srcRoot.replaceAll("\\\\", "\\\\\\\\")+ "'`; source $cyg_src_root/winenv.set.sh;";
177cdf0e10cSrcweir                 command = envSet + command;
178cdf0e10cSrcweir                 cmdLines = new String[]{mShell, "--login", "-c", "\"" + command + "\""};
179cdf0e10cSrcweir             } else {
180cdf0e10cSrcweir             cmdLines = new String[]{mShell, "/C", "\"" + command + "\""};
181cdf0e10cSrcweir             }
182cdf0e10cSrcweir         } else {
183cdf0e10cSrcweir             cmdLines = new String[]{mShell, "-c", command};
184cdf0e10cSrcweir         }
185cdf0e10cSrcweir         return cmdLines;
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir 
getEnvValue(String line)188cdf0e10cSrcweir     private String getEnvValue(String line) {
189cdf0e10cSrcweir         final String[] split = line.split("=");
190cdf0e10cSrcweir         return split[1];
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir }
193