def list_dir(path): def _cmp(x, y): if x[1] and not y[1]: return -1 if not x[1] and y[1]: return 1 if x[0].lower() > y[0].lower(): return 1 if x[0].lower() < y[0].lower(): return -1 return 0 items = [] if path.startswith("~"): path = fs.expanduser(path) if not os.path.isdir(path): return items for item in os.listdir(path): try: item_is_dir = os.path.isdir(os.path.join(path, item)) if item_is_dir: os.listdir(os.path.join(path, item)) items.append((item, item_is_dir)) except OSError: pass return sorted(items, key=cmp_to_key(_cmp))
def _get_project_data(): data = {"boards": [], "envLibdepsDirs": [], "libExtraDirs": []} config = ProjectConfig() data["envs"] = config.envs() data["description"] = config.get("platformio", "description") data["libExtraDirs"].extend( config.get("platformio", "lib_extra_dirs", [])) libdeps_dir = config.get_optional_dir("libdeps") for section in config.sections(): if not section.startswith("env:"): continue data["envLibdepsDirs"].append(join(libdeps_dir, section[4:])) if config.has_option(section, "board"): data["boards"].append(config.get(section, "board")) data["libExtraDirs"].extend( config.get(section, "lib_extra_dirs", [])) # skip non existing folders and resolve full path for key in ("envLibdepsDirs", "libExtraDirs"): data[key] = [ fs.expanduser(d) if d.startswith("~") else realpath(d) for d in data[key] if isdir(d) ] return data
def GetLibSourceDirs(env): items = env.GetProjectOption("lib_extra_dirs", []) items.extend(env["LIBSOURCE_DIRS"]) return [ env.subst(fs.expanduser(item) if item.startswith("~") else item) for item in items ]
def load_state(): with app.State(AppRPC.APPSTATE_PATH, lock=True) as state: storage = state.get("storage", {}) # base data caller_id = app.get_session_var("caller_id") storage["cid"] = app.get_cid() storage["coreVersion"] = __version__ storage["coreSystype"] = util.get_systype() storage["coreCaller"] = str( caller_id).lower() if caller_id else None storage["coreSettings"] = { name: { "description": data["description"], "default_value": data["value"], "value": app.get_setting(name), } for name, data in app.DEFAULT_SETTINGS.items() } storage["homeDir"] = fs.expanduser("~") storage["projectsDir"] = storage["coreSettings"]["projects_dir"][ "value"] # skip non-existing recent projects storage["recentProjects"] = [ p for p in storage.get("recentProjects", []) if is_platformio_project(p) ] state["storage"] = storage state.modified = False # skip saving extra fields return state.as_dict()
def get_default_projects_dir(): docs_dir = join(fs.expanduser("~"), "Documents") try: assert WINDOWS import ctypes.wintypes # pylint: disable=import-outside-toplevel buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH) ctypes.windll.shell32.SHGetFolderPathW(None, 5, None, 0, buf) docs_dir = buf.value except: # pylint: disable=bare-except pass return join(docs_dir, "PlatformIO", "Projects")
def _load_tplvars(self): tpl_vars = { "config": self.config, "systype": util.get_systype(), "project_name": basename(self.project_dir), "project_dir": self.project_dir, "env_name": self.env_name, "user_home_dir": realpath(fs.expanduser("~")), "platformio_path": sys.argv[0] if isfile(sys.argv[0]) else where_is_program("platformio"), "env_path": os.getenv("PATH"), "env_pathsep": os.pathsep, } # default env configuration tpl_vars.update(self.config.items(env=self.env_name, as_dict=True)) # build data tpl_vars.update( load_project_ide_data(self.project_dir, self.env_name) or {}) with fs.cd(self.project_dir): tpl_vars.update({ "src_files": self.get_src_files(), "project_src_dir": self.config.get_optional_dir("src"), "project_lib_dir": self.config.get_optional_dir("lib"), "project_libdeps_dir": join(self.config.get_optional_dir("libdeps"), self.env_name), }) for key, value in tpl_vars.items(): if key.endswith(("_path", "_dir")): tpl_vars[key] = fs.to_unix_path(value) for key in ("src_files", "libsource_dirs"): if key not in tpl_vars: continue tpl_vars[key] = [fs.to_unix_path(inc) for inc in tpl_vars[key]] tpl_vars["to_unix_path"] = fs.to_unix_path tpl_vars["filter_includes"] = self.filter_includes return tpl_vars
def validate_path(ctx, param, value): # pylint: disable=unused-argument invalid_path = None value = list(value) for i, p in enumerate(value): if p.startswith("~"): value[i] = fs.expanduser(p) value[i] = abspath(value[i]) if not glob(value[i]): invalid_path = p break try: assert invalid_path is None return value except AssertionError: raise click.BadParameter("Found invalid path: %s" % invalid_path)
def read(self, path, parse_extra=True): if path in self._parsed: return self._parsed.append(path) try: self._parser.read(path) except ConfigParser.Error as e: raise exception.InvalidProjectConf(path, str(e)) if not parse_extra: return # load extra configs for pattern in self.get("platformio", "extra_configs", []): if pattern.startswith("~"): pattern = fs.expanduser(pattern) for item in glob.glob(pattern): self.read(item)
def get_optional_dir(self, name, exists=False): if not ProjectOptions.get("platformio.%s_dir" % name): raise ValueError("Unknown optional directory -> " + name) if name == "core": result = self._get_core_dir(exists) else: result = self.get("platformio", name + "_dir") if result is None: return None project_dir = os.getcwd() # patterns if "$PROJECT_HASH" in result: result = result.replace( "$PROJECT_HASH", "%s-%s" % ( os.path.basename(project_dir), sha1(hashlib_encode_data(project_dir)).hexdigest()[:10], ), ) if "$PROJECT_DIR" in result: result = result.replace("$PROJECT_DIR", project_dir) if "$PROJECT_CORE_DIR" in result: result = result.replace("$PROJECT_CORE_DIR", self.get_optional_dir("core")) if "$PROJECT_WORKSPACE_DIR" in result: result = result.replace("$PROJECT_WORKSPACE_DIR", self.get_optional_dir("workspace")) if result.startswith("~"): result = fs.expanduser(result) result = os.path.realpath(result) if exists and not os.path.isdir(result): os.makedirs(result) return result
"Extend main configuration with the extra configuration files" ), multiple=True, ), # Dirs ConfigPlatformioOption( group="directory", name="core_dir", description=( "PlatformIO Core location where it keeps installed development " "platforms, packages, global libraries, " "and other internal information" ), oldnames=["home_dir"], sysenvvar="PLATFORMIO_CORE_DIR", default=os.path.join(fs.expanduser("~"), ".platformio"), ), ConfigPlatformioOption( group="directory", name="globallib_dir", description=( "A library folder/storage where PlatformIO Library Dependency " "Finder (LDF) looks for global libraries" ), sysenvvar="PLATFORMIO_GLOBALLIB_DIR", default=os.path.join("$PROJECT_CORE_DIR", "lib"), ), ConfigPlatformioOption( group="directory", name="platforms_dir", description=(