12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ # pylint: disable=import-outside-toplevel
16
+
15
17
import json
16
18
import logging
17
19
import os
18
20
import platform
19
21
import subprocess
22
+ import sys
20
23
import time
21
24
22
25
import click
@@ -140,40 +143,46 @@ def _install_platformio_core(shutdown_piohome=True, develop=False, ignore_python
140
143
return True
141
144
142
145
143
- def check (dev = False , auto_upgrade = False , version_spec = None ):
144
- # pylint: disable=bad-option-value, import-outside-toplevel, unused-import, import-error, unused-variable, cyclic-import, too-many-branches
146
+ def check (develop = False , global_ = False , auto_upgrade = False , version_spec = None ):
145
147
from pioinstaller import penv
146
148
147
- platformio_exe = os .path .join (
148
- penv .get_penv_bin_dir (),
149
- "platformio.exe" if util .IS_WINDOWS else "platformio" ,
149
+ python_exe = (
150
+ os .path .normpath (sys .executable )
151
+ if global_
152
+ else os .path .join (
153
+ penv .get_penv_bin_dir (), "python.exe" if util .IS_WINDOWS else "python"
154
+ )
150
155
)
151
- python_exe = os .path .join (
152
- penv .get_penv_bin_dir (), "python.exe" if util .IS_WINDOWS else "python"
156
+ platformio_exe = (
157
+ util .where_is_program ("platformio" )
158
+ if global_
159
+ else os .path .join (
160
+ penv .get_penv_bin_dir (),
161
+ "platformio.exe" if util .IS_WINDOWS else "platformio" ,
162
+ )
153
163
)
154
- result = {}
155
-
156
164
if not os .path .isfile (platformio_exe ):
157
165
raise exception .InvalidPlatformIOCore (
158
166
"PlatformIO executable not found in `%s`" % penv .get_penv_bin_dir ()
159
167
)
160
-
161
- if not os .path .isfile (os .path .join (penv .get_penv_dir (), "state.json" )):
168
+ try :
169
+ subprocess .check_output ([platformio_exe , "--version" ], stderr = subprocess .STDOUT )
170
+ except subprocess .CalledProcessError as e :
171
+ error = e .output .decode ()
162
172
raise exception .InvalidPlatformIOCore (
163
- "Could not found state.json file in `%s`"
164
- % os .path .join (penv .get_penv_dir (), "state.json" )
173
+ "Could not run `%s --version`.\n Error: %s" % (platformio_exe , str (error ))
165
174
)
166
175
176
+ result = {}
167
177
try :
168
- result . update ( fetch_python_state (python_exe ) )
178
+ result = fetch_python_state (python_exe )
169
179
except subprocess .CalledProcessError as e :
170
180
error = e .output .decode ()
171
181
raise exception .InvalidPlatformIOCore (
172
182
"Could not import PlatformIO module. Error: %s" % error
173
183
)
174
-
175
184
piocore_version = convert_version (result .get ("core_version" ))
176
- dev = dev or bool (piocore_version .prerelease if piocore_version else False )
185
+ develop = develop or bool (piocore_version .prerelease if piocore_version else False )
177
186
result .update (
178
187
{
179
188
"core_dir" : get_core_dir (),
@@ -184,71 +193,52 @@ def check(dev=False, auto_upgrade=False, version_spec=None):
184
193
"installer_version" : __version__ ,
185
194
"python_exe" : python_exe ,
186
195
"system" : util .get_systype (),
187
- "is_develop_core" : dev ,
196
+ "is_develop_core" : develop ,
188
197
}
189
198
)
190
199
191
200
if version_spec :
201
+ _check_core_version (piocore_version , version_spec )
202
+ if not global_ :
203
+ _check_platform_version ()
204
+ if auto_upgrade and not global_ :
192
205
try :
193
- if piocore_version not in semantic_version .Spec (version_spec ):
194
- raise exception .InvalidPlatformIOCore (
195
- "PlatformIO Core version %s does not match version requirements %s."
196
- % (str (piocore_version ), version_spec )
197
- )
198
- except ValueError :
199
- click .secho (
200
- "Invalid version requirements format: %s. "
201
- "More about Semantic Versioning: https://semver.org/" % version_spec
202
- )
206
+ auto_upgrade_core (platformio_exe , develop )
207
+ except : # pylint:disable=bare-except
208
+ pass
209
+ # re-fetch Pyrhon state
210
+ try :
211
+ result .update (fetch_python_state (python_exe ))
212
+ except : # pylint:disable=bare-except
213
+ raise exception .InvalidPlatformIOCore ("Could not import PlatformIO module" )
203
214
204
- with open (os .path .join (penv .get_penv_dir (), "state.json" )) as fp :
205
- penv_state = json .load (fp )
206
- if penv_state .get ("platform" ) != platform .platform (terse = True ):
207
- raise exception .InvalidPlatformIOCore (
208
- "PlatformIO Core was installed using another platform `%s`. "
209
- "Your current platform: %s"
210
- % (penv_state .get ("platform" ), platform .platform (terse = True ))
211
- )
215
+ return result
212
216
217
+
218
+ def _check_core_version (piocore_version , version_spec ):
213
219
try :
214
- subprocess .check_output ([platformio_exe , "--version" ], stderr = subprocess .STDOUT )
215
- except subprocess .CalledProcessError as e :
216
- error = e .output .decode ()
217
- raise exception .InvalidPlatformIOCore (
218
- "Could not run `%s --version`.\n Error: %s" % (platformio_exe , str (error ))
220
+ if piocore_version not in semantic_version .Spec (version_spec ):
221
+ raise exception .InvalidPlatformIOCore (
222
+ "PlatformIO Core version %s does not match version requirements %s."
223
+ % (str (piocore_version ), version_spec )
224
+ )
225
+ except ValueError :
226
+ click .secho (
227
+ "Invalid version requirements format: %s. "
228
+ "More about Semantic Versioning: https://semver.org/" % version_spec
219
229
)
220
230
221
- if not auto_upgrade :
222
- return result
223
-
224
- time_now = int (round (time .time ()))
225
-
226
- last_piocore_version_check = penv_state .get ("last_piocore_version_check" )
227
-
228
- if (
229
- last_piocore_version_check
230
- and (time_now - int (last_piocore_version_check )) < UPDATE_INTERVAL
231
- ):
232
- return result
233
-
234
- with open (os .path .join (penv .get_penv_dir (), "state.json" ), "w" ) as fp :
235
- penv_state ["last_piocore_version_check" ] = time_now
236
- json .dump (penv_state , fp )
237
231
238
- if not last_piocore_version_check :
239
- return result
240
-
241
- # capture exception when Internet is off-line
242
- try :
243
- upgrade_core (platformio_exe , dev )
244
- except : # pylint:disable=bare-except
245
- return result
232
+ def _check_platform_version ():
233
+ from pioinstaller import penv
246
234
247
- try :
248
- result .update (fetch_python_state (python_exe ))
249
- except : # pylint:disable=bare-except
250
- raise exception .InvalidPlatformIOCore ("Could not import PlatformIO module" )
251
- return result
235
+ state = penv .load_state ()
236
+ if state .get ("platform" ) != platform .platform (terse = True ):
237
+ raise exception .InvalidPlatformIOCore (
238
+ "PlatformIO Core was installed using another platform `%s`. "
239
+ "Your current platform: %s"
240
+ % (state .get ("platform" ), platform .platform (terse = True ))
241
+ )
252
242
253
243
254
244
def fetch_python_state (python_exe ):
@@ -290,9 +280,25 @@ def convert_version(version):
290
280
return None
291
281
292
282
293
- def upgrade_core (platformio_exe , dev = False ):
283
+ def auto_upgrade_core (platformio_exe , develop = False ):
284
+ from pioinstaller import penv
285
+
286
+ state = penv .load_state ()
287
+ time_now = int (round (time .time ()))
288
+ last_piocore_version_check = state .get ("last_piocore_version_check" )
289
+ if (
290
+ last_piocore_version_check
291
+ and (time_now - int (last_piocore_version_check )) < UPDATE_INTERVAL
292
+ ):
293
+ return None
294
+
295
+ state ["last_piocore_version_check" ] = time_now
296
+ penv .save_state (state )
297
+ if not last_piocore_version_check :
298
+ return None
299
+
294
300
command = [platformio_exe , "upgrade" ]
295
- if dev :
301
+ if develop :
296
302
command .append ("--dev" )
297
303
try :
298
304
subprocess .check_output (
@@ -304,6 +310,7 @@ def upgrade_core(platformio_exe, dev=False):
304
310
raise exception .PIOInstallerException (
305
311
"Could not upgrade PlatformIO Core: %s" % str (e )
306
312
)
313
+ return False
307
314
308
315
309
316
def dump_state (target , state ):
0 commit comments