Skip to content

Commit

Permalink
IEP-720: Fix for python script errors (#560)
Browse files Browse the repository at this point in the history
* IEP-720: Fix for python script errors
  • Loading branch information
alirana01 committed Aug 2, 2022
1 parent 4add476 commit 33a502a
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 34 deletions.
Expand Up @@ -49,4 +49,8 @@ public static IStatus errorStatus(String msg, Exception e) {
return new Status(IStatus.ERROR, PLUGIN_ID, msg, e);
}

public static IStatus okStatus(String msg, Exception e)
{
return new Status(IStatus.OK, PLUGIN_ID, msg, e);
}
}
Expand Up @@ -149,7 +149,7 @@ protected String getPythonExecutablePath()
*/
protected abstract void execute();

protected void runCommand(List<String> arguments)
protected IStatus runCommand(List<String> arguments)
{
ProcessBuilderFactory processRunner = new ProcessBuilderFactory();

Expand All @@ -174,17 +174,18 @@ protected void runCommand(List<String> arguments)
if (status == null)
{
Logger.log(IDFCorePlugin.getPlugin(), IDFCorePlugin.errorStatus("Status can't be null", null)); //$NON-NLS-1$
return;
return IDFCorePlugin.errorStatus("Status can't be null", null); //$NON-NLS-1$
}

console.println(status.getMessage());
console.println();


return IDFCorePlugin.okStatus(status.getMessage(), null);
}
catch (Exception e1)
{
Logger.log(IDFCorePlugin.getPlugin(), e1);

return IDFCorePlugin.errorStatus(e1.getMessage(), e1);
}
}

Expand Down
Expand Up @@ -38,7 +38,7 @@ public class ExportIDFTools
* @param gitExePath git executable full path
* @param console Console stream to write messages
*/
public void runToolsExport(final String pythonExePath, final String gitExePath, final MessageConsoleStream console)
public IStatus runToolsExport(final String pythonExePath, final String gitExePath, final MessageConsoleStream console)
{
final List<String> arguments = new ArrayList<>();
arguments.add(pythonExePath);
Expand All @@ -61,19 +61,26 @@ public void runToolsExport(final String pythonExePath, final String gitExePath,
if (status == null)
{
Logger.log(IDFCorePlugin.getPlugin(), IDFCorePlugin.errorStatus("Status can't be null", null)); //$NON-NLS-1$
return;
return IDFCorePlugin.errorStatus("Status can't be null", null); //$NON-NLS-1$
}

if (status.getSeverity() == IStatus.ERROR)
{
return status;
}

// process export command output
final String exportCmdOp = status.getMessage();
log(exportCmdOp, console);
processExportCmdOutput(exportCmdOp, gitExePath);

return status;
}
catch (IOException e1)
{
Logger.log(IDFCorePlugin.getPlugin(), e1);
return IDFCorePlugin.errorStatus(e1.getMessage(), e1);
}

}

private void log(final String cmd, final MessageConsoleStream console)
Expand Down
Expand Up @@ -14,6 +14,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.cdt.cmake.core.ICMakeToolChainManager;
import org.eclipse.cdt.core.CCorePlugin;
Expand All @@ -22,6 +23,8 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.swt.SWT;
Expand Down Expand Up @@ -65,22 +68,36 @@ protected IStatus run(IProgressMonitor monitor)
monitor.beginTask(Messages.InstallToolsHandler_ItWilltakeTimeMsg, 5);
monitor.worked(1);

handleToolsInstall();
monitor.worked(1);
IStatus status = handleToolsInstall();
if (status.getSeverity() == IStatus.ERROR)
{
return status;
}

monitor.worked(1);
monitor.setTaskName(Messages.InstallToolsHandler_InstallingPythonMsg);
handleToolsInstallPython();
status = handleToolsInstallPython();
if (status.getSeverity() == IStatus.ERROR)
{
return status;
}

