1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew 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      */
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))&&
80cdf0e10cSrcweir             (!mPlatform.equalsIgnoreCase(PropertyName.WNTMSCI)) ){
81cdf0e10cSrcweir 
82cdf0e10cSrcweir             msg += PropertyName.OPERATING_SYSTEM + ":" + mPlatform + "\nFill this parameter with an operating system like unxsols," +
83cdf0e10cSrcweir                 " unxsoli, unxlngi, unxmacxi or wntmsci.  \n\n";
84cdf0e10cSrcweir             error = true;
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         mShell = (String) param.get(PropertyName.SHELL);
88cdf0e10cSrcweir         if (mShell == null) {
89cdf0e10cSrcweir             msg += PropertyName.SHELL + "\nFill this parameter with a shell" +
90cdf0e10cSrcweir                 "\n\t/bin/tcsh c:\\myShell\\myShell.exe\n\n";
91cdf0e10cSrcweir             error = true;
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir         mCygwin = (param.getBool(PropertyName.CYGWIN));
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         if (error) {
97cdf0e10cSrcweir             throw new ParameterNotFoundException(msg);
98cdf0e10cSrcweir         }
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     /**
102cdf0e10cSrcweir      * Executes the given commands in OOo-Environment shell.
103cdf0e10cSrcweir      * @param commands
104cdf0e10cSrcweir      * @param workDir
105cdf0e10cSrcweir      * @param shortWait
106cdf0e10cSrcweir      * @return the processHandler of the commands
107cdf0e10cSrcweir      * @see helper.ProcessHandler
108cdf0e10cSrcweir      */
109cdf0e10cSrcweir     public ProcessHandler runCommandsInEnvironmentShell(String[] commands, File workDir, int shortWait) {
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         final String[] cmdLines = getCmdLinesWithCommand(commands);
112cdf0e10cSrcweir         final ProcessHandler pHdl = new ProcessHandler(cmdLines, (PrintWriter) log, workDir, shortWait, param);
113cdf0e10cSrcweir         pHdl.runCommand();
114cdf0e10cSrcweir         return pHdl;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     public String getSrcRoot() {
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         String sSrcRoot = (String) param.get(PropertyName.SRC_ROOT);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         if (sSrcRoot == null) {
122cdf0e10cSrcweir             String[] cmdLines = null;
123cdf0e10cSrcweir             if (mPlatform.equals(PropertyName.WNTMSCI) && ! mCygwin) {
124cdf0e10cSrcweir                 cmdLines = new String[]{mShell, "/C", "echo SRC_ROOT=%SRC_ROOT"};
125cdf0e10cSrcweir             } else {
126cdf0e10cSrcweir                 cmdLines = new String[]{mShell, "--login ", "-c ", "echo \"SRC_ROOT=$SRC_ROOT\""};
127cdf0e10cSrcweir             }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir             final ProcessHandler procHdl = new ProcessHandler(cmdLines, (PrintWriter) log, null, 5000, param);
130cdf0e10cSrcweir             procHdl.runCommand();
131cdf0e10cSrcweir 
132cdf0e10cSrcweir             if (mDebug) {
133cdf0e10cSrcweir                 log.println("---> Output of command:");
134cdf0e10cSrcweir                 log.println(procHdl.getOutputText());
135cdf0e10cSrcweir                 log.println("<--- Output of command:");
136cdf0e10cSrcweir                 log.println("---> Error output of command");
137cdf0e10cSrcweir                 log.println(procHdl.getErrorText());
138cdf0e10cSrcweir                 log.println("<--- Error output of command");
139cdf0e10cSrcweir             }
140cdf0e10cSrcweir             final String output = procHdl.getOutputText();
141cdf0e10cSrcweir             final String[] outs = output.split("\n");
142cdf0e10cSrcweir 
143cdf0e10cSrcweir             for (int i = 0; i < outs.length; i++) {
144cdf0e10cSrcweir                 final String line = outs[i];
145cdf0e10cSrcweir                 if (line.startsWith("SRC_ROOT")) {
146cdf0e10cSrcweir                     sSrcRoot = getEnvValue(line);
147cdf0e10cSrcweir                 }
148cdf0e10cSrcweir             }
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir         return sSrcRoot;
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     private String[] getCmdLinesWithCommand(String[] commands) {
154cdf0e10cSrcweir         String[] cmdLines = null;
155cdf0e10cSrcweir         log.println("prepare command for platform " + mPlatform);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         String seperator = "";
158cdf0e10cSrcweir         if (mPlatform.equals(PropertyName.WNTMSCI)) {
159cdf0e10cSrcweir             seperator = mCygwin ? ";" : "^";
160cdf0e10cSrcweir         } else {
161cdf0e10cSrcweir             seperator = ";";
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         String command = "";
165cdf0e10cSrcweir         for (int i = 0; i < commands.length; i++) {
166cdf0e10cSrcweir             if (i != 0) {
167cdf0e10cSrcweir                 command += seperator;
168cdf0e10cSrcweir             }
169cdf0e10cSrcweir             command += commands[i];
170cdf0e10cSrcweir         }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir         if (mPlatform.equals(PropertyName.WNTMSCI)){
173cdf0e10cSrcweir             if (mCygwin){
174cdf0e10cSrcweir                 String srcRoot = (String) param.get(PropertyName.SRC_ROOT);
175cdf0e10cSrcweir                 String envSet = "export cyg_src_root=`cygpath '" + srcRoot.replaceAll("\\\\", "\\\\\\\\")+ "'`; source $cyg_src_root/winenv.set.sh;";
176cdf0e10cSrcweir                 command = envSet + command;
177cdf0e10cSrcweir                 cmdLines = new String[]{mShell, "--login", "-c", "\"" + command + "\""};
178cdf0e10cSrcweir             } else {
179cdf0e10cSrcweir             cmdLines = new String[]{mShell, "/C", "\"" + command + "\""};
180cdf0e10cSrcweir             }
181cdf0e10cSrcweir         } else {
182cdf0e10cSrcweir             cmdLines = new String[]{mShell, "-c", command};
183cdf0e10cSrcweir         }
184cdf0e10cSrcweir         return cmdLines;
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     private String getEnvValue(String line) {
188cdf0e10cSrcweir         final String[] split = line.split("=");
189cdf0e10cSrcweir         return split[1];
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir }
192