Skip to content

Commit

Permalink
Show notification when Mypy exits abnormally
Browse files Browse the repository at this point in the history
  • Loading branch information
leinardi committed Feb 26, 2022
1 parent 107f589 commit 62c06bb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ private Map<String, PsiFile> mapFilesToElements(final List<ScannableFile> filesT
return filePathsToElements;
}

private Map<PsiFile, List<Problem>> checkFiles(final Set<PsiFile> filesToScan) throws InterruptedIOException {
private Map<PsiFile, List<Problem>> checkFiles(final Set<PsiFile> filesToScan)
throws InterruptedIOException, InterruptedException {
final List<ScannableFile> scannableFiles = new ArrayList<>();
try {
scannableFiles.addAll(ScannableFile.createAndValidate(filesToScan, plugin));
Expand All @@ -103,7 +104,8 @@ private Map<PsiFile, List<Problem>> checkFiles(final Set<PsiFile> filesToScan) t
}
}

private Map<PsiFile, List<Problem>> scan(final List<ScannableFile> filesToScan) throws InterruptedIOException {
private Map<PsiFile, List<Problem>> scan(final List<ScannableFile> filesToScan)
throws InterruptedIOException, InterruptedException {
Map<String, PsiFile> fileNamesToPsiFiles = mapFilesToElements(filesToScan);
List<Issue> errors = PylintRunner.scan(plugin.getProject(), fileNamesToPsiFiles.keySet());
String baseDir = plugin.getProject().getBasePath();
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/com/leinardi/pycharm/pylint/plapi/PylintRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ public static String detectSystemPylintPath() {
}
}

public static List<Issue> scan(Project project, Set<String> filesToScan) throws InterruptedIOException {
public static List<Issue> scan(Project project, Set<String> filesToScan) throws InterruptedIOException,
InterruptedException {
if (!checkPylintAvailable(project, true)) {
return Collections.emptyList();
}
Expand Down Expand Up @@ -270,18 +271,34 @@ public static List<Issue> scan(Project project, Set<String> filesToScan) throws

cmd.setWorkDirectory(project.getBasePath());
final Process process;

try {
process = cmd.createProcess();
Moshi moshi = new Moshi.Builder().build();
Type type = Types.newParameterizedType(List.class, Issue.class);
JsonAdapter<List<Issue>> adapter = moshi.adapter(type);
InputStream inputStream = process.getInputStream();
//TODO check stderr for errors
assert (inputStream != null);
List<Issue> issues;
if (checkIfInputStreamIsEmpty(inputStream)) {
return new ArrayList<>();
issues = new ArrayList<>();
} else {
return adapter.fromJson(Okio.buffer(Okio.source(inputStream)));
issues = adapter.fromJson(Okio.buffer(Okio.source(inputStream)));
}
process.waitFor();

// Anything equal or bigger than 32 is an abnormal exit code
// See https://docs.pylint.org/en/1.6.0/run.html#exit-codes
int exitCode = process.exitValue();
if (exitCode >= 32) {
InputStream errStream = process.getErrorStream();
String detail = new BufferedReader(new InputStreamReader(errStream))
.lines().collect(Collectors.joining("\n"));

Notifications.showPylintAbnormalExit(project, detail);
throw new PylintToolException("Pylint failed with code " + exitCode);
}
return issues;
} catch (InterruptedIOException e) {
LOG.info("Command Line string: " + cmd.getCommandLineString());
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ public static void showUnableToRunPylint(final Project project) {
.notify(project);
}

public static void showPylintAbnormalExit(final Project project, final String detail) {
BALLOON_GROUP
.createNotification(TITLE, detail, ERROR)
.setListener(URL_OPENING_LISTENER)
.setSubtitle(PylintBundle.message("plugin.notification.abnormal-exit.subtitle"))
.notify(project);
}

public static void showNoPythonInterpreter(Project project) {
Notification notification = BALLOON_GROUP
.createNotification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ plugin.notification.unable-to-run-pylint.subtitle=Unable to run Pylint
plugin.notification.unable-to-run-pylint.content=Pylint is installed inside the project environment but the plugin \
is not able to run it. If you just installed it try to run File -> Synchronize or restart your IDE. \
If the problem persists you may need to manually enter the path to the Pylint executable inside the Plugin settings.
plugin.notification.abnormal-exit.subtitle=Pylint exited abnormally
plugin.notification.action.plugin-settings=Plugin settings
plugin.notification.install-pylint.subtitle=Pylint missing
plugin.notification.install-pylint.content=The project interpreter is missing Pylint, which is needed \
Expand Down

0 comments on commit 62c06bb

Please sign in to comment.