fix check utilities
[idea/community.git] / python / educational-python / student-python / src / com / jetbrains / edu / learning / PyStudyTestRunner.java
1 package com.jetbrains.edu.learning;
2
3 import com.intellij.execution.ExecutionException;
4 import com.intellij.execution.configurations.GeneralCommandLine;
5 import com.intellij.openapi.diagnostic.Logger;
6 import com.intellij.openapi.module.ModuleManager;
7 import com.intellij.openapi.project.Project;
8 import com.intellij.openapi.projectRoots.Sdk;
9 import com.intellij.openapi.util.io.FileUtil;
10 import com.intellij.openapi.util.io.FileUtilRt;
11 import com.intellij.openapi.vfs.VirtualFile;
12 import com.jetbrains.edu.learning.checker.StudyTestRunner;
13 import com.jetbrains.edu.learning.core.EduNames;
14 import com.jetbrains.edu.learning.courseFormat.Course;
15 import com.jetbrains.edu.learning.courseFormat.Task;
16 import com.jetbrains.python.sdk.PythonSdkType;
17 import org.jetbrains.annotations.NotNull;
18
19 import java.io.File;
20 import java.util.Map;
21
22 public class PyStudyTestRunner extends StudyTestRunner {
23   private static final String PYTHONPATH = "PYTHONPATH";
24   private static final Logger LOG = Logger.getInstance(PyStudyTestRunner.class.getName());
25
26   PyStudyTestRunner(@NotNull final Task task, @NotNull final VirtualFile taskDir) {
27     super(task, taskDir);
28   }
29
30   public Process createCheckProcess(@NotNull final Project project, @NotNull final String executablePath) throws ExecutionException {
31     final Sdk sdk = PythonSdkType.findPythonSdk(ModuleManager.getInstance(project).getModules()[0]);
32     Course course = myTask.getLesson().getCourse();
33     StudyLanguageManager manager = StudyUtils.getLanguageManager(course);
34     if (manager == null) {
35       LOG.info("Language manager is null for " + course.getLanguageById().getDisplayName());
36       return null;
37     }
38
39     String testsFileName = manager.getTestFileName();
40     if (myTask.hasSubtasks()) {
41       testsFileName = FileUtil.getNameWithoutExtension(testsFileName);
42       int index = myTask.getActiveSubtaskIndex() + 1;
43       testsFileName += EduNames.SUBTASK_MARKER + index + "." + FileUtilRt.getExtension(manager.getTestFileName());
44     }
45     final File testRunner = new File(myTaskDir.getPath(), testsFileName);
46     final GeneralCommandLine commandLine = new GeneralCommandLine();
47     commandLine.withWorkDirectory(myTaskDir.getPath());
48     final Map<String, String> env = commandLine.getEnvironment();
49
50     final VirtualFile courseDir = project.getBaseDir();
51     if (courseDir != null) {
52       env.put(PYTHONPATH, courseDir.getPath());
53     }
54     if (sdk != null) {
55       String pythonPath = sdk.getHomePath();
56       if (pythonPath != null) {
57         commandLine.setExePath(pythonPath);
58         commandLine.addParameter(testRunner.getPath());
59         File resourceFile = new File(course.getCourseDirectory());
60         commandLine.addParameter(resourceFile.getPath());
61         commandLine.addParameter(FileUtil.toSystemDependentName(executablePath));
62         return commandLine.createProcess();
63       }
64     }
65     return null;
66   }
67 }