summaryrefslogtreecommitdiff
path: root/python/edu/learn-python/src/com/jetbrains/python/edu/actions/StudyCheckAction.java
blob: f8e10c9c4521a4b85952f69a8ae8ee265a20db14 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package com.jetbrains.python.edu.actions;

import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.ide.projectView.ProjectView;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.BalloonBuilder;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.JBColor;
import com.jetbrains.python.edu.StudyDocumentListener;
import com.jetbrains.python.edu.StudyTaskManager;
import com.jetbrains.python.edu.StudyUtils;
import com.jetbrains.python.edu.course.*;
import com.jetbrains.python.edu.editor.StudyEditor;
import com.jetbrains.python.sdk.PythonSdkType;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;

public class StudyCheckAction extends DumbAwareAction {

  private static final Logger LOG = Logger.getInstance(StudyCheckAction.class.getName());
  public static final String PYTHONPATH = "PYTHONPATH";

  static class StudyTestRunner {
    public static final String TEST_OK = "#study_plugin test OK";
    private static final String TEST_FAILED = "#study_plugin FAILED + ";
    private final Task myTask;
    private final VirtualFile myTaskDir;

    StudyTestRunner(Task task, VirtualFile taskDir) {
      myTask = task;
      myTaskDir = taskDir;
    }

    Process launchTests(Project project, String executablePath) throws ExecutionException {
      Sdk sdk = PythonSdkType.findPythonSdk(ModuleManager.getInstance(project).getModules()[0]);
      File testRunner = new File(myTaskDir.getPath(), myTask.getTestFile());
      GeneralCommandLine commandLine = new GeneralCommandLine();
      commandLine.setWorkDirectory(myTaskDir.getPath());
      final Map<String, String> env = commandLine.getEnvironment();
      final VirtualFile courseDir = project.getBaseDir();
      if (courseDir != null)
        env.put(PYTHONPATH, courseDir.getPath());
      if (sdk != null) {
        String pythonPath = sdk.getHomePath();
        if (pythonPath != null) {
          commandLine.setExePath(pythonPath);
          commandLine.addParameter(testRunner.getPath());
          final Course course = StudyTaskManager.getInstance(project).getCourse();
          assert course != null;
          commandLine.addParameter(new File(course.getResourcePath()).getParent());
          commandLine.addParameter(FileUtil.toSystemDependentName(executablePath));
          return commandLine.createProcess();
        }
      }
      return null;
    }


    String getPassedTests(Process p) {
      InputStream testOutput = p.getInputStream();
      BufferedReader testOutputReader = new BufferedReader(new InputStreamReader(testOutput));
      String line;
      try {
        while ((line = testOutputReader.readLine()) != null) {
          if (line.contains(TEST_FAILED)) {
             return line.substring(TEST_FAILED.length(), line.length());
          }
        }
      }
      catch (IOException e) {
        LOG.error(e);
      }
      finally {
        StudyUtils.closeSilently(testOutputReader);
      }
      return TEST_OK;
    }
  }