monitor.worked(1);

monitor.setTaskName(Messages.InstallToolsHandler_ExportingPathsMsg);
new ExportIDFTools().runToolsExport(pythonExecutablenPath, gitExecutablePath, console);
status = new ExportIDFTools().runToolsExport(pythonExecutablenPath, gitExecutablePath, console);
if (status.getSeverity() == IStatus.ERROR)
{
return status;
}

monitor.worked(1);
console.println(Messages.InstallToolsHandler_ConfiguredBuildEnvVarMsg);

monitor.setTaskName(Messages.InstallToolsHandler_AutoConfigureToolchain);
configureToolChain();
monitor.worked(1);

configEnv();

monitor.setTaskName(Messages.InstallToolsHandler_InstallingWebscoketMsg);
Expand All @@ -105,6 +122,7 @@ protected IStatus run(IProgressMonitor monitor)
{
Logger.log(e);
}
installToolsJob.addJobChangeListener(new ToolInstallListener());
installToolsJob.schedule();
}

Expand All @@ -114,10 +132,10 @@ protected IStatus run(IProgressMonitor monitor)
protected void configEnv()
{
IDFEnvironmentVariables idfEnvMgr = new IDFEnvironmentVariables();
//Enable IDF_COMPONENT_MANAGER by default

// Enable IDF_COMPONENT_MANAGER by default
idfEnvMgr.addEnvVariable(IDFEnvironmentVariables.IDF_COMPONENT_MANAGER, "1");

}

private void copyOpenOcdRules()
Expand All @@ -137,7 +155,7 @@ private void copyOpenOcdRules()
Path target = Paths.get("/etc/udev/rules.d/60-openocd.rules"); //$NON-NLS-1$
console.println(String.format(Messages.InstallToolsHandler_OpenOCDRulesCopyPaths, source.toString(),
target.toString()));

