-
Notifications
You must be signed in to change notification settings - Fork 134
IEP-1074: Python dropdown removed #856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,10 @@ | |
| *******************************************************************************/ | ||
| package com.espressif.idf.ui.update; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.InputStreamReader; | ||
| import java.util.Map; | ||
|
|
||
| import org.eclipse.core.runtime.Platform; | ||
| import org.eclipse.core.runtime.preferences.ConfigurationScope; | ||
| import org.eclipse.jface.dialogs.Dialog; | ||
| import org.eclipse.jface.dialogs.IDialogConstants; | ||
|
|
@@ -19,7 +20,6 @@ | |
| import org.eclipse.swt.layout.GridData; | ||
| import org.eclipse.swt.layout.GridLayout; | ||
| import org.eclipse.swt.widgets.Button; | ||
| import org.eclipse.swt.widgets.Combo; | ||
| import org.eclipse.swt.widgets.Composite; | ||
| import org.eclipse.swt.widgets.Control; | ||
| import org.eclipse.swt.widgets.DirectoryDialog; | ||
|
|
@@ -41,25 +41,22 @@ | |
| public class DirectorySelectionDialog extends TitleAreaDialog | ||
| { | ||
|
|
||
| private Shell shell; | ||
| private Text text; | ||
| private String idfDirPath; | ||
| private String pythonExecutablePath; | ||
| private Combo pythonVersionCombo; | ||
| private Map<String, String> pythonVersions; | ||
| private String gitPath; | ||
| private Text gitLocationtext; | ||
| private Text pythonLocationtext; | ||
| private String commandId; | ||
| private static final String pythonPathNodeKey = "PYTHON_EXECUTABLE"; //$NON-NLS-1$ | ||
| private static final String gitPathNodeKey = "GIT_EXECUTABLE"; //$NON-NLS-1$ | ||
|
|
||
| protected DirectorySelectionDialog(Shell parentShell, String commandId, String pythonExecutablePath, | ||
| Map<String, String> pythonVersions, String idfPath, String gitExecutablePath) | ||
| { | ||
| super(parentShell); | ||
| setShellStyle(getShellStyle() | SWT.RESIZE); | ||
| this.shell = parentShell; | ||
| this.pythonExecutablePath = getPythonPreferenceOrDefault(pythonExecutablePath); | ||
| this.pythonVersions = pythonVersions; | ||
| this.idfDirPath = idfPath; | ||
|
|
@@ -154,76 +151,65 @@ public void widgetSelected(SelectionEvent event) | |
| }); | ||
|
|
||
| // Python version selection | ||
| if (Platform.OS_WIN32.equals(Platform.getOS()) && pythonVersions != null && !pythonVersions.isEmpty()) | ||
| { | ||
| new Label(composite, SWT.NONE).setText(Messages.DirectorySelectionDialog_ChoosePyVersion); | ||
| addPythonVersionSelectionControls(composite); | ||
|
|
||
| pythonVersionCombo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY); | ||
| GridData gridData = new GridData(SWT.NONE, SWT.NONE, true, false, 2, 1); | ||
| gridData.widthHint = 250; | ||
| pythonVersionCombo.setLayoutData(gridData); | ||
| Dialog.applyDialogFont(composite); | ||
| return composite; | ||
| } | ||
|
|
||
| String[] versions = pythonVersions.keySet().toArray(new String[pythonVersions.size()]); | ||
| pythonVersionCombo.setItems(versions); | ||
| pythonVersionCombo.select(0); // select the first one | ||
| private void addPythonVersionSelectionControls(Composite composite) | ||
| { | ||
| // Python executable location | ||
| new Label(composite, SWT.NONE).setText(Messages.DirectorySelectionDialog_PyExeLocation); | ||
|
|
||
| } | ||
| else | ||
| pythonLocationtext = new Text(composite, SWT.BORDER); | ||
| GridData data = new GridData(); | ||
| data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); | ||
| pythonLocationtext.setLayoutData(data); | ||
|
|
||
| pythonLocationtext.setText(pythonExecutablePath != null ? pythonExecutablePath : StringUtil.EMPTY); | ||
| pythonLocationtext.addModifyListener(new ModifyListener() | ||
| { | ||
| new Label(composite, SWT.NONE).setText(Messages.DirectorySelectionDialog_PyExeLocation); | ||
|
|
||
| pythonLocationtext = new Text(composite, SWT.BORDER); | ||
| data = new GridData(); | ||
| data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); | ||
| pythonLocationtext.setLayoutData(data); | ||
| pythonLocationtext.setText(pythonExecutablePath != null ? pythonExecutablePath : StringUtil.EMPTY); | ||
| pythonLocationtext.addModifyListener(new ModifyListener() | ||
| @Override | ||
| public void modifyText(ModifyEvent e) | ||
| { | ||
| @Override | ||
| public void modifyText(ModifyEvent e) | ||
| { | ||
| validate(); | ||
| } | ||
| }); | ||
| validate(); | ||
| } | ||
| }); | ||
|
|
||
| Button pyBrowseBtn = new Button(composite, SWT.PUSH); | ||
| pyBrowseBtn.setText(Messages.DirectorySelectionDialog_Browse); | ||
| pyBrowseBtn.addSelectionListener(new SelectionAdapter() | ||
| Button pyBrowseBtn = new Button(composite, SWT.PUSH); | ||
| pyBrowseBtn.setText(Messages.DirectorySelectionDialog_Browse); | ||
| pyBrowseBtn.addSelectionListener(new SelectionAdapter() | ||
| { | ||
| @Override | ||
| public void widgetSelected(SelectionEvent event) | ||
| { | ||
| @Override | ||
| public void widgetSelected(SelectionEvent event) | ||
| FileDialog dlg = new FileDialog(Display.getDefault().getActiveShell()); | ||
| dlg.setText(Messages.DirectorySelectionDialog_PyExecutableLocation); | ||
| String pythonLocationPathString = dlg.open(); | ||
| if (pythonLocationPathString != null) | ||
| { | ||
| FileDialog dlg = new FileDialog(Display.getDefault().getActiveShell()); | ||
| dlg.setText(Messages.DirectorySelectionDialog_PyExecutableLocation); | ||
|
|
||
| String dir = dlg.open(); | ||
| if (dir != null) | ||
| { | ||
| pythonLocationtext.setText(dir); | ||
| } | ||
| pythonLocationtext.setText(pythonLocationPathString); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| Dialog.applyDialogFont(composite); | ||
| return composite; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| protected void validate() | ||
| { | ||
| idfDirPath = text.getText(); | ||
| if (pythonVersionCombo != null) | ||
| { | ||
| String version = pythonVersionCombo.getText(); | ||
| pythonExecutablePath = pythonVersions.getOrDefault(version, null); | ||
| } | ||
| else | ||
| pythonExecutablePath = pythonLocationtext.getText(); | ||
|
|
||
| gitPath = gitLocationtext.getText(); | ||
|
|
||
| boolean isValidPythonPath = validatePythonExecutable(pythonExecutablePath); | ||
|
|
||
| if (!isValidPythonPath) | ||
| { | ||
| pythonExecutablePath = pythonLocationtext.getText(); | ||
| setErrorMessage(Messages.DirectorySelectionDialog_InvalidPythonPath); | ||
| getButton(IDialogConstants.OK_ID).setEnabled(false); | ||
| } | ||
| gitPath = gitLocationtext.getText(); | ||
|
|
||
| if (StringUtil.isEmpty(pythonExecutablePath) || StringUtil.isEmpty(gitPath) || StringUtil.isEmpty(idfDirPath)) | ||
| else if (StringUtil.isEmpty(pythonExecutablePath) || StringUtil.isEmpty(gitPath) || StringUtil.isEmpty(idfDirPath)) | ||
| { | ||
| setErrorMessage(Messages.DirectorySelectionDialog_CantbeEmpty); | ||
| getButton(IDialogConstants.OK_ID).setEnabled(false); | ||
|
|
@@ -254,12 +240,12 @@ private String getPythonPreferenceOrDefault(String pythonExecutablePath) | |
| { | ||
| return getPreferences().get(pythonPathNodeKey, pythonExecutablePath); | ||
| } | ||
| private String getGitPreferenceOrDefault(String gitExecutablePath) | ||
|
|
||
| private String getGitPreferenceOrDefault(String gitExecutablePath) | ||
| { | ||
| return getPreferences().get(gitPathNodeKey, gitExecutablePath); | ||
| } | ||
|
|
||
| public String getIDFDirectory() | ||
| { | ||
| return idfDirPath; | ||
|
|
@@ -279,15 +265,8 @@ public String getGitExecutable() | |
| protected void okPressed() | ||
| { | ||
| idfDirPath = text.getText(); | ||
| if (pythonVersionCombo != null) | ||
| { | ||
| String version = pythonVersionCombo.getText(); | ||
| pythonExecutablePath = pythonVersions.getOrDefault(version, null); | ||
| } | ||
| else | ||
| { | ||
| pythonExecutablePath = pythonLocationtext.getText(); | ||
| } | ||
| pythonExecutablePath = pythonLocationtext.getText(); | ||
|
|
||
| gitPath = gitLocationtext.getText(); | ||
|
|
||
| super.okPressed(); | ||
|
|
@@ -313,4 +292,20 @@ private Preferences getPreferences() | |
| { | ||
| return ConfigurationScope.INSTANCE.getNode(UIPlugin.PLUGIN_ID).node("preference"); //$NON-NLS-1$ | ||
| } | ||
|
|
||
| private boolean validatePythonExecutable(String path) | ||
| { | ||
| try | ||
| { | ||
| Process process = new ProcessBuilder(path, "--version").start(); | ||
| BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
| String output = reader.readLine(); | ||
| int exitCode = process.waitFor(); | ||
| return exitCode == 0 && output.startsWith("Python"); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
Comment on lines
+296
to
+310
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The private boolean validatePythonExecutable(String path) {
try {
Process process = new ProcessBuilder(path, "--version").start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String output = reader.readLine();
String errorOutput = errorReader.readLine();
int exitCode = process.waitFor();
return exitCode == 0 && (output != null && output.startsWith("Python") || errorOutput != null && errorOutput.startsWith("Python"));
} catch (Exception e) {
return false;
}
} |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
validatemethod has been updated to include validation for the new Python executable path. However, the validation logic only checks if the path is not empty and if it is a valid executable. It might be beneficial to also check if the path actually points to a Python executable, as the current validation could pass for any executable file.