1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ # pylint: disable=import-outside-toplevel
16+
1517import json
1618import logging
1719import os
1820import platform
1921import subprocess
22+ import sys
2023import time
2124
2225import click
@@ -140,40 +143,46 @@ def _install_platformio_core(shutdown_piohome=True, develop=False, ignore_python
140143 return True
141144
142145
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 ):
145147 from pioinstaller import penv
146148
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+ )
150155 )
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+ )
153163 )
154- result = {}
155-
156164 if not os .path .isfile (platformio_exe ):
157165 raise exception .InvalidPlatformIOCore (
158166 "PlatformIO executable not found in `%s`" % penv .get_penv_bin_dir ()
159167 )
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 ()
162172 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 ))
165174 )
166175
176+ result = {}
167177 try :
168- result . update ( fetch_python_state (python_exe ) )
178+ result = fetch_python_state (python_exe )
169179 except subprocess .CalledProcessError as e :
170180 error = e .output .decode ()
171181 raise exception .InvalidPlatformIOCore (
172182 "Could not import PlatformIO module. Error: %s" % error
173183 )
174-
175184 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 )
177186 result .update (
178187 {
179188 "core_dir" : get_core_dir (),
@@ -184,71 +193,52 @@ def check(dev=False, auto_upgrade=False, version_spec=None):
184193 "installer_version" : __version__ ,
185194 "python_exe" : python_exe ,
186195 "system" : util .get_systype (),
187- "is_develop_core" : dev ,
196+ "is_develop_core" : develop ,
188197 }
189198 )
190199
191200 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_ :
192205 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" )
203214
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
212216
217+
218+ def _check_core_version (piocore_version , version_spec ):
213219 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
219229 )
220230
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 )
237231
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
246234
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+ )
252242
253243
254244def fetch_python_state (python_exe ):
@@ -290,9 +280,25 @@ def convert_version(version):
290280 return None
291281
292282
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+
294300 command = [platformio_exe , "upgrade" ]
295- if dev :
301+ if develop :
296302 command .append ("--dev" )
297303 try :
298304 subprocess .check_output (
@@ -304,6 +310,7 @@ def upgrade_core(platformio_exe, dev=False):
304310 raise exception .PIOInstallerException (
305311 "Could not upgrade PlatformIO Core: %s" % str (e )
306312 )
313+ return False
307314
308315
309316def dump_state (target , state ):
0 commit comments