def check_prune_system(): last_check = app.get_state_item("last_check", {}) interval = 30 * 3600 * 24 # 1 time per month if (time() - interval) < last_check.get("prune_system", 0): return last_check["prune_system"] = int(time()) app.set_state_item("last_check", last_check) threshold_mb = int(app.get_setting("check_prune_system_threshold") or 0) if threshold_mb <= 0: return unnecessary_size = calculate_unnecessary_system_data() if (unnecessary_size / 1024) < threshold_mb: return terminal_width, _ = click.get_terminal_size() click.echo() click.echo("*" * terminal_width) click.secho( "We found %s of unnecessary PlatformIO system data (temporary files, " "unnecessary packages, etc.).\nUse `pio system prune --dry-run` to list " "them or `pio system prune` to save disk space." % fs.humanize_file_size(unnecessary_size), fg="yellow", )
def check_platformio_upgrade(): last_check = app.get_state_item("last_check", {}) interval = int(app.get_setting("check_platformio_interval")) * 3600 * 24 if (time() - interval) < last_check.get("platformio_upgrade", 0): return last_check['platformio_upgrade'] = int(time()) app.set_state_item("last_check", last_check) latest_version = get_latest_version() if (latest_version == __version__ or Upgrader.version_to_int(latest_version) < Upgrader.version_to_int(__version__)): return terminal_width, _ = click.get_terminal_size() click.echo("") click.echo("*" * terminal_width) click.secho("There is a new version %s of PlatformIO available.\n" "Please upgrade it via `" % latest_version, fg="yellow", nl=False) click.secho("platformio upgrade", fg="cyan", nl=False) click.secho("` or `", fg="yellow", nl=False) click.secho("pip install -U platformio", fg="cyan", nl=False) click.secho("` command.\nChanges: ", fg="yellow", nl=False) click.secho("http://docs.platformio.org/en/latest/history.html", fg="cyan") click.echo("*" * terminal_width) click.echo("")
def get_platforms(cls, installed=False): platforms = {} for d in (util.get_home_dir(), util.get_source_dir()): pdir = join(d, "platforms") if not isdir(pdir): continue for p in listdir(pdir): if p in ("__init__.py", "base.py") or not p.endswith(".py"): continue type_ = p[:-3] path = join(pdir, p) try: isplatform = hasattr( cls.load_module(type_, path), cls.get_clsname(type_) ) if isplatform: platforms[type_] = path except exception.UnknownPlatform: pass if not installed: return platforms installed_platforms = {} for type_ in get_state_item("installed_platforms", []): if type_ in platforms: installed_platforms[type_] = platforms[type_] return installed_platforms
def after_upgrade(ctx): last_version = app.get_state_item("last_version", "0.0.0") if last_version == __version__: return if last_version == "0.0.0": app.set_state_item("last_version", __version__) else: click.secho("Please wait while upgrading PlatformIO ...", fg="yellow") clean_cache() u = Upgrader(last_version, __version__) if u.run(ctx): app.set_state_item("last_version", __version__) # update development platforms pm = PlatformManager() for manifest in pm.get_installed(): # pm.update(manifest['name'], "^" + manifest['version']) pm.update(manifest['name']) # update PlatformIO Plus tool if installed pioplus_update() click.secho( "PlatformIO has been successfully upgraded to %s!\n" % __version__, fg="green") telemetry.on_event( category="Auto", action="Upgrade", label="%s > %s" % (last_version, __version__)) else: raise exception.UpgradeError("Auto upgrading...") click.echo("") # PlatformIO banner terminal_width, _ = click.get_terminal_size() click.echo("*" * terminal_width) click.echo("If you like %s, please:" % (click.style( "PlatformIO", fg="cyan"))) click.echo("- %s us on Twitter to stay up-to-date " "on the latest project news > %s" % (click.style( "follow", fg="cyan"), click.style( "https://twitter.com/PlatformIO_Org", fg="cyan"))) click.echo("- %s it on GitHub > %s" % (click.style( "star", fg="cyan"), click.style( "https://github.com/platformio/platformio", fg="cyan"))) if not getenv("PLATFORMIO_IDE"): click.echo("- %s PlatformIO IDE for IoT development > %s" % (click.style( "try", fg="cyan"), click.style( "http://platformio.org/platformio-ide", fg="cyan"))) if not util.is_ci(): click.echo("- %s us with PlatformIO Plus > %s" % (click.style( "support", fg="cyan"), click.style( "https://pioplus.com", fg="cyan"))) click.echo("*" * terminal_width) click.echo("")
def get_platforms(cls, installed=False): platforms = {} for d in (util.get_home_dir(), util.get_source_dir()): pdir = join(d, "platforms") if not isdir(pdir): continue for p in listdir(pdir): if p in ("__init__.py", "base.py") or not p.endswith(".py"): continue type_ = p[:-3] path = join(pdir, p) try: isplatform = hasattr(cls.load_module(type_, path), cls.get_clsname(type_)) if isplatform: platforms[type_] = path except exception.UnknownPlatform: pass if not installed: return platforms installed_platforms = {} for type_ in get_state_item("installed_platforms", []): if type_ in platforms: installed_platforms[type_] = platforms[type_] return installed_platforms
def get_account_info(self, offline=False): account = app.get_state_item("account") or {} if (account.get("summary") and account["summary"].get("expire_at", 0) > time.time()): return account["summary"] if offline and account.get("email"): return { "profile": { "email": account.get("email"), "username": account.get("username"), } } result = self.send_auth_request( "get", "/v1/summary", ) account["summary"] = dict( profile=result.get("profile"), packages=result.get("packages"), subscriptions=result.get("subscriptions"), user_id=result.get("user_id"), expire_at=int(time.time()) + self.SUMMARY_CACHE_TTL, ) app.set_state_item("account", account) return result
def get_account_info(self, offline): account = app.get_state_item("account") if not account: raise exception.AccountNotAuthorized() if (account.get("summary") and account["summary"].get("expire_at", 0) > time.time()): return account["summary"] if offline: return { "profile": { "email": account.get("email"), "username": account.get("username"), } } token = self.fetch_authentication_token() result = self.send_request( "get", self.api_base_url + "/v1/summary", headers={"Authorization": "Bearer %s" % token}, ) account["summary"] = dict( profile=result.get("profile"), packages=result.get("packages"), subscriptions=result.get("subscriptions"), user_id=result.get("user_id"), expire_at=int(time.time()) + self.SUMMARY_CACHE_TTL, ) app.set_state_item("account", account) return result
def check_platformio_upgrade(): last_check = app.get_state_item("last_check", {}) interval = int(app.get_setting("check_platformio_interval")) * 3600 * 24 if (time() - interval) < last_check.get("platformio_upgrade", 0): return last_check['platformio_upgrade'] = int(time()) app.set_state_item("last_check", last_check) latest_version = get_latest_version() if (latest_version == __version__ or Upgrader.version_to_int(latest_version) < Upgrader.version_to_int(__version__)): return terminal_width, _ = click.get_terminal_size() click.echo("") click.echo("*" * terminal_width) click.secho("There is a new version %s of PlatformIO available.\n" "Please upgrade it via " % latest_version, fg="yellow", nl=False) click.secho("platformio upgrade", fg="cyan", nl=False) click.secho(" command.\nChanges: ", fg="yellow", nl=False) click.secho("http://docs.platformio.org/en/latest/history.html", fg="cyan") click.echo("*" * terminal_width) click.echo("")
def install(self, with_packages=None, without_packages=None, skip_default_packages=False): with_packages = set( self.pkg_aliases_to_names(with_packages or [])) without_packages = set( self.pkg_aliases_to_names(without_packages or [])) upkgs = with_packages | without_packages ppkgs = set(self.get_packages().keys()) if not upkgs.issubset(ppkgs): raise exception.UnknownPackage(", ".join(upkgs - ppkgs)) requirements = [] for name, opts in self.get_packages().items(): if name in without_packages: continue elif (name in with_packages or (not skip_default_packages and opts.get("default"))): requirements.append(name) pm = PackageManager() for name in requirements: pm.install(name) # register installed platform data = get_state_item("installed_platforms", []) if self.get_type() not in data: data.append(self.get_type()) set_state_item("installed_platforms", data) return len(requirements)
def check_platformio_upgrade(): last_check = app.get_state_item("last_check", {}) interval = int(app.get_setting("check_platformio_interval")) * 3600 * 24 if (time() - interval) < last_check.get("platformio_upgrade", 0): return last_check['platformio_upgrade'] = int(time()) app.set_state_item("last_check", last_check) try: latest_version = get_latest_version() except GetLatestVersionError: click.secho("Failed to check for PlatformIO upgrades", fg="red") return if (latest_version == __version__ or Upgrader.version_to_int(latest_version) < Upgrader.version_to_int(__version__)): return click.secho("There is a new version %s of PlatformIO available.\n" "Please upgrade it via " % latest_version, fg="yellow", nl=False) click.secho("platformio upgrade", fg="cyan", nl=False) click.secho(" command.\nChanges: ", fg="yellow", nl=False) click.secho("http://docs.platformio.org/en/latest/history.html\n", fg="cyan")
def backup_reports(items): if not items: return KEEP_MAX_REPORTS = 100 tm = app.get_state_item("telemetry", {}) if "backup" not in tm: tm['backup'] = [] for params in items: # skip static options for key in params.keys(): if key in ("v", "tid", "cid", "cd1", "cd2", "sr", "an"): del params[key] # store time in UNIX format if "qt" not in params: params['qt'] = time() elif not isinstance(params['qt'], float): params['qt'] = time() - (params['qt'] / 1000) tm['backup'].append(params) tm['backup'] = tm['backup'][KEEP_MAX_REPORTS * -1:] app.set_state_item("telemetry", tm)
def check_internal_updates(ctx, what): last_check = app.get_state_item("last_check", {}) interval = int(app.get_setting("check_%s_interval" % what)) * 3600 * 24 if (time() - interval) < last_check.get(what + "_update", 0): return last_check[what + '_update'] = int(time()) app.set_state_item("last_check", last_check) pm = PlatformManager() if what == "platforms" else LibraryManager() outdated_items = [] for manifest in pm.get_installed(): if manifest['name'] not in outdated_items and \ pm.is_outdated(manifest['name']): outdated_items.append(manifest['name']) if not outdated_items: return terminal_width, _ = click.get_terminal_size() click.echo("") click.echo("*" * terminal_width) click.secho("There are the new updates for %s (%s)" % (what, ", ".join(outdated_items)), fg="yellow") if not app.get_setting("auto_update_" + what): click.secho("Please update them via ", fg="yellow", nl=False) click.secho("`platformio %s update`" % ("lib --global" if what == "libraries" else "platform"), fg="cyan", nl=False) click.secho(" command.\n", fg="yellow") click.secho( "If you want to manually check for the new versions " "without updating, please use ", fg="yellow", nl=False) click.secho("`platformio %s update --only-check`" % ("lib --global" if what == "libraries" else "platform"), fg="cyan", nl=False) click.secho(" command.", fg="yellow") else: click.secho("Please wait while updating %s ..." % what, fg="yellow") if what == "platforms": ctx.invoke(cmd_platform_update, platforms=outdated_items) elif what == "libraries": ctx.obj = pm ctx.invoke(cmd_lib_update, libraries=outdated_items) click.echo() telemetry.on_event(category="Auto", action="Update", label=what.title()) click.echo("*" * terminal_width) click.echo("")
def after_upgrade(ctx): last_version = app.get_state_item("last_version", "0.0.0") if last_version == __version__: return if last_version == "0.0.0": app.set_state_item("last_version", __version__) else: click.secho("Please wait while upgrading PlatformIO ...", fg="yellow") clean_cache() u = Upgrader(last_version, __version__) if u.run(ctx): app.set_state_item("last_version", __version__) # update development platforms pm = PlatformManager() for manifest in pm.get_installed(): # pm.update(manifest['name'], "^" + manifest['version']) pm.update(manifest['name']) # update PlatformIO Plus tool if installed pioplus_update() click.secho("PlatformIO has been successfully upgraded to %s!\n" % __version__, fg="green") telemetry.on_event(category="Auto", action="Upgrade", label="%s > %s" % (last_version, __version__)) else: raise exception.UpgradeError("Auto upgrading...") click.echo("") # PlatformIO banner terminal_width, _ = click.get_terminal_size() click.echo("*" * terminal_width) click.echo("If you like %s, please:" % (click.style("PlatformIO", fg="cyan"))) click.echo("- %s us on Twitter to stay up-to-date " "on the latest project news > %s" % (click.style("follow", fg="cyan"), click.style("https://twitter.com/PlatformIO_Org", fg="cyan"))) click.echo( "- %s it on GitHub > %s" % (click.style("star", fg="cyan"), click.style("https://github.com/platformio/platformio", fg="cyan"))) if not getenv("PLATFORMIO_IDE"): click.echo( "- %s PlatformIO IDE for IoT development > %s" % (click.style("try", fg="cyan"), click.style("http://platformio.org/platformio-ide", fg="cyan"))) if not util.is_ci(): click.echo("- %s us with PlatformIO Plus > %s" % (click.style("support", fg="cyan"), click.style("https://pioplus.com", fg="cyan"))) click.echo("*" * terminal_width) click.echo("")
def after_upgrade(ctx): last_version = app.get_state_item("last_version", "0.0.0") if last_version == __version__: return terminal_width, _ = click.get_terminal_size() # promotion click.echo("") click.echo("*" * terminal_width) click.echo("If you like %s, please:" % ( click.style("PlatformIO", fg="cyan") )) click.echo( "- %s us on Twitter to stay up-to-date " "on the latest project news > %s" % (click.style("follow", fg="cyan"), click.style("https://twitter.com/PlatformIO_Org", fg="cyan")) ) click.echo("- %s it on GitHub > %s" % ( click.style("star", fg="cyan"), click.style("https://github.com/platformio/platformio", fg="cyan") )) if not getenv("PLATFORMIO_IDE"): click.echo("- %s PlatformIO IDE for IoT development > %s" % ( click.style("try", fg="cyan"), click.style("http://platformio.org/#!/platformio-ide", fg="cyan") )) if not util.is_ci(): click.echo("- %s to keep PlatformIO alive! > %s" % ( click.style("donate", fg="cyan"), click.style("http://platformio.org/#!/donate", fg="cyan") )) click.echo("*" * terminal_width) click.echo("") if last_version == "0.0.0": app.set_state_item("last_version", __version__) return click.secho("Please wait while upgrading PlatformIO ...", fg="yellow") u = Upgrader(last_version, __version__) if u.run(ctx): app.set_state_item("last_version", __version__) ctx.invoke(cmd_platforms_update) click.secho("PlatformIO has been successfully upgraded to %s!\n" % __version__, fg="green") telemetry.on_event(category="Auto", action="Upgrade", label="%s > %s" % (last_version, __version__)) else: raise exception.UpgradeError("Auto upgrading...") click.echo("")
def get_platforms(cls, installed=False): platforms = cls._lookup_platforms() if not installed: return platforms installed_platforms = {} for type_ in get_state_item("installed_platforms", []): if type_ in platforms: installed_platforms[type_] = platforms[type_] return installed_platforms
def after_upgrade(ctx): last_version = app.get_state_item("last_version", "0.0.0") if last_version == __version__: return terminal_width, _ = click.get_terminal_size() # promotion click.echo("") click.echo("*" * terminal_width) click.echo("If you like %s, please:" % (click.style("PlatformIO", fg="cyan"))) click.echo("- %s us on Twitter to stay up-to-date " "on the latest project news > %s" % (click.style("follow", fg="cyan"), click.style("https://twitter.com/PlatformIO_Org", fg="cyan"))) click.echo( "- %s it on GitHub > %s" % (click.style("star", fg="cyan"), click.style("https://github.com/platformio/platformio", fg="cyan"))) if not getenv("PLATFORMIO_IDE"): click.echo("- %s PlatformIO IDE for IoT development > %s" % (click.style("try", fg="cyan"), click.style("http://platformio.org/#!/platformio-ide", fg="cyan"))) if not util.is_ci(): click.echo("- %s to keep PlatformIO alive! > %s" % (click.style("donate", fg="cyan"), click.style("http://platformio.org/#!/donate", fg="cyan"))) click.echo("*" * terminal_width) click.echo("") if last_version == "0.0.0": app.set_state_item("last_version", __version__) return click.secho("Please wait while upgrading PlatformIO ...", fg="yellow") u = Upgrader(last_version, __version__) if u.run(ctx): app.set_state_item("last_version", __version__) ctx.invoke(cmd_platforms_update) click.secho("PlatformIO has been successfully upgraded to %s!\n" % __version__, fg="green") telemetry.on_event(category="Auto", action="Upgrade", label="%s > %s" % (last_version, __version__)) else: raise exception.UpgradeError("Auto upgrading...") click.echo("")
def check_internal_updates(ctx, what): last_check = app.get_state_item("last_check", {}) interval = int(app.get_setting("check_%s_interval" % what)) * 3600 * 24 if (time() - interval) < last_check.get(what + "_update", 0): return last_check[what + '_update'] = int(time()) app.set_state_item("last_check", last_check) outdated_items = [] if what == "platforms": for platform in PlatformFactory.get_platforms(installed=True).keys(): p = PlatformFactory.newPlatform(platform) if p.is_outdated(): outdated_items.append(platform) elif what == "libraries": lm = LibraryManager() outdated_items = lm.get_outdated() if not outdated_items: return terminal_width, _ = click.get_terminal_size() click.echo("") click.echo("*" * terminal_width) click.secho("There are the new updates for %s (%s)" % (what, ", ".join(outdated_items)), fg="yellow") if not app.get_setting("auto_update_" + what): click.secho("Please update them via ", fg="yellow", nl=False) click.secho("`platformio %s update`" % ("lib" if what == "libraries" else "platforms"), fg="cyan", nl=False) click.secho(" command.", fg="yellow") else: click.secho("Please wait while updating %s ..." % what, fg="yellow") if what == "platforms": ctx.invoke(cmd_platforms_update) elif what == "libraries": ctx.invoke(cmd_libraries_update) click.echo() telemetry.on_event(category="Auto", action="Update", label=what.title()) click.echo("*" * terminal_width) click.echo("")
def after_upgrade(ctx): last_version = app.get_state_item("last_version", "0.0.0") if last_version == __version__: return terminal_width, _ = click.get_terminal_size() # promotion click.echo("") click.echo("*" * terminal_width) click.echo("If you like %s, please:" % ( click.style("PlatformIO", fg="cyan") )) click.echo( "- %s us on Twitter to stay up-to-date " "on the latest project news > %s" % (click.style("follow", fg="cyan"), click.style("https://twitter.com/PlatformIO_Org", fg="cyan")) ) click.echo("- %s us a star on GitHub > %s" % ( click.style("give", fg="cyan"), click.style("https://github.com/platformio/platformio", fg="cyan") )) click.echo("- %s for the new features/issues on Bountysource > %s" % ( click.style("vote", fg="cyan"), click.style("https://www.bountysource.com/teams/platformio/issues", fg="cyan") )) click.echo("*" * terminal_width) click.echo("") if last_version == "0.0.0": app.set_state_item("last_version", __version__) return click.secho("Please wait while upgrading PlatformIO ...", fg="yellow") u = Upgrader(last_version, __version__) if u.run(ctx): app.set_state_item("last_version", __version__) ctx.invoke(cmd_platforms_update) click.secho("PlatformIO has been successfully upgraded to %s!\n" % __version__, fg="green") telemetry.on_event(category="Auto", action="Upgrade", label="%s > %s" % (last_version, __version__)) else: raise exception.UpgraderFailed() click.echo("")
def after_upgrade(ctx): if app.get_state_item("last_version", None) == __version__: return # promotion click.echo("\nIf you like %s, please:" % ( click.style("PlatformIO", fg="cyan") )) click.echo( "- %s us on Twitter to stay up-to-date " "on the latest project news > %s" % (click.style("follow", fg="cyan"), click.style("https://twitter.com/PlatformIO_Org", fg="cyan")) ) click.echo("- %s us a star on GitHub > %s" % ( click.style("give", fg="cyan"), click.style("https://github.com/ivankravets/platformio", fg="cyan") )) click.secho("Thanks a lot!\n", fg="green") if not isdir(get_home_dir()): return click.secho("Please wait while upgrading PlatformIO ...", fg="yellow") last_version = app.get_state_item("last_version", "0.0.0") u = Upgrader(last_version, __version__) if u.run(ctx): app.set_state_item("last_version", __version__) click.secho("PlatformIO has been successfully upgraded to %s!\n" % __version__, fg="green") telemetry.on_event(category="Auto", action="Upgrade", label="%s > %s" % (last_version, __version__)) else: raise UpgraderFailed() click.echo("")
def resend_backuped_reports(): tm = app.get_state_item("telemetry", {}) if "backup" not in tm or not tm['backup']: return False for report in tm['backup']: mp = MeasurementProtocol() for key, value in report.items(): mp[key] = value mp.send(report['t']) # clean tm['backup'] = [] app.set_state_item("telemetry", tm)
def resend_backuped_report(): tm = app.get_state_item("telemetry", {}) if "backup" not in tm or not tm['backup']: return False report = tm['backup'].pop() app.set_state_item("telemetry", tm) mp = MeasurementProtocol() for key, value in report.items(): mp[key] = value mp.send(report['t']) return True
def check_platformio_upgrade(): last_check = app.get_state_item("last_check", {}) interval = int(app.get_setting("check_platformio_interval")) * 3600 * 24 if (time() - interval) < last_check.get("platformio_upgrade", 0): return last_check["platformio_upgrade"] = int(time()) app.set_state_item("last_check", last_check) util.internet_on(raise_exception=True) # Update PlatformIO's Core packages update_core_packages(silent=True) latest_version = get_latest_version() if semantic_version.Version.coerce(util.pepver_to_semver( latest_version)) <= semantic_version.Version.coerce( util.pepver_to_semver(__version__)): return terminal_width, _ = click.get_terminal_size() click.echo("") click.echo("*" * terminal_width) click.secho( "There is a new version %s of PlatformIO available.\n" "Please upgrade it via `" % latest_version, fg="yellow", nl=False, ) if getenv("PLATFORMIO_IDE"): click.secho("PlatformIO IDE Menu: Upgrade PlatformIO", fg="cyan", nl=False) click.secho("`.", fg="yellow") elif join("Cellar", "platformio") in fs.get_source_dir(): click.secho("brew update && brew upgrade", fg="cyan", nl=False) click.secho("` command.", fg="yellow") else: click.secho("platformio upgrade", fg="cyan", nl=False) click.secho("` or `", fg="yellow", nl=False) click.secho("pip install -U platformio", fg="cyan", nl=False) click.secho("` command.", fg="yellow") click.secho("Changes: ", fg="yellow", nl=False) click.secho("https://docs.platformio.org/en/latest/history.html", fg="cyan") click.echo("*" * terminal_width) click.echo("")
def fetch_authentication_token(self): if "PLATFORMIO_AUTH_TOKEN" in os.environ: return os.environ["PLATFORMIO_AUTH_TOKEN"] auth = app.get_state_item("account", {}).get("auth", {}) if auth.get("access_token") and auth.get("access_token_expire"): if auth.get("access_token_expire") > time.time(): return auth.get("access_token") if auth.get("refresh_token"): response = self._session.post( self.api_base_url + "/v1/login", headers={"Authorization": "Bearer %s" % auth.get("refresh_token")}, ) result = self.raise_error_from_response(response) app.set_state_item("account", result) return result.get("auth").get("access_token") raise exception.AccountNotAuthenticated()
def login_with_code(self, client_id, code, redirect_uri): try: self.fetch_authentication_token() except: # pylint:disable=bare-except pass else: raise exception.AccountAlreadyAuthenticated( app.get_state_item("account", {}).get("email", "") ) response = self._session.post( self.api_base_url + "/v1/login/code", data={"client_id": client_id, "code": code, "redirect_uri": redirect_uri}, ) result = self.raise_error_from_response(response) app.set_state_item("account", result) return result
def login_with_code(self, client_id, code, redirect_uri): try: self.fetch_authentication_token() except: # pylint:disable=bare-except pass else: raise AccountAlreadyAuthorized( app.get_state_item("account", {}).get("email", "") ) result = self.fetch_json_data( "post", "/v1/login/code", data={"client_id": client_id, "code": code, "redirect_uri": redirect_uri}, ) app.set_state_item("account", result) return result
def check_platformio_upgrade(): last_check = app.get_state_item("last_check", {}) interval = int(app.get_setting("check_platformio_interval")) * 3600 * 24 if (time() - interval) < last_check.get("platformio_upgrade", 0): return last_check['platformio_upgrade'] = int(time()) app.set_state_item("last_check", last_check) util.internet_on(raise_exception=True) # Update PlatformIO's Core packages update_core_packages(silent=True) latest_version = get_latest_version() if semantic_version.Version.coerce(util.pepver_to_semver( latest_version)) <= semantic_version.Version.coerce( util.pepver_to_semver(__version__)): return terminal_width, _ = click.get_terminal_size() click.echo("") click.echo("*" * terminal_width) click.secho( "There is a new version %s of PlatformIO available.\n" "Please upgrade it via `" % latest_version, fg="yellow", nl=False) if getenv("PLATFORMIO_IDE"): click.secho( "PlatformIO IDE Menu: Upgrade PlatformIO", fg="cyan", nl=False) click.secho("`.", fg="yellow") elif join("Cellar", "platformio") in util.get_source_dir(): click.secho("brew update && brew upgrade", fg="cyan", nl=False) click.secho("` command.", fg="yellow") else: click.secho("platformio upgrade", fg="cyan", nl=False) click.secho("` or `", fg="yellow", nl=False) click.secho("pip install -U platformio", fg="cyan", nl=False) click.secho("` command.", fg="yellow") click.secho("Changes: ", fg="yellow", nl=False) click.secho( "https://docs.platformio.org/en/latest/history.html", fg="cyan") click.echo("*" * terminal_width) click.echo("")
def login(self, username, password): try: self.fetch_authentication_token() except: # pylint:disable=bare-except pass else: raise exception.AccountAlreadyAuthenticated( app.get_state_item("account", {}).get("email", "") ) response = self._session.post( self.api_base_url + "/v1/login", data={"username": username, "password": password}, ) result = self.raise_error_from_response(response) app.set_state_item("account", result) return result
def login(self, username, password): try: self.fetch_authentication_token() except: # pylint:disable=bare-except pass else: raise AccountAlreadyAuthorized( app.get_state_item("account", {}).get("email", "") ) data = self.fetch_json_data( "post", "/v1/login", data={"username": username, "password": password}, ) app.set_state_item("account", data) return data
def get_account_info(self, offline): if offline: account = app.get_state_item("account") if not account: raise exception.AccountNotAuthenticated() return { "profile": { "email": account.get("email"), "username": account.get("username"), } } try: token = self.fetch_authentication_token() except: # pylint:disable=bare-except raise exception.AccountNotAuthenticated() response = self._session.get( self.api_base_url + "/v1/summary", headers={"Authorization": "Bearer %s" % token}, ) return self.raise_error_from_response(response)
def registration(self, username, email, password, firstname, lastname): # pylint:disable=too-many-arguments try: self.fetch_authentication_token() except: # pylint:disable=bare-except pass else: raise exception.AccountAlreadyAuthorized( app.get_state_item("account", {}).get("email", "")) return self.send_request( "post", self.api_base_url + "/v1/registration", data={ "username": username, "email": email, "password": password, "firstname": firstname, "lastname": lastname, }, )
def fetch_authentication_token(self): if os.environ.get("PLATFORMIO_AUTH_TOKEN"): return os.environ.get("PLATFORMIO_AUTH_TOKEN") auth = app.get_state_item("account", {}).get("auth", {}) if auth.get("access_token") and auth.get("access_token_expire"): if auth.get("access_token_expire") > time.time(): return auth.get("access_token") if auth.get("refresh_token"): try: data = self.fetch_json_data( "post", "/v1/login", headers={ "Authorization": "Bearer %s" % auth.get("refresh_token") }, ) app.set_state_item("account", data) return data.get("auth").get("access_token") except AccountError: self.delete_local_session() raise AccountNotAuthorized()
def fetch_authentication_token(self): if "PLATFORMIO_AUTH_TOKEN" in os.environ: return os.environ["PLATFORMIO_AUTH_TOKEN"] auth = app.get_state_item("account", {}).get("auth", {}) if auth.get("access_token") and auth.get("access_token_expire"): if auth.get("access_token_expire") > time.time(): return auth.get("access_token") if auth.get("refresh_token"): try: result = self.send_request( "post", self.api_base_url + "/v1/login", headers={ "Authorization": "Bearer %s" % auth.get("refresh_token") }, ) app.set_state_item("account", result) return result.get("auth").get("access_token") except exception.AccountError: self.delete_local_session() raise exception.AccountNotAuthorized()
def after_upgrade(ctx): last_version = app.get_state_item("last_version", "0.0.0") if last_version == __version__: return # promotion click.echo("\nIf you like %s, please:" % ( click.style("PlatformIO", fg="cyan") )) click.echo( "- %s us on Twitter to stay up-to-date " "on the latest project news > %s" % (click.style("follow", fg="cyan"), click.style("https://twitter.com/PlatformIO_Org", fg="cyan")) ) click.echo("- %s us a star on GitHub > %s" % ( click.style("give", fg="cyan"), click.style("https://github.com/ivankravets/platformio", fg="cyan") )) click.secho("Thanks a lot!\n", fg="green") if last_version == "0.0.0": app.set_state_item("last_version", __version__) return click.secho("Please wait while upgrading PlatformIO ...", fg="yellow") u = Upgrader(last_version, __version__) if u.run(ctx): app.set_state_item("last_version", __version__) click.secho("PlatformIO has been successfully upgraded to %s!\n" % __version__, fg="green") telemetry.on_event(category="Auto", action="Upgrade", label="%s > %s" % (last_version, __version__)) else: raise UpgraderFailed() click.echo("")
def _upgrade_to_3_0_0(ctx): # convert custom board configuration boards_dir = join(util.get_home_dir(), "boards") if isdir(boards_dir): for item in os.listdir(boards_dir): if not item.endswith(".json"): continue data = util.load_json(join(boards_dir, item)) if set(["name", "url", "vendor"]) <= set(data.keys()): continue os.remove(join(boards_dir, item)) for key, value in data.items(): with open(join(boards_dir, "%s.json" % key), "w") as f: json.dump(value, f, sort_keys=True, indent=2) # re-install PlatformIO 2.0 development platforms installed_platforms = app.get_state_item("installed_platforms", []) if installed_platforms: if "espressif" in installed_platforms: installed_platforms[installed_platforms.index( "espressif")] = "espressif8266" ctx.invoke(cmd_platform_install, platforms=installed_platforms) return True
def get_installed(): return get_state_item("installed_packages", {})
def after_upgrade(ctx): terminal_width, _ = click.get_terminal_size() last_version = app.get_state_item("last_version", "0.0.0") if last_version == __version__: return if last_version == "0.0.0": app.set_state_item("last_version", __version__) elif semantic_version.Version.coerce(util.pepver_to_semver( last_version)) > semantic_version.Version.coerce( util.pepver_to_semver(__version__)): click.secho("*" * terminal_width, fg="yellow") click.secho( "Obsolete PIO Core v%s is used (previous was %s)" % (__version__, last_version), fg="yellow") click.secho( "Please remove multiple PIO Cores from a system:", fg="yellow") click.secho( "http://docs.platformio.org/page/faq.html" "#multiple-pio-cores-in-a-system", fg="cyan") click.secho("*" * terminal_width, fg="yellow") return else: click.secho("Please wait while upgrading PlatformIO...", fg="yellow") app.clean_cache() # Update PlatformIO's Core packages update_core_packages(silent=True) u = Upgrader(last_version, __version__) if u.run(ctx): app.set_state_item("last_version", __version__) click.secho( "PlatformIO has been successfully upgraded to %s!\n" % __version__, fg="green") telemetry.on_event( category="Auto", action="Upgrade", label="%s > %s" % (last_version, __version__)) else: raise exception.UpgradeError("Auto upgrading...") click.echo("") # PlatformIO banner click.echo("*" * terminal_width) click.echo("If you like %s, please:" % (click.style("PlatformIO", fg="cyan"))) click.echo("- %s us on Twitter to stay up-to-date " "on the latest project news > %s" % (click.style("follow", fg="cyan"), click.style("https://twitter.com/PlatformIO_Org", fg="cyan"))) click.echo( "- %s it on GitHub > %s" % (click.style("star", fg="cyan"), click.style("https://github.com/platformio/platformio", fg="cyan"))) if not getenv("PLATFORMIO_IDE"): click.echo( "- %s PlatformIO IDE for IoT development > %s" % (click.style("try", fg="cyan"), click.style("http://platformio.org/platformio-ide", fg="cyan"))) if not util.is_ci(): click.echo("- %s us with PlatformIO Plus > %s" % (click.style("support", fg="cyan"), click.style("https://pioplus.com", fg="cyan"))) click.echo("*" * terminal_width) click.echo("")
def get_cid(self): cid = app.get_state_item("cid") if not cid: cid = self.MACHINE_ID app.set_state_item("cid", cid) return cid
def after_upgrade(ctx): terminal_width, _ = click.get_terminal_size() last_version = app.get_state_item("last_version", "0.0.0") if last_version == __version__: return if last_version == "0.0.0": app.set_state_item("last_version", __version__) elif semantic_version.Version.coerce(util.pepver_to_semver( last_version)) > semantic_version.Version.coerce( util.pepver_to_semver(__version__)): click.secho("*" * terminal_width, fg="yellow") click.secho( "Obsolete PIO Core v%s is used (previous was %s)" % (__version__, last_version), fg="yellow") click.secho( "Please remove multiple PIO Cores from a system:", fg="yellow") click.secho( "https://docs.platformio.org/page/faq.html" "#multiple-pio-cores-in-a-system", fg="cyan") click.secho("*" * terminal_width, fg="yellow") return else: click.secho("Please wait while upgrading PlatformIO...", fg="yellow") app.clean_cache() # Update PlatformIO's Core packages update_core_packages(silent=True) u = Upgrader(last_version, __version__) if u.run(ctx): app.set_state_item("last_version", __version__) click.secho( "PlatformIO has been successfully upgraded to %s!\n" % __version__, fg="green") telemetry.on_event( category="Auto", action="Upgrade", label="%s > %s" % (last_version, __version__)) else: raise exception.UpgradeError("Auto upgrading...") click.echo("") # PlatformIO banner click.echo("*" * terminal_width) click.echo( "If you like %s, please:" % (click.style("PlatformIO", fg="cyan"))) click.echo("- %s us on Twitter to stay up-to-date " "on the latest project news > %s" % (click.style("follow", fg="cyan"), click.style("https://twitter.com/PlatformIO_Org", fg="cyan"))) click.echo( "- %s it on GitHub > %s" % (click.style("star", fg="cyan"), click.style("https://github.com/platformio/platformio", fg="cyan"))) if not getenv("PLATFORMIO_IDE"): click.echo( "- %s PlatformIO IDE for IoT development > %s" % (click.style("try", fg="cyan"), click.style("https://platformio.org/platformio-ide", fg="cyan"))) if not util.is_ci(): click.echo("- %s us with PlatformIO Plus > %s" % (click.style( "support", fg="cyan"), click.style( "https://pioplus.com", fg="cyan"))) click.echo("*" * terminal_width) click.echo("")
def check_internal_updates(ctx, what): last_check = app.get_state_item("last_check", {}) interval = int(app.get_setting("check_%s_interval" % what)) * 3600 * 24 if (time() - interval) < last_check.get(what + "_update", 0): return last_check[what + '_update'] = int(time()) app.set_state_item("last_check", last_check) pm = PlatformManager() if what == "platforms" else LibraryManager() outdated_items = [] for manifest in pm.get_installed(): if manifest['name'] not in outdated_items and \ pm.is_outdated(manifest['name']): outdated_items.append(manifest['name']) if not outdated_items: return terminal_width, _ = click.get_terminal_size() click.echo("") click.echo("*" * terminal_width) click.secho( "There are the new updates for %s (%s)" % (what, ", ".join(outdated_items)), fg="yellow") if not app.get_setting("auto_update_" + what): click.secho("Please update them via ", fg="yellow", nl=False) click.secho( "`platformio %s update`" % ("lib --global" if what == "libraries" else "platform"), fg="cyan", nl=False) click.secho(" command.\n", fg="yellow") click.secho( "If you want to manually check for the new versions " "without updating, please use ", fg="yellow", nl=False) click.secho( "`platformio %s update --only-check`" % ("lib --global" if what == "libraries" else "platform"), fg="cyan", nl=False) click.secho(" command.", fg="yellow") else: click.secho("Please wait while updating %s ..." % what, fg="yellow") if what == "platforms": ctx.invoke(cmd_platform_update, platforms=outdated_items) elif what == "libraries": ctx.obj = pm ctx.invoke(cmd_lib_update, libraries=outdated_items) click.echo() telemetry.on_event( category="Auto", action="Update", label=what.title()) click.echo("*" * terminal_width) click.echo("")