  public void check(@NotNull final Project project) {
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      @Override
      public void run() {
        CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
          @Override
          public void run() {
        final Editor selectedEditor = StudyEditor.getSelectedEditor(project);
        if (selectedEditor != null) {
          final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
          final VirtualFile openedFile = fileDocumentManager.getFile(selectedEditor.getDocument());
          if (openedFile != null) {
            StudyTaskManager taskManager = StudyTaskManager.getInstance(project);
            final TaskFile selectedTaskFile = taskManager.getTaskFile(openedFile);
            List<VirtualFile> filesToDelete = new ArrayList<VirtualFile>();
            if (selectedTaskFile != null) {
              final VirtualFile taskDir = openedFile.getParent();
              Task currentTask = selectedTaskFile.getTask();
              StudyStatus oldStatus = currentTask.getStatus();
              Map<String, TaskFile> taskFiles = selectedTaskFile.getTask().getTaskFiles();
              for (Map.Entry<String, TaskFile> entry : taskFiles.entrySet()) {
                String name = entry.getKey();
                TaskFile taskFile = entry.getValue();
                VirtualFile virtualFile = taskDir.findChild(name);
                if (virtualFile == null) {
                  continue;
                }
                VirtualFile windowFile = StudyUtils.flushWindows(FileDocumentManager.getInstance().getDocument(virtualFile), taskFile, virtualFile);
                filesToDelete.add(windowFile);
                FileDocumentManager.getInstance().saveAllDocuments();
              }

              StudyRunAction runAction = (StudyRunAction)ActionManager.getInstance().getAction(StudyRunAction.ACTION_ID);
              if (runAction != null && currentTask.getTaskFiles().size() == 1) {
                runAction.run(project);
              }
              final StudyTestRunner testRunner = new StudyTestRunner(currentTask, taskDir);
              Process testProcess = null;
              try {
                testProcess = testRunner.launchTests(project, openedFile.getPath());
              }
              catch (ExecutionException e) {
                LOG.error(e);
              }
              if (testProcess != null) {
                String failedMessage = testRunner.getPassedTests(testProcess);
                if (failedMessage.equals(StudyTestRunner.TEST_OK)) {
                  currentTask.setStatus(StudyStatus.Solved, oldStatus);
                  StudyUtils.updateStudyToolWindow(project);
                  selectedTaskFile.drawAllWindows(selectedEditor);
                  ProjectView.getInstance(project).refresh();
                  for (VirtualFile file:filesToDelete) {
                    try {
                      file.delete(this);
                    }
                    catch (IOException e) {
                      LOG.error(e);
                    }
                  }
                  createTestResultPopUp("Congratulations!", JBColor.GREEN, project);
                  return;
                }
                for (Map.Entry<String, TaskFile> entry : taskFiles.entrySet()) {
                  String name = entry.getKey();
                  TaskFile taskFile = entry.getValue();
                  TaskFile answerTaskFile = new TaskFile();
                  VirtualFile virtualFile = taskDir.findChild(name);
                  if (virtualFile == null) {
                    continue;
                  }
                  VirtualFile answerFile = getCopyWithAnswers(taskDir, virtualFile, taskFile, answerTaskFile);
                  for (TaskWindow taskWindow : answerTaskFile.getTaskWindows()) {
                    Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
                    if (document == null) {
                      continue;
                    }
                    if (!taskWindow.isValid(document)) {
                      continue;
                    }
                    check(project, taskWindow, answerFile, answerTaskFile, taskFile, document, testRunner, virtualFile);
                  }
                  FileEditor fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(virtualFile);
                  Editor editor = null;
                  if (fileEditor instanceof StudyEditor) {
                    StudyEditor studyEditor = (StudyEditor) fileEditor;
                    editor = studyEditor.getEditor();
                  }

                  if (editor != null) {
                    taskFile.drawAllWindows(editor);
                    StudyUtils.synchronize();
                  }
                  try {
                    answerFile.delete(this);
                  }
                  catch (IOException e) {
                    LOG.error(e);
                  }
                }
                for (VirtualFile file:filesToDelete) {
                  try {
                    file.delete(this);
                  }
                  catch (IOException e) {
                    LOG.error(e);
                  }
                }
                currentTask.setStatus(StudyStatus.Failed, oldStatus);
                StudyUtils.updateStudyToolWindow(project);
                createTestResultPopUp(failedMessage, JBColor.RED, project);
              }
            }
          }
        }

         }
      });
      }
    });
  }

  private void check(Project project,
                     TaskWindow taskWindow,
                     VirtualFile answerFile,
                     TaskFile answerTaskFile,
                     TaskFile usersTaskFile,
                     Document usersDocument,
                     StudyTestRunner testRunner,
                     VirtualFile openedFile) {

    try {
       VirtualFile windowCopy = answerFile.copy(this, answerFile.getParent(), answerFile.getNameWithoutExtension() + "_window" + taskWindow.getIndex() + ".py");
      final FileDocumentManager documentManager = FileDocumentManager.getInstance();
      final Document windowDocument = documentManager.getDocument(windowCopy);
      if (windowDocument != null) {
        StudyTaskManager taskManager = StudyTaskManager.getInstance(project);
        Course course = taskManager.getCourse();
        Task task = usersTaskFile.getTask();
        int taskNum = task.getIndex() + 1;
        int lessonNum = task.getLesson().getIndex() + 1;
        assert course != null;
        String pathToResource = FileUtil.join(new File(course.getResourcePath()).getParent(), Lesson.LESSON_DIR + lessonNum,  Task.TASK_DIR + taskNum);
        File resourceFile = new File(pathToResource, windowCopy.getName());
        FileUtil.copy(new File(pathToResource, openedFile.getName()), resourceFile);
        TaskFile windowTaskFile = new TaskFile();
        TaskFile.copy(answerTaskFile, windowTaskFile);
        StudyDocumentListener listener = new StudyDocumentListener(windowTaskFile);
        windowDocument.addDocumentListener(listener);
        int start = taskWindow.getRealStartOffset(windowDocument);
        int end = start + taskWindow.getLength();
        TaskWindow userTaskWindow = usersTaskFile.getTaskWindows().get(taskWindow.getIndex());
        int userStart = userTaskWindow.getRealStartOffset(usersDocument);
        int userEnd = userStart + userTaskWindow.getLength();
        String text = usersDocument.getText(new TextRange(userStart, userEnd));
        windowDocument.replaceString(start, end, text);
        ApplicationManager.getApplication().runWriteAction(new Runnable() {
          @Override
          public void run() {
            documentManager.saveDocument(windowDocument);
          }
        });
        VirtualFile fileWindows = StudyUtils.flushWindows(windowDocument, windowTaskFile, windowCopy);
        Process smartTestProcess = testRunner.launchTests(project, windowCopy.getPath());
        boolean res = testRunner.getPassedTests(smartTestProcess).equals(StudyTestRunner.TEST_OK);
        userTaskWindow.setStatus(res ? StudyStatus.Solved : StudyStatus.Failed, StudyStatus.Unchecked);
        windowCopy.delete(this);
        fileWindows.delete(this);
        if (!resourceFile.delete()) {
          LOG.error("failed to delete", resourceFile.getPath());
        }
      }
    }
    catch (IOException e) {
      LOG.error(e);
    }
    catch (ExecutionException e) {
      LOG.error(e);
    }
  }


  private VirtualFile getCopyWithAnswers(final VirtualFile taskDir,
                                         final VirtualFile file,
                                         final TaskFile source,
                                         TaskFile target) {
    VirtualFile copy = null;
    try {

      copy = file.copy(this, taskDir, file.getNameWithoutExtension() +"_answers.py");
      final FileDocumentManager documentManager = FileDocumentManager.getInstance();
      final Document document = documentManager.getDocument(copy);
      if (document != null) {
        TaskFile.copy(source, target);
        StudyDocumentListener listener = new StudyDocumentListener(target);
        document.addDocumentListener(listener);
        for (TaskWindow taskWindow : target.getTaskWindows()) {
          if (!taskWindow.isValid(document)) {
            continue;
          }
          final int start = taskWindow.getRealStartOffset(document);
          final int end = start + taskWindow.getLength();
          final String text = taskWindow.getPossibleAnswer();
          document.replaceString(start, end, text);
        }
        ApplicationManager.getApplication().runWriteAction(new Runnable() {
          @Override
          public void run() {
            documentManager.saveDocument(document);
          }
        });
      }
    }
    catch (IOException e) {
      LOG.error(e);
    }


    return copy;
  }

  private static void createTestResultPopUp(final String text, Color color, @NotNull final Project project) {
    BalloonBuilder balloonBuilder =
      JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(text, null, color, null);
    Balloon balloon = balloonBuilder.createBalloon();
    StudyEditor studyEditor = StudyEditor.getSelectedStudyEditor(project);
    assert studyEditor != null;
    JButton checkButton = studyEditor.getCheckButton();
    balloon.showInCenterOf(checkButton);
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    if (project != null) {
      check(project);
    }
  }
}