use of com.jetbrains.python.packaging.PyPackage in project intellij-community by JetBrains.
the class RestPythonUtil method updateSphinxQuickStartRequiredAction.
public static Presentation updateSphinxQuickStartRequiredAction(final AnActionEvent e) {
final Presentation presentation = e.getPresentation();
final Project project = e.getData(CommonDataKeys.PROJECT);
if (project != null) {
Module module = e.getData(LangDataKeys.MODULE);
if (module == null) {
Module[] modules = ModuleManager.getInstance(project).getModules();
module = modules.length == 0 ? null : modules[0];
}
if (module != null) {
final Sdk sdk = PythonSdkType.findPythonSdk(module);
if (sdk != null) {
final List<PyPackage> packages = PyPackageManager.getInstance(sdk).getPackages();
final PyPackage sphinx = packages != null ? PyPackageUtil.findPackage(packages, "Sphinx") : null;
presentation.setEnabled(sphinx != null);
}
}
}
return presentation;
}
use of com.jetbrains.python.packaging.PyPackage in project intellij-community by JetBrains.
the class IpnbConnectionManager method startIpythonServer.
public boolean startIpythonServer(@NotNull final String initUrl, @NotNull final IpnbFileEditor fileEditor) {
final Module module = ProjectFileIndex.SERVICE.getInstance(myProject).getModuleForFile(fileEditor.getVirtualFile());
if (module == null)
return false;
final Sdk sdk = PythonSdkType.findPythonSdk(module);
if (sdk == null) {
showWarning(fileEditor, "Please check Python Interpreter in Settings->Python Interpreter", null);
return false;
}
final List<PyPackage> packages = PyPackageManager.getInstance(sdk).getPackages();
final PyPackage ipythonPackage = packages != null ? PyPackageUtil.findPackage(packages, "ipython") : null;
final PyPackage jupyterPackage = packages != null ? PyPackageUtil.findPackage(packages, "jupyter") : null;
if (ipythonPackage == null && jupyterPackage == null) {
showWarning(fileEditor, "Add Jupyter to the interpreter of the current project.", null);
return false;
}
String url = showDialogUrl(initUrl);
if (url == null)
return false;
final IpnbSettings ipnbSettings = IpnbSettings.getInstance(myProject);
ipnbSettings.setURL(url);
final Pair<String, String> hostPort = getHostPortFromUrl(url);
if (hostPort == null) {
showWarning(fileEditor, "Please, check Jupyter Notebook URL in <a href=\"\">Settings->Tools->Jupyter Notebook</a>", new IpnbSettingsAdapter());
return false;
}
final String homePath = sdk.getHomePath();
if (homePath == null) {
showWarning(fileEditor, "Python Sdk is invalid, please check Python Interpreter in Settings->Python Interpreter", null);
return false;
}
Map<String, String> env = null;
final ArrayList<String> parameters = Lists.newArrayList(homePath);
String ipython = findJupyterRunner(homePath);
if (ipython == null) {
ipython = findIPythonRunner(homePath);
if (ipython == null) {
ipython = PythonHelper.LOAD_ENTRY_POINT.asParamString();
env = ImmutableMap.of("PYCHARM_EP_DIST", "ipython", "PYCHARM_EP_NAME", "ipython");
}
parameters.add(ipython);
parameters.add("notebook");
} else {
parameters.add(ipython);
}
parameters.add("--no-browser");
if (hostPort.getFirst() != null) {
parameters.add("--ip");
parameters.add(hostPort.getFirst());
}
if (hostPort.getSecond() != null) {
parameters.add("--port");
parameters.add(hostPort.getSecond());
}
final String arguments = ipnbSettings.getArguments();
if (!StringUtil.isEmptyOrSpaces(arguments)) {
parameters.addAll(StringUtil.split(arguments, " "));
}
final String directory = ipnbSettings.getWorkingDirectory();
final String baseDir = !StringUtil.isEmptyOrSpaces(directory) ? directory : ModuleRootManager.getInstance(module).getContentRoots()[0].getCanonicalPath();
final GeneralCommandLine commandLine = new GeneralCommandLine(parameters).withWorkDirectory(baseDir);
if (env != null) {
commandLine.withEnvironment(env);
}
try {
final boolean[] serverStarted = { false };
final KillableColoredProcessHandler processHandler = new KillableColoredProcessHandler(commandLine) {
@Override
protected void doDestroyProcess() {
super.doDestroyProcess();
myKernels.clear();
myToken = null;
UnixProcessManager.sendSigIntToProcessTree(getProcess());
}
@Override
public void coloredTextAvailable(@NotNull @NonNls String text, @NotNull Key attributes) {
super.coloredTextAvailable(text, attributes);
if (text.toLowerCase().contains("active kernels")) {
serverStarted[0] = true;
}
final String token = "?token=";
if (text.toLowerCase().contains(token)) {
myToken = text.substring(text.indexOf(token) + token.length()).trim();
}
}
@Override
public boolean isSilentlyDestroyOnClose() {
return true;
}
};
processHandler.setShouldDestroyProcessRecursively(true);
GuiUtils.invokeLaterIfNeeded(() -> new RunContentExecutor(myProject, processHandler).withTitle("Jupyter Notebook").withStop(() -> {
myKernels.clear();
processHandler.destroyProcess();
UnixProcessManager.sendSigIntToProcessTree(processHandler.getProcess());
}, () -> !processHandler.isProcessTerminated()).withRerun(() -> startIpythonServer(url, fileEditor)).withHelpId("reference.manage.py").withFilter(new UrlFilter()).run(), ModalityState.defaultModalityState());
int countAttempt = 0;
while (!serverStarted[0] && countAttempt < MAX_ATTEMPTS) {
countAttempt += 1;
TimeoutUtil.sleep(1000);
}
return true;
} catch (ExecutionException e) {
return false;
}
}
use of com.jetbrains.python.packaging.PyPackage in project intellij-community by JetBrains.
the class VFSTestFrameworkListener method checkTestFrameworksInstalled.
@NotNull
private Map<String, Boolean> checkTestFrameworksInstalled(@Nullable Sdk sdk, @NotNull String... testPackageNames) {
final Map<String, Boolean> result = new HashMap<>();
if (sdk == null || StringUtil.isEmptyOrSpaces(sdk.getHomePath())) {
LOG.info("Searching test runner in empty sdk");
return result;
}
final PyPackageManager manager = PyPackageManager.getInstance(sdk);
final boolean refreshed = PyPackageUtil.updatePackagesSynchronouslyWithGuard(manager, myIsUpdating);
if (refreshed) {
final List<PyPackage> packages = manager.getPackages();
if (packages != null) {
for (String name : testPackageNames) {
result.put(name, PyPackageUtil.findPackage(packages, name) != null);
}
}
}
return result;
}
use of com.jetbrains.python.packaging.PyPackage in project intellij-community by JetBrains.
the class ProjectSpecificSettingsStep method checkValid.
@Override
public boolean checkValid() {
myInstallFramework = false;
if (!super.checkValid()) {
return false;
}
if (myProjectGenerator instanceof PythonProjectGenerator) {
final Sdk sdk = getSdk();
if (sdk == null) {
if (!((PythonProjectGenerator) myProjectGenerator).hideInterpreter()) {
setErrorText("No Python interpreter selected");
return false;
}
return true;
}
if (PythonSdkType.isInvalid(sdk)) {
setErrorText("Choose valid python interpreter");
return false;
}
final List<String> warningList = new ArrayList<>();
final boolean isPy3k = PythonSdkType.getLanguageLevelForSdk(sdk).isPy3K();
try {
acceptsSdk(myProjectGenerator, sdk, new File(myLocationField.getText()));
} catch (final PythonProjectGenerator.PyNoProjectAllowedOnSdkException e) {
setErrorText(e.getMessage());
return false;
}
if (myRemotePathRequired && StringUtil.isEmpty(myRemotePathField.getTextField().getText())) {
setErrorText("Remote path not provided");
return false;
}
if (myProjectGenerator instanceof PyFrameworkProjectGenerator) {
PyFrameworkProjectGenerator frameworkProjectGenerator = (PyFrameworkProjectGenerator) myProjectGenerator;
String frameworkName = frameworkProjectGenerator.getFrameworkTitle();
if (!isFrameworkInstalled(sdk)) {
if (PyPackageUtil.packageManagementEnabled(sdk)) {
myInstallFramework = true;
final List<PyPackage> packages = PyPackageUtil.refreshAndGetPackagesModally(sdk);
if (packages == null) {
warningList.add(frameworkName + " will be installed on the selected interpreter");
return false;
}
if (!PyPackageUtil.hasManagement(packages)) {
warningList.add("Python packaging tools and " + frameworkName + " will be installed on the selected interpreter");
} else {
warningList.add(frameworkName + " will be installed on the selected interpreter");
}
} else {
warningList.add(frameworkName + " is not installed on the selected interpreter");
}
}
final ValidationResult warningResult = ((PythonProjectGenerator) myProjectGenerator).warningValidation(sdk);
if (!warningResult.isOk()) {
warningList.add(warningResult.getErrorMessage());
}
if (!warningList.isEmpty()) {
final String warning = StringUtil.join(warningList, "<br/>");
setWarningText(warning);
}
if (isPy3k && !((PyFrameworkProjectGenerator) myProjectGenerator).supportsPython3()) {
setErrorText(frameworkName + " is not supported for the selected interpreter");
return false;
}
}
}
return true;
}
use of com.jetbrains.python.packaging.PyPackage in project intellij-community by JetBrains.
the class SphinxRunConfiguration method createConfigurationEditor.
@Override
protected SettingsEditor<? extends RunConfiguration> createConfigurationEditor() {
final SphinxTasksModel model = new SphinxTasksModel();
if (!model.contains("pdf") && getSdk() != null) {
final List<PyPackage> packages = PyPackageManager.getInstance(getSdk()).getPackages();
if (packages != null) {
final PyPackage rst2pdf = PyPackageUtil.findPackage(packages, "rst2pdf");
if (rst2pdf != null) {
model.add(13, "pdf");
}
}
}
RestConfigurationEditor editor = new RestConfigurationEditor(getProject(), this, model);
editor.setConfigurationName("Sphinx task");
editor.setOpenInBrowserVisible(false);
editor.setInputDescriptor(FileChooserDescriptorFactory.createSingleFolderDescriptor());
editor.setOutputDescriptor(FileChooserDescriptorFactory.createSingleFolderDescriptor());
return editor;
}
Aggregations