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