1
1
from enum import Enum
2
2
import subprocess
3
3
import sys
4
- import tempfile
5
4
import pathlib
6
5
import platform
7
6
from typing import List , Optional , Tuple
8
7
9
8
10
- MPY_CROSS_PATH = (
9
+ MPY_CROSS_PATH = str (
11
10
(pathlib .Path (__file__ ).parent / "mpy-cross" )
12
11
.with_suffix (".exe" if platform .system () == "Windows" else "" )
13
12
.absolute ()
@@ -42,7 +41,7 @@ def mpy_cross_compile(
42
41
emit : Optional [Emitter ] = None ,
43
42
heap_size : Optional [int ] = None ,
44
43
extra_args : Optional [List [str ]] = None ,
45
- ) -> Tuple [subprocess .CompletedProcess , Optional [bytes ]]:
44
+ ) -> Tuple [subprocess .CompletedProcess [ bytes ] , Optional [bytes ]]:
46
45
"""
47
46
Compiles a file using mpy-cross.
48
47
@@ -75,44 +74,32 @@ def mpy_cross_compile(
75
74
...
76
75
77
76
"""
78
- with tempfile .TemporaryDirectory () as tmp_dir :
79
- tmp_dir = pathlib .Path (tmp_dir )
77
+ args : list [str ] = [MPY_CROSS_PATH , "-" , "-s" , file_name ]
80
78
81
- with open (tmp_dir / "tmp.py" , "w" ) as in_file :
82
- in_file .write (file_contents )
79
+ if optimization_level is not None :
80
+ if optimization_level not in range (4 ):
81
+ raise ValueError ("optimization_level must be between 0 and 3" )
83
82
84
- args = [ MPY_CROSS_PATH , in_file . name , "-s" , file_name ]
83
+ args . append ( f"-O { optimization_level } " )
85
84
86
- if optimization_level is not None :
87
- if optimization_level not in range (4 ):
88
- raise ValueError ("optimization_level must be between 0 and 3" )
85
+ if small_number_bits is not None :
86
+ args .append (f"-msmall-int-bits={ small_number_bits } " )
89
87
90
- args .append (f"-O{ optimization_level } " )
88
+ if arch is not None :
89
+ args .append (f"-march={ arch .value } " )
91
90
92
- if small_number_bits is not None :
93
- args . append ( f"-msmall-int-bits= { small_number_bits } " )
91
+ if emit is not None :
92
+ args += [ "-X" , f"emit= { emit . value } " ]
94
93
95
- if arch is not None :
96
- args . append ( f"-march= { arch . value } " )
94
+ if heap_size is not None :
95
+ args += [ "-X" , f"heapsize= { heap_size } " ]
97
96
98
- if emit is not None :
99
- args += [ "-X" , f"emit= { emit . value } " ]
97
+ if extra_args :
98
+ args += extra_args
100
99
101
- if heap_size is not None :
102
- args += ["-X" , f"heapsize={ heap_size } " ]
100
+ process = subprocess .run (args , capture_output = True , input = file_contents .encode ())
103
101
104
- if extra_args :
105
- args += extra_args
106
-
107
- process = subprocess .run (args , capture_output = True )
108
-
109
- try :
110
- with open (tmp_dir / "tmp.mpy" , "rb" ) as out_file :
111
- data = out_file .read ()
112
- except OSError :
113
- data = None
114
-
115
- return process , data
102
+ return process , process .stdout if process .returncode == 0 else None
116
103
117
104
118
105
def mpy_cross_version () -> str :
@@ -124,7 +111,7 @@ def mpy_cross_version() -> str:
124
111
return proc .stdout .decode ().strip ()
125
112
126
113
127
- def _run () :
114
+ def run () -> None :
128
115
"""
129
116
Run mpy-cross directly.
130
117
"""
0 commit comments