def process_dependencies(self): # pylint: disable=too-many-branches for uri in self.dependencies: found = False for storage_dir in self.env.GetLibSourceDirs(): if found: break lm = LibraryManager(storage_dir) lib_dir = lm.get_package_dir(*lm.parse_pkg_uri(uri)) if not lib_dir: continue for lb in self.env.GetLibBuilders(): if lib_dir not in lb: continue if lb not in self.depbuilders: self.depend_recursive(lb) found = True break if found: continue # look for built-in libraries by a name # which don't have package manifest for lb in self.env.GetLibBuilders(): if lb.name != uri: continue if lb not in self.depbuilders: self.depend_recursive(lb) found = True break
def process_dependencies(self): # pylint: disable=too-many-branches uris = self.env.get("LIB_DEPS", []) if not uris: return storage_dirs = [] for lb in self.env.GetLibBuilders(): if dirname(lb.path) not in storage_dirs: storage_dirs.append(dirname(lb.path)) for uri in uris: found = False for storage_dir in storage_dirs: if found: break lm = LibraryManager(storage_dir) pkg_dir = lm.get_package_dir(*lm.parse_pkg_uri(uri)) if not pkg_dir: continue for lb in self.env.GetLibBuilders(): if lb.path != pkg_dir: continue if lb not in self.depbuilders: self.depend_recursive(lb) found = True break if not found: for lb in self.env.GetLibBuilders(): if lb.name != uri: continue if lb not in self.depbuilders: self.depend_recursive(lb) break
def lib_uninstall(ctx, libraries): storage_dirs = ctx.meta[CTX_META_STORAGE_DIRS_KEY] for storage_dir in storage_dirs: print_storage_header(storage_dirs, storage_dir) lm = LibraryManager(storage_dir) for library in libraries: lm.uninstall(library)
def get_dependencies(): lm = LibraryManager(platformio.util.get_projectlibdeps_dir()) dependencies = {library['name']: library['version'] for library in lm.get_installed()} platform = env['PIOPLATFORM'] dependencies[platform] = PlatformFactory.newPlatform(platform).version dependencies['PlatformIO'] = platformio.__version__ return dependencies
def get_dependencies(): lib_storage = os.path.join(env['PROJECTLIBDEPS_DIR'], env['PIOENV']) lm = LibraryManager(lib_storage) dependencies = {library['name']: library['version'] for library in lm.get_installed()} platform = env['PIOPLATFORM'] dependencies[platform] = PlatformFactory.newPlatform(platform).version dependencies['PlatformIO'] = platformio.__version__ return dependencies
def install_dependencies(self): def _is_builtin(uri): for lb in self.env.GetLibBuilders(): if lb.name == uri: return True return False not_found_uri = [] for uri in self.dependencies: # check if built-in library if _is_builtin(uri): continue found = False for storage_dir in self.env.GetLibSourceDirs(): lm = LibraryManager(storage_dir) if lm.get_package_dir(*lm.parse_pkg_uri(uri)): found = True break if not found: not_found_uri.append(uri) did_install = False lm = LibraryManager( self.env.subst(join("$PROJECT_LIBDEPS_DIR", "$PIOENV"))) for uri in not_found_uri: try: lm.install(uri) did_install = True except (exception.LibNotFound, exception.InternetIsOffline) as e: click.secho("Warning! %s" % e, fg="yellow") # reset cache if did_install: DefaultEnvironment().Replace(__PIO_LIB_BUILDERS=None)
def get_builtin_libs(storage_names=None): items = [] storage_names = storage_names or [] pm = PlatformManager() for manifest in pm.get_installed(): p = PlatformFactory.newPlatform(manifest['__pkg_dir']) for storage in p.get_lib_storages(): if storage_names and storage['name'] not in storage_names: continue lm = LibraryManager(storage['path']) items.append({ "name": storage['name'], "path": storage['path'], "items": lm.get_installed() }) return items
def cli(ctx, **options): non_storage_cmds = ("search", "show", "register", "stats", "builtin") # skip commands that don't need storage folder if ctx.invoked_subcommand in non_storage_cmds or \ (len(ctx.args) == 2 and ctx.args[1] in ("-h", "--help")): return storage_dir = options['storage_dir'] if not storage_dir: if options['global']: storage_dir = join(util.get_home_dir(), "lib") elif util.is_platformio_project(): storage_dir = util.get_projectlibdeps_dir() elif util.is_ci(): storage_dir = join(util.get_home_dir(), "lib") click.secho( "Warning! Global library storage is used automatically. " "Please use `platformio lib --global %s` command to remove " "this warning." % ctx.invoked_subcommand, fg="yellow") if not storage_dir and not util.is_platformio_project(): raise exception.NotGlobalLibDir(util.get_project_dir(), join(util.get_home_dir(), "lib"), ctx.invoked_subcommand) ctx.obj = LibraryManager(storage_dir) if "--json-output" not in ctx.args: click.echo("Library Storage: " + storage_dir)
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 _autoinstall_libdeps(ctx, libraries, verbose=False): storage_dir = util.get_projectlibdeps_dir() ctx.obj = LibraryManager(storage_dir) if verbose: click.echo("Library Storage: " + storage_dir) for lib in libraries: try: ctx.invoke(cmd_lib_install, libraries=[lib], silent=not verbose) except exception.LibNotFound as e: click.secho("Warning! %s" % e, fg="yellow")
def cli(ctx, only_check): click.echo("Platform Manager") click.echo("================") ctx.invoke(cmd_platform_update, only_check=only_check) pioplus_update() click.echo() click.echo("Library Manager") click.echo("===============") ctx.obj = LibraryManager() ctx.invoke(cmd_lib_update, only_check=only_check)
def library_manager_libdeps(lib_deps, storage=None): from platformio.managers.lib import LibraryManager from platformio.project.helpers import get_project_global_lib_dir if not storage: manager = LibraryManager(get_project_global_lib_dir()) else: manager = LibraryManager(storage) for lib in lib_deps: if manager.get_package_dir(*manager.parse_pkg_uri(lib)): continue log("installing: {}".format(lib)) manager.install(lib)
def lib_install( # pylint: disable=too-many-arguments ctx, libraries, save, silent, interactive, force): storage_dirs = ctx.meta[CTX_META_STORAGE_DIRS_KEY] storage_libdeps = ctx.meta.get(CTX_META_STORAGE_LIBDEPS_KEY, []) installed_manifests = {} for storage_dir in storage_dirs: if not silent and (libraries or storage_dir in storage_libdeps): print_storage_header(storage_dirs, storage_dir) lm = LibraryManager(storage_dir) if libraries: for library in libraries: pkg_dir = lm.install(library, silent=silent, interactive=interactive, force=force) installed_manifests[library] = lm.load_manifest(pkg_dir) elif storage_dir in storage_libdeps: builtin_lib_storages = None for library in storage_libdeps[storage_dir]: try: pkg_dir = lm.install(library, silent=silent, interactive=interactive, force=force) installed_manifests[library] = lm.load_manifest(pkg_dir) except exception.LibNotFound as e: if builtin_lib_storages is None: builtin_lib_storages = get_builtin_libs() if not silent or not is_builtin_lib( builtin_lib_storages, library): click.secho("Warning! %s" % e, fg="yellow") if not save or not libraries: return input_dirs = ctx.meta.get(CTX_META_INPUT_DIRS_KEY, []) project_environments = ctx.meta[CTX_META_PROJECT_ENVIRONMENTS_KEY] for input_dir in input_dirs: config = ProjectConfig.get_instance( os.path.join(input_dir, "platformio.ini")) config.validate(project_environments) for env in config.envs(): if project_environments and env not in project_environments: continue config.expand_interpolations = False lib_deps = config.get("env:" + env, "lib_deps", []) for library in libraries: if library in lib_deps: continue manifest = installed_manifests[library] try: assert library.lower() == manifest["name"].lower() assert semantic_version.Version(manifest["version"]) lib_deps.append("{name}@^{version}".format(**manifest)) except (AssertionError, ValueError): lib_deps.append(library) config.set("env:" + env, "lib_deps", lib_deps) config.save()
def lib_list(ctx, json_output): storage_dirs = ctx.meta[CTX_META_STORAGE_DIRS_KEY] json_result = {} for storage_dir in storage_dirs: if not json_output: print_storage_header(storage_dirs, storage_dir) lm = LibraryManager(storage_dir) items = lm.get_installed() if json_output: json_result[storage_dir] = items elif items: for item in sorted(items, key=lambda i: i['name']): print_lib_item(item) else: click.echo("No items found") if json_output: return click.echo( dump_json_to_unicode(json_result[storage_dirs[0]] if len(storage_dirs) == 1 else json_result)) return True
def lib_update(ctx, libraries, only_check, dry_run, json_output): storage_dirs = ctx.meta[CTX_META_STORAGE_DIRS_KEY] only_check = dry_run or only_check json_result = {} for storage_dir in storage_dirs: if not json_output: print_storage_header(storage_dirs, storage_dir) lm = LibraryManager(storage_dir) _libraries = libraries if not _libraries: _libraries = [ manifest['__pkg_dir'] for manifest in lm.get_installed() ] if only_check and json_output: result = [] for library in _libraries: pkg_dir = library if isdir(library) else None requirements = None url = None if not pkg_dir: name, requirements, url = lm.parse_pkg_uri(library) pkg_dir = lm.get_package_dir(name, requirements, url) if not pkg_dir: continue latest = lm.outdated(pkg_dir, requirements) if not latest: continue manifest = lm.load_manifest(pkg_dir) manifest['versionLatest'] = latest result.append(manifest) json_result[storage_dir] = result else: for library in _libraries: lm.update(library, only_check=only_check) if json_output: return click.echo( dump_json_to_unicode(json_result[storage_dirs[0]] if len(storage_dirs) == 1 else json_result)) return True
def _autoinstall_libdeps(ctx, libraries, verbose=False): if not libraries: return storage_dir = util.get_projectlibdeps_dir() ctx.obj = LibraryManager(storage_dir) if verbose: click.echo("Library Storage: " + storage_dir) for lib in libraries: try: ctx.invoke(cmd_lib_install, libraries=[lib], silent=not verbose) except exception.LibNotFound as e: if verbose or not is_builtin_lib(lib): click.secho("Warning! %s" % e, fg="yellow") except exception.InternetIsOffline as e: click.secho(str(e), fg="yellow")
def cli(ctx, core_packages, only_check): # cleanup lib search results, cached board and platform lists app.clean_cache() update_core_packages(only_check) if core_packages: return click.echo() click.echo("Platform Manager") click.echo("================") ctx.invoke(cmd_platform_update, only_check=only_check) click.echo() click.echo("Library Manager") click.echo("===============") ctx.obj = LibraryManager() ctx.invoke(cmd_lib_update, only_check=only_check)
def cli(ctx, core_packages, only_check, dry_run): # cleanup lib search results, cached board and platform lists app.clean_cache() only_check = dry_run or only_check update_core_packages(only_check) if core_packages: return click.echo() click.echo("Platform Manager") click.echo("================") ctx.invoke(cmd_platform_update, only_check=only_check) click.echo() click.echo("Library Manager") click.echo("===============") ctx.meta[CTX_META_STORAGE_DIRS_KEY] = [LibraryManager().package_dir] ctx.invoke(cmd_lib_update, only_check=only_check)
def _autoinstall_libdeps(ctx, libraries, verbose=False): storage_dir = util.get_projectlibdeps_dir() ctx.obj = LibraryManager(storage_dir) if verbose: click.echo("Library Storage: " + storage_dir) ctx.invoke(cmd_lib_install, libraries=libraries, silent=not verbose)
def dependencies(self): return LibraryManager.normalize_dependencies( self._manifest.get("dependencies", []))
def lib_show(library, json_output): lm = LibraryManager() name, requirements, _ = lm.parse_pkg_input(library) lib_id = lm.get_pkg_id_by_name(name, requirements, silent=json_output, interactive=not json_output) lib = get_api_result("/lib/info/%d" % lib_id, cache_valid="1d") if json_output: return click.echo(json.dumps(lib)) click.secho(lib['name'], fg="cyan") click.echo("=" * len(lib['name'])) click.secho("#ID: %d" % lib['id'], bold=True) click.echo(lib['description']) click.echo() click.echo("Version: %s, released %s" % (lib['version']['name'], arrow.get( lib['version']['released']).humanize())) click.echo("Manifest: %s" % lib['confurl']) for key in ("homepage", "repository", "license"): if key not in lib or not lib[key]: continue if isinstance(lib[key], list): click.echo("%s: %s" % (key.title(), ", ".join(lib[key]))) else: click.echo("%s: %s" % (key.title(), lib[key])) blocks = [] _authors = [] for author in lib.get("authors", []): _data = [] for key in ("name", "email", "url", "maintainer"): if not author[key]: continue if key == "email": _data.append("<%s>" % author[key]) elif key == "maintainer": _data.append("(maintainer)") else: _data.append(author[key]) _authors.append(" ".join(_data)) if _authors: blocks.append(("Authors", _authors)) blocks.append(("Keywords", lib['keywords'])) for key in ("frameworks", "platforms"): if key not in lib or not lib[key]: continue blocks.append(("Compatible %s" % key, [i['title'] for i in lib[key]])) blocks.append(("Headers", lib['headers'])) blocks.append(("Examples", lib['examples'])) blocks.append(("Versions", [ "%s, released %s" % (v['name'], arrow.get(v['released']).humanize()) for v in lib['versions'] ])) blocks.append(("Unique Downloads", [ "Today: %s" % lib['dlstats']['day'], "Week: %s" % lib['dlstats']['week'], "Month: %s" % lib['dlstats']['month'] ])) for (title, rows) in blocks: click.echo() click.secho(title, bold=True) click.echo("-" * len(title)) for row in rows: click.echo(row)
def lib_show(library, json_output): lm = LibraryManager() name, requirements, _ = lm.parse_pkg_uri(library) lib_id = lm.search_lib_id( { "name": name, "requirements": requirements }, silent=json_output, interactive=not json_output, ) lib = util.get_api_result("/lib/info/%d" % lib_id, cache_valid="1d") if json_output: return click.echo(dump_json_to_unicode(lib)) click.secho(lib["name"], fg="cyan") click.echo("=" * len(lib["name"])) click.secho("#ID: %d" % lib["id"], bold=True) click.echo(lib["description"]) click.echo() click.echo("Version: %s, released %s" % ( lib["version"]["name"], time.strftime("%c", util.parse_date(lib["version"]["released"])), )) click.echo("Manifest: %s" % lib["confurl"]) for key in ("homepage", "repository", "license"): if key not in lib or not lib[key]: continue if isinstance(lib[key], list): click.echo("%s: %s" % (key.title(), ", ".join(lib[key]))) else: click.echo("%s: %s" % (key.title(), lib[key])) blocks = [] _authors = [] for author in lib.get("authors", []): _data = [] for key in ("name", "email", "url", "maintainer"): if not author[key]: continue if key == "email": _data.append("<%s>" % author[key]) elif key == "maintainer": _data.append("(maintainer)") else: _data.append(author[key]) _authors.append(" ".join(_data)) if _authors: blocks.append(("Authors", _authors)) blocks.append(("Keywords", lib["keywords"])) for key in ("frameworks", "platforms"): if key not in lib or not lib[key]: continue blocks.append(("Compatible %s" % key, [i["title"] for i in lib[key]])) blocks.append(("Headers", lib["headers"])) blocks.append(("Examples", lib["examples"])) blocks.append(( "Versions", [ "%s, released %s" % (v["name"], time.strftime("%c", util.parse_date(v["released"]))) for v in lib["versions"] ], )) blocks.append(( "Unique Downloads", [ "Today: %s" % lib["dlstats"]["day"], "Week: %s" % lib["dlstats"]["week"], "Month: %s" % lib["dlstats"]["month"], ], )) for (title, rows) in blocks: click.echo() click.secho(title, bold=True) click.echo("-" * len(title)) for row in rows: click.echo(row) return True
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) util.internet_on(raise_exception=True) pm = PlatformManager() if what == "platforms" else LibraryManager() outdated_items = [] for manifest in pm.get_installed(): if manifest["name"] in outdated_items: continue conds = [ pm.outdated(manifest["__pkg_dir"]), what == "platforms" and PlatformFactory.newPlatform( manifest["__pkg_dir"]).are_outdated_packages(), ] if any(conds): 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 --dry-run`" % ("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.meta[CTX_META_STORAGE_DIRS_KEY] = [pm.package_dir] ctx.invoke(cmd_lib_update, libraries=outdated_items) click.echo() telemetry.send_event(category="Auto", action="Update", label=what.title()) click.echo("*" * terminal_width) click.echo("")
def lib_show(library, json_output): lm = LibraryManager() name, requirements, _ = lm.parse_pkg_uri(library) lib_id = lm.search_lib_id({ "name": name, "requirements": requirements }, silent=json_output, interactive=not json_output) lib = get_api_result("/lib/info/%d" % lib_id, cache_valid="1d") if json_output: return click.echo(json.dumps(lib)) click.secho(lib['name'], fg="cyan") click.echo("=" * len(lib['name'])) click.secho("#ID: %d" % lib['id'], bold=True) click.echo(lib['description']) click.echo() click.echo( "Version: %s, released %s" % (lib['version']['name'], time.strftime("%c", util.parse_date(lib['version']['released'])))) click.echo("Manifest: %s" % lib['confurl']) for key in ("homepage", "repository", "license"): if key not in lib or not lib[key]: continue if isinstance(lib[key], list): click.echo("%s: %s" % (key.title(), ", ".join(lib[key]))) else: click.echo("%s: %s" % (key.title(), lib[key])) blocks = [] _authors = [] for author in lib.get("authors", []): _data = [] for key in ("name", "email", "url", "maintainer"): if not author[key]: continue if key == "email": _data.append("<%s>" % author[key]) elif key == "maintainer": _data.append("(maintainer)") else: _data.append(author[key]) _authors.append(" ".join(_data)) if _authors: blocks.append(("Authors", _authors)) blocks.append(("Keywords", lib['keywords'])) for key in ("frameworks", "platforms"): if key not in lib or not lib[key]: continue blocks.append(("Compatible %s" % key, [i['title'] for i in lib[key]])) blocks.append(("Headers", lib['headers'])) blocks.append(("Examples", lib['examples'])) blocks.append(("Versions", [ "%s, released %s" % (v['name'], time.strftime("%c", util.parse_date(v['released']))) for v in lib['versions'] ])) blocks.append(("Unique Downloads", [ "Today: %s" % lib['dlstats']['day'], "Week: %s" % lib['dlstats']['week'], "Month: %s" % lib['dlstats']['month'] ])) for (title, rows) in blocks: click.echo() click.secho(title, bold=True) click.echo("-" * len(title)) for row in rows: click.echo(row) return True
def _get_vcs_info(lb): path = LibraryManager.get_src_manifest_path(lb.path) return fs.load_json(path) if path else None