summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/bitbake/ShellSession.java
blob: 0e262b143ae45c618584a3fd5fd253c93bc12dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*****************************************************************************
 * Copyright (c) 2013 Ken Gilmer, Intel Corporation
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Ken Gilmer - initial API and implementation
 *     Ioana Grigoropol (Intel) - adapt class for remote support
 *******************************************************************************/
package org.yocto.bc.bitbake;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Writer;
	
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.rse.core.model.IHost;
import org.eclipse.rse.services.files.IHostFile;
import org.yocto.bc.ui.model.ProjectInfo;
import org.yocto.remote.utils.ICommandResponseHandler;
import org.yocto.remote.utils.RemoteHelper;
import org.yocto.remote.utils.YoctoCommand;

/**
 * A class for Linux shell sessions.
 * @author kgilmer
 *
 */
public class ShellSession {
	private volatile boolean interrupt = false;
	/**
	 * String used to isolate command execution
	 */
	public static final String TERMINATOR = "#234o987dsfkcqiuwey18837032843259d";
	public static final String LT = System.getProperty("line.separator");
	public static final String exportCmd = "export BB_ENV_EXTRAWHITE=\\\"DISABLE_SANITY_CHECKS $BB_ENV_EXTRAWHITE\\\"";
	public static final String exportColumnsCmd = "export COLUMNS=1000";
	private static final String BUILD_DIR = "/build/";

	
	public static String getFilePath(String file) throws IOException {
		File f = new File(file);
		
		if (!f.exists() || f.isDirectory()) {
			throw new IOException("Path passed is not a file: " + file);
		}
		
		StringBuffer sb = new StringBuffer();
		
		String elems[] = file.split("//");
		
		for (int i = 0; i < elems.length - 1; ++i) {
			sb.append(elems[i]);
			sb.append("//");
		}
		
		return sb.toString();
	}
	private Process process;

	private OutputStream pos = null;
	private String shellPath = null;
	private final String initCmd;
	private final IHostFile root;
	private ProjectInfo projectInfo;

	public ProjectInfo getProjectInfo() {
		return projectInfo;
	}

	public void setProjectInfo(ProjectInfo projectInfo) {
		this.projectInfo = projectInfo;
	}

	public ShellSession(ProjectInfo pInfo, IHostFile root, String initCmd) throws IOException {
		this.projectInfo = pInfo;
		this.root = root;
		this.initCmd  = initCmd;

		initializeShell(new NullProgressMonitor());
	}

	private void initializeShell(IProgressMonitor monitor) throws IOException {
		try {
			if (root != null) {
				IHost connection = projectInfo.getConnection();
				RemoteHelper.handleRunCommandRemote(connection, new YoctoCommand("source " + initCmd, root.getAbsolutePath(), ""), monitor);
				RemoteHelper.handleRunCommandRemote(connection,  new YoctoCommand(exportCmd, root.getAbsolutePath(), ""), monitor);
			} else {
				throw new Exception("Root file not found!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	synchronized 
	public String execute(String command) throws IOException {
		return execute(command, false);
	}

	synchronized 
	public String execute(String command, boolean hasErrors) throws IOException {
		try {
			if (projectInfo.getConnection() != null) {
				command = getInitCmd() + command;
				RemoteHelper.handleRunCommandRemote(projectInfo.getConnection(), new YoctoCommand(command, getBuildDirAbsolutePath(), ""), new NullProgressMonitor());
				return getBuildDirAbsolutePath();
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private String getBuildDirAbsolutePath() {
		return root.getAbsolutePath() + BUILD_DIR;
	}

	private String getInitCmd() {
		return "source " + initCmd + " " + getBuildDirAbsolutePath()
				+ " > tempsf; rm -rf tempsf;" + exportCmd + ";"
				+ exportColumnsCmd + ";" + "cd " + getBuildDirAbsolutePath()
				+ ";";
	}
	
	synchronized 
	public void execute(String command, String terminator, ICommandResponseHandler handler) throws IOException {
		interrupt = false;
		InputStream errIs = process.getErrorStream();
		if (errIs.available() > 0) {
			clearErrorStream(errIs);
		}
		sendToProcessAndTerminate(command);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
		String std = null;

		do {		
			if (errIs.available() > 0) {
				byte[] msg = new byte[errIs.available()];

				errIs.read(msg, 0, msg.length);
				handler.response(new String(msg), true);
			} 
			
			std = br.readLine();
			
			if (std != null && !std.endsWith(terminator)) {
				handler.response(std, false);
			} 
			
		} while (std != null && !std.endsWith(terminator) && !interrupt);
		
		if (interrupt) {
			process.destroy();
			initializeShell(null);
			interrupt = false;
		}
	}
	
	private void clearErrorStream(InputStream is) {
	
		try {
			byte b[] = new byte[is.available()];
			is.read(b);			
			System.out.println("clearing: " + new String(b));
		} catch (IOException e) {
			e.printStackTrace();
			//Ignore any error
		}
	}

	/**
	 * Send command string to shell process and add special terminator string so
	 * reader knows when output is complete.
	 * 
	 * @param command
	 * @throws IOException
	 */
	private void sendToProcessAndTerminate(String command) throws IOException {
		pos.write(command.getBytes());
		pos.write(LT.getBytes());
		pos.flush();
		pos.write("echo $?".getBytes());
		pos.write(TERMINATOR.getBytes());
		pos.write(LT.getBytes());
		pos.flush();
	}

	/**
	 * Interrupt any running processes.
	 */
	public void interrupt() {
		interrupt = true;
	}
	
	public void printError(String errorLines) {
		RemoteHelper.getCommandHandler(projectInfo.getConnection()).response(errorLines, true);
	}
}