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.WNTMSCI)) ){
81 
82             msg += PropertyName.OPERATING_SYSTEM + ":" + mPlatform + "\nFill this parameter with an operating system like unxsols," +
83                 " unxsoli, unxlngi, unxmacxi or wntmsci.  \n\n";
84             error = true;
85         }
86 
87         mShell = (String) param.get(PropertyName.SHELL);
88         if (mShell == null) {
89             msg += PropertyName.SHELL + "\nFill this parameter with a shell" +
90                 "\n\t/bin/tcsh c:\\myShell\\myShell.exe\n\n";
91             error = true;
92         }
93 
94         mCygwin = (param.getBool(PropertyName.CYGWIN));
95 
96         if (error) {
97             throw new ParameterNotFoundException(msg);
98         }
99     }
100 
101     /**
102      * Executes the given commands in OOo-Environment shell.
103      * @param commands
104      * @param workDir
105      * @param shortWait
106      * @return the processHandler of the commands
107      * @see helper.ProcessHandler
108      */
runCommandsInEnvironmentShell(String[] commands, File workDir, int shortWait)109     public ProcessHandler runCommandsInEnvironmentShell(String[] commands, File workDir, int shortWait) {
110 
111         final String[] cmdLines = getCmdLinesWithCommand(commands);
112         final ProcessHandler pHdl = new ProcessHandler(cmdLines, (PrintWriter) log, workDir, shortWait, param);
113         pHdl.runCommand();
114         return pHdl;
115     }
116 
getSrcRoot()117     public String getSrcRoot() {
118 
119         String sSrcRoot = (String) param.get(PropertyName.SRC_ROOT);
120 
121         if (sSrcRoot == null) {
122             String[] cmdLines = null;
123             if (mPlatform.equals(PropertyName.WNTMSCI) && ! mCygwin) {
124                 cmdLines = new String[]{mShell, "/C", "echo SRC_ROOT=%SRC_ROOT"};
125             } else {
126                 cmdLines = new String[]{mShell, "--login ", "-c ", "echo \"SRC_ROOT=$SRC_ROOT\""};
127             }
128 
129             final ProcessHandler procHdl = new ProcessHandler(cmdLines, (PrintWriter) log, null, 5000, param);
130             procHdl.runCommand();
131 
132             if (mDebug) {
133                 log.println("---> Output of command:");
134                 log.println(procHdl.getOutputText());
135                 log.println("<--- Output of command:");
136                 log.println("---> Error output of command");
137                 log.println(procHdl.getErrorText());
138                 log.println("<--- Error output of command");
139             }
140             final String output = procHdl.getOutputText();
141             final String[] outs = output.split("\n");
142 
143             for (int i = 0; i < outs.length; i++) {
144                 final String line = outs[i];
145                 if (line.startsWith("SRC_ROOT")) {
146                     sSrcRoot = getEnvValue(line);
147                 }
148             }
149         }
150         return sSrcRoot;
151     }
152 
getCmdLinesWithCommand(String[] commands)153     private String[] getCmdLinesWithCommand(String[] commands) {
154         String[] cmdLines = null;
155         log.println("prepare command for platform " + mPlatform);
156 
157         String seperator = "";
158         if (mPlatform.equals(PropertyName.WNTMSCI)) {
159             seperator = mCygwin ? ";" : "^";
160         } else {
161             seperator = ";";
162         }
163 
164         String command = "";
165         for (int i = 0; i < commands.length; i++) {
166             if (i != 0) {
167                 command += seperator;
168             }
169             command += commands[i];
170         }
171 
172         if (mPlatform.equals(PropertyName.WNTMSCI)){
173             if (mCygwin){
174                 String srcRoot = (String) param.get(PropertyName.SRC_ROOT);
175                 String envSet = "export cyg_src_root=`cygpath '" + srcRoot.replaceAll("\\\\", "\\\\\\\\")+ "'`; source $cyg_src_root/winenv.set.sh;";
176                 command = envSet + command;
177                 cmdLines = new String[]{mShell, "--login", "-c", "\"" + command + "\""};
178             } else {
179             cmdLines = new String[]{mShell, "/C", "\"" + command + "\""};
180             }
181         } else {
182             cmdLines = new String[]{mShell, "-c", command};
183         }
184         return cmdLines;
185     }
186 
getEnvValue(String line)187     private String getEnvValue(String line) {
188         final String[] split = line.split("=");
189         return split[1];
190     }
191 }
192