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
@@ -77,47 +76,35 @@ def mpy_cross_compile(
77
76
...
78
77
79
78
"""
80
- with tempfile .TemporaryDirectory () as tmp_dir :
81
- tmp_dir = pathlib .Path (tmp_dir )
79
+ args : list [str ] = [MPY_CROSS_PATH , "-" , "-s" , file_name ]
82
80
83
- with open (tmp_dir / "tmp.py" , "w" ) as in_file :
84
- in_file .write (file_contents )
81
+ if optimization_level is not None :
82
+ if optimization_level not in range (4 ):
83
+ raise ValueError ("optimization_level must be between 0 and 3" )
85
84
86
- args = [ MPY_CROSS_PATH , in_file . name , "-s" , file_name ]
85
+ args . append ( f"-O { optimization_level } " )
87
86
88
- if optimization_level is not None :
89
- if optimization_level not in range (4 ):
90
- raise ValueError ("optimization_level must be between 0 and 3" )
87
+ if small_number_bits is not None :
88
+ args .append (f"-msmall-int-bits={ small_number_bits } " )
91
89
92
- args .append (f"-O{ optimization_level } " )
90
+ if no_unicode :
91
+ args .append ("-mno-unicode" )
93
92
94
- if small_number_bits is not None :
95
- args .append (f"-msmall-int-bits= { small_number_bits } " )
93
+ if arch is not None :
94
+ args .append (f"-march= { arch . value } " )
96
95
97
- if no_unicode :
98
- args . append ( "-mno-unicode" )
96
+ if emit is not None :
97
+ args += [ "-X" , f"emit= { emit . value } " ]
99
98
100
- if arch is not None :
101
- args . append ( f"-march= { arch . value } " )
99
+ if heap_size is not None :
100
+ args += [ "-X" , f"heapsize= { heap_size } " ]
102
101
103
- if emit is not None :
104
- args += [ "-X" , f"emit= { emit . value } " ]
102
+ if extra_args :
103
+ args += extra_args
105
104
106
- if heap_size is not None :
107
- args += ["-X" , f"heapsize={ heap_size } " ]
105
+ process = subprocess .run (args , capture_output = True , input = file_contents .encode ())
108
106
109
- if extra_args :
110
- args += extra_args
111
-
112
- process = subprocess .run (args , capture_output = True )
113
-
114
- try :
115
- with open (tmp_dir / "tmp.mpy" , "rb" ) as out_file :
116
- data = out_file .read ()
117
- except OSError :
118
- data = None
119
-
120
- return process , data
107
+ return process , process .stdout if process .returncode == 0 else None
121
108
122
109
123
110
def mpy_cross_version () -> str :
@@ -129,7 +116,7 @@ def mpy_cross_version() -> str:
129
116
return proc .stdout .decode ().strip ()
130
117
131
118
132
- def _run () :
119
+ def run () -> None :
133
120
"""
134
121
Run mpy-cross directly.
135
122
"""
0 commit comments