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