Display.getDefault().syncExec(new Runnable()
{
@Override
Expand All @@ -147,13 +165,14 @@ public void run()
{
if (target.toFile().exists())
{
MessageBox messageBox = new MessageBox(Display.getDefault().getActiveShell(), SWT.ICON_WARNING | SWT.YES | SWT.NO);
MessageBox messageBox = new MessageBox(Display.getDefault().getActiveShell(),
SWT.ICON_WARNING | SWT.YES | SWT.NO);
messageBox.setText(Messages.InstallToolsHandler_OpenOCDRulesCopyWarning);
messageBox.setMessage(Messages.InstallToolsHandler_OpenOCDRulesCopyWarningMessage);
int response = messageBox.open();
if (response == SWT.YES)
{
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
}
else
{
Expand All @@ -163,9 +182,9 @@ public void run()
}
else
{
Files.copy(source, target);
Files.copy(source, target);
}

console.println(Messages.InstallToolsHandler_OpenOCDRulesCopied);
}
catch (IOException e)
Expand All @@ -190,7 +209,7 @@ protected void configureToolChain()
toolchainManager.initCMakeToolChain(tcManager, cmakeTcManager);
}

protected void handleToolsInstall()
protected IStatus handleToolsInstall()
{
// idf_tools.py install all
List<String> arguments = new ArrayList<String>();
Expand All @@ -199,20 +218,19 @@ protected void handleToolsInstall()

console.println(Messages.InstallToolsHandler_InstallingToolsMsg);
console.println(Messages.InstallToolsHandler_ItWilltakeTimeMsg);
runCommand(arguments);

return runCommand(arguments);
}

protected void handleToolsInstallPython()
protected IStatus handleToolsInstallPython()
{
List<String> arguments;
// idf_tools.py install-python-env
arguments = new ArrayList<String>();
arguments.add(IDFConstants.TOOLS_INSTALL_PYTHON_CMD);
runCommand(arguments);
return runCommand(arguments);
}
protected void handleWebSocketClientInstall()

protected IStatus handleWebSocketClientInstall()
{

// pip install websocket-client
Expand All @@ -222,14 +240,15 @@ protected void handleWebSocketClientInstall()
{
console.println(String.format("%s executable not found. Unable to run `%s -m pip install websocket-client`", //$NON-NLS-1$
IDFConstants.PYTHON_CMD, IDFConstants.PYTHON_CMD));
return;
return IDFCorePlugin.errorStatus(String.format("%s executable not found. Unable to run `%s -m pip install websocket-client`", //$NON-NLS-1$
IDFConstants.PYTHON_CMD, IDFConstants.PYTHON_CMD), null);
}
arguments.add(pythonEnvPath);
arguments.add("-m"); //$NON-NLS-1$
arguments.add("pip"); //$NON-NLS-1$
arguments.add("install"); //$NON-NLS-1$
arguments.add("websocket-client"); //$NON-NLS-1$

ProcessBuilderFactory processRunner = new ProcessBuilderFactory();

try
Expand All @@ -244,21 +263,92 @@ protected void handleWebSocketClientInstall()
IStatus status = processRunner.runInBackground(arguments, org.eclipse.core.runtime.Path.ROOT, environment);
if (status == null)
{
Logger.log(IDFCorePlugin.getPlugin(), IDFCorePlugin.errorStatus("Unable to get the process status.", null)); //$NON-NLS-1$
Logger.log(IDFCorePlugin.getPlugin(),
IDFCorePlugin.errorStatus("Unable to get the process status.", null)); //$NON-NLS-1$
console.println("Unable to get the process status.");
return;
return IDFCorePlugin.errorStatus("Unable to get the process status.", null); //$NON-NLS-1$ ;
}

console.println(status.getMessage());

return status;
}
catch (Exception e1)
{
Logger.log(IDFCorePlugin.getPlugin(), e1);
console.println(e1.getLocalizedMessage());

return IDFCorePlugin.errorStatus(e1.getLocalizedMessage(), e1); // $NON-NLS-1$;
}
console.println();
}

private class ToolInstallListener implements IJobChangeListener
{
Map<String, String> existingVarMap;

@Override
public void aboutToRun(IJobChangeEvent event)
{
this.existingVarMap = loadExistingVars();
}

private Map<String, String> loadExistingVars()
{
IDFEnvironmentVariables idfEnvironmentVariables = new IDFEnvironmentVariables();
Map<String, String> existingVarMap = new HashMap<>();

existingVarMap.put(IDFEnvironmentVariables.IDF_COMPONENT_MANAGER,
idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.IDF_COMPONENT_MANAGER));
existingVarMap.put(IDFEnvironmentVariables.IDF_PATH,
idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.IDF_PATH));
existingVarMap.put(IDFEnvironmentVariables.IDF_PYTHON_ENV_PATH,
idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.IDF_PYTHON_ENV_PATH));
existingVarMap.put(IDFEnvironmentVariables.OPENOCD_SCRIPTS,
idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.OPENOCD_SCRIPTS));
existingVarMap.put(IDFEnvironmentVariables.PATH,
idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.PATH));

return existingVarMap;
}

@Override
public void awake(IJobChangeEvent event)
{

}

@Override
public void done(IJobChangeEvent event)
{
if (event.getResult().getSeverity() == IStatus.ERROR)
{
restoreOldVars();
}
}

private void restoreOldVars()
{
IDFEnvironmentVariables idfEnvironmentVariables = new IDFEnvironmentVariables();
for (Entry<String, String> varsEntry : existingVarMap.entrySet())
{
idfEnvironmentVariables.addEnvVariable(varsEntry.getKey(), varsEntry.getValue());
}
}

@Override
public void running(IJobChangeEvent event)
{

}

@Override
public void scheduled(IJobChangeEvent event)
{

}

@Override
public void sleeping(IJobChangeEvent event)
{

}
}
}

0 comments on commit 33a502a

Please sign in to comment.