1
+ package org.utbot.python.newtyping.runmypy
2
+
3
+ import org.utbot.python.newtyping.MypyAnnotationStorage
4
+ import org.utbot.python.newtyping.readMypyAnnotationStorage
5
+ import org.utbot.python.utils.TemporaryFileManager
6
+ import org.utbot.python.utils.runCommand
7
+ import java.io.File
8
+
9
+ fun readMypyAnnotationStorageAndInitialErrors (
10
+ pythonPath : String ,
11
+ sourcePath : String ,
12
+ configFile : File
13
+ ): Pair <MypyAnnotationStorage , String > {
14
+ val fileForAnnotationStorage = TemporaryFileManager .assignTemporaryFile(tag = " annotations.json" )
15
+ val fileForMypyStdout = TemporaryFileManager .assignTemporaryFile(tag = " mypy.out" )
16
+ val fileForMypyStderr = TemporaryFileManager .assignTemporaryFile(tag = " mypy.err" )
17
+ val result = runCommand(
18
+ listOf (
19
+ pythonPath,
20
+ " -m" ,
21
+ " utbot_mypy_runner" ,
22
+ " --config" ,
23
+ configFile.absolutePath,
24
+ " --sources" ,
25
+ sourcePath,
26
+ " --annotations_out" ,
27
+ fileForAnnotationStorage.absolutePath,
28
+ " --mypy_stdout" ,
29
+ fileForMypyStdout.absolutePath,
30
+ " --mypy_stderr" ,
31
+ fileForMypyStderr.absolutePath
32
+ )
33
+ )
34
+ val stderr = fileForMypyStderr.readText()
35
+ if (result.exitValue != 0 )
36
+ error(" Something went wrong in initial mypy run. Stderr: $stderr " )
37
+ return Pair (
38
+ readMypyAnnotationStorage(fileForAnnotationStorage.readText()),
39
+ fileForMypyStdout.readText()
40
+ )
41
+ }
42
+
43
+ fun checkWithDMypy (pythonPath : String , fileWithCodePath : String , configFile : File ): String {
44
+ val result = runCommand(
45
+ listOf (
46
+ pythonPath,
47
+ " -m" ,
48
+ " mypy.dmypy" ,
49
+ " run" ,
50
+ " --" ,
51
+ fileWithCodePath,
52
+ " --config-file" ,
53
+ configFile.path
54
+ )
55
+ )
56
+ return result.stdout
57
+ }
58
+
59
+ private const val configFilename = " config.ini"
60
+
61
+ private fun setConfigFile (): File {
62
+ val file = TemporaryFileManager .assignTemporaryFile(configFilename)
63
+ val configContent = """
64
+ [mypy]
65
+ namespace_packages = True
66
+ explicit_package_bases = True
67
+ show_absolute_path = True
68
+ cache_fine_grained = True
69
+ """ .trimIndent()
70
+ TemporaryFileManager .writeToAssignedFile(file, configContent)
71
+ return file
72
+ }
73
+
74
+ fun main () {
75
+ TemporaryFileManager .setup()
76
+ val configFile = setConfigFile()
77
+ println (
78
+ readMypyAnnotationStorageAndInitialErrors(
79
+ " python3" ,
80
+ " /home/tochilinak/Documents/projects/utbot/UTBotJava/utbot-python/samples/easy_samples/annotation_tests.py" ,
81
+ configFile
82
+ )
83
+ )
84
+ println (
85
+ checkWithDMypy(
86
+ " python3" ,
87
+ " /home/tochilinak/Documents/projects/utbot/UTBotJava/utbot-python/samples/easy_samples/annotation_tests.py" ,
88
+ configFile
89
+ )
90
+ )
91
+ }
0 commit comments