bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / runner / helper / BuildEnvTools.java
blobb35d14cb3f1c17fb38a45ed2770ade3c80c54cd3
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package helper;
20 import java.io.File;
21 import java.io.PrintWriter;
22 import lib.TestParameters;
23 import share.LogWriter;
24 import util.*;
26 /**
27 * This class support you to execute some shell commands in a buld environment. At ervery call of commands
28 * a build environment was created and the commands will be executed.
31 public class BuildEnvTools {
33 private final TestParameters param;
34 private final LogWriter log;
35 private final boolean mDebug;
36 private final String mPlatform;
37 private final String mShell;
38 private boolean mCygwin;
40 /**
41 * This constructor creates an instance of BuildEncTools. It is verifying for all neccesarry
42 * parameters in <CODE>TestParameters</CODE> This must be:
43 * <ul>
44 * <li>OperatingSystem: Fill this parameter with an operating system like unxsols, unxsoli, unxlngi or wntmsci.
45 * </li>
46 * <li> Shell: Fill this parameter with a shell f.e '/bin/tcsh'
47 * or 'c:\\myShell\\myShell.exe'
48 * </li>
49 * @param param
50 * @param log
51 * @throws helper.ParameterNotFoundException
53 public BuildEnvTools(TestParameters param, LogWriter log) throws ParameterNotFoundException {
54 this.param = param;
55 this.log = log;
56 mDebug = param.getBool(PropertyName.DEBUG_IS_ACTIVE);
58 boolean error = false;
60 String msg = "\nERROR: the following parameter must be set before executing the test:\n\n";
62 mPlatform = (String) param.get(PropertyName.OPERATING_SYSTEM);
63 if (mDebug) {
64 log.println("### " + mPlatform);
66 if (mPlatform == null){
67 msg += PropertyName.OPERATING_SYSTEM + "\nFill this parameter with an operating system like unxsols," +
68 " unxsoli, unxlngi, unxmacxi or wntmsci. \n\n";
70 if(
71 (!mPlatform.equalsIgnoreCase(PropertyName.UNXSOLS)) &&
72 (!mPlatform.equalsIgnoreCase(PropertyName.UNXSOLI)) &&
73 (!mPlatform.equalsIgnoreCase(PropertyName.UNXLNGI)) &&
74 (!mPlatform.equalsIgnoreCase(PropertyName.UNXMACXI))&&
75 (!mPlatform.equalsIgnoreCase(PropertyName.WNTMSCI)) ){
77 msg += PropertyName.OPERATING_SYSTEM + ":" + mPlatform + "\nFill this parameter with an operating system like unxsols," +
78 " unxsoli, unxlngi, unxmacxi or wntmsci. \n\n";
79 error = true;
82 mShell = (String) param.get(PropertyName.SHELL);
83 if (mShell == null) {
84 msg += PropertyName.SHELL + "\nFill this parameter with a shell" +
85 "\n\t/bin/tcsh c:\\myShell\\myShell.exe\n\n";
86 error = true;
89 mCygwin = (param.getBool(PropertyName.CYGWIN));
91 if (error) {
92 throw new ParameterNotFoundException(msg);
96 /**
97 * Executes the given commands in OOo-Environment shell.
98 * @param commands
99 * @param workDir
100 * @param shortWait
101 * @return the processHandler of the commands
102 * @see helper.ProcessHandler
104 public ProcessHandler runCommandsInEnvironmentShell(String[] commands, File workDir, int shortWait) {
106 final String[] cmdLines = getCmdLinesWithCommand(commands);
107 final ProcessHandler pHdl = new ProcessHandler(cmdLines, (PrintWriter) log, workDir, shortWait, param);
108 pHdl.runCommand();
109 return pHdl;
112 public String getSrcRoot() {
114 String sSrcRoot = (String) param.get(PropertyName.SRC_ROOT);
116 if (sSrcRoot == null) {
117 String[] cmdLines = null;
118 if (mPlatform.equals(PropertyName.WNTMSCI) && ! mCygwin) {
119 cmdLines = new String[]{mShell, "/C", "echo SRC_ROOT=%SRC_ROOT"};
120 } else {
121 cmdLines = new String[]{mShell, "--login ", "-c ", "echo \"SRC_ROOT=$SRC_ROOT\""};
124 final ProcessHandler procHdl = new ProcessHandler(cmdLines, (PrintWriter) log, null, 5000, param);
125 procHdl.runCommand();
127 if (mDebug) {
128 log.println("---> Output of command:");
129 log.println(procHdl.getOutputText());
130 log.println("<--- Output of command:");
131 log.println("---> Error output of command");
132 log.println(procHdl.getErrorText());
133 log.println("<--- Error output of command");
135 final String output = procHdl.getOutputText();
136 final String[] outs = output.split("\n");
138 for (int i = 0; i < outs.length; i++) {
139 final String line = outs[i];
140 if (line.startsWith("SRC_ROOT")) {
141 sSrcRoot = getEnvValue(line);
145 return sSrcRoot;
148 private String[] getCmdLinesWithCommand(String[] commands) {
149 String[] cmdLines = null;
150 log.println("prepare command for platform " + mPlatform);
152 String separator = "";
153 if (mPlatform.equals(PropertyName.WNTMSCI)) {
154 separator = mCygwin ? ";" : "^";
155 } else {
156 separator = ";";
159 String command = "";
160 for (int i = 0; i < commands.length; i++) {
161 if (i != 0) {
162 command += separator;
164 command += commands[i];
167 if (mPlatform.equals(PropertyName.WNTMSCI)){
168 if (mCygwin){
169 String srcRoot = (String) param.get(PropertyName.SRC_ROOT);
170 String envSet = "export cyg_src_root=`cygpath '" + srcRoot.replaceAll("\\\\", "\\\\\\\\")+ "'`; source $cyg_src_root/winenv.set.sh;";
171 command = envSet + command;
172 cmdLines = new String[]{mShell, "--login", "-c", "\"" + command + "\""};
173 } else {
174 cmdLines = new String[]{mShell, "/C", "\"" + command + "\""};
176 } else {
177 cmdLines = new String[]{mShell, "-c", command};
179 return cmdLines;
182 private String getEnvValue(String line) {
183 final String[] split = line.split("=");
184 return split[1];