mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 10:07:14 +02:00
Check if directory exists before fetching project info
This commit is contained in:
@ -17,7 +17,6 @@ from __future__ import absolute_import
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import time
|
import time
|
||||||
from os.path import basename, getmtime, isdir, isfile, join, realpath, sep
|
|
||||||
|
|
||||||
import jsonrpc # pylint: disable=import-error
|
import jsonrpc # pylint: disable=import-error
|
||||||
|
|
||||||
@ -38,7 +37,7 @@ class ProjectRPC(object):
|
|||||||
assert isinstance(init_kwargs, dict)
|
assert isinstance(init_kwargs, dict)
|
||||||
assert "path" in init_kwargs
|
assert "path" in init_kwargs
|
||||||
project_dir = get_project_dir()
|
project_dir = get_project_dir()
|
||||||
if isfile(init_kwargs["path"]):
|
if os.path.isfile(init_kwargs["path"]):
|
||||||
project_dir = os.path.dirname(init_kwargs["path"])
|
project_dir = os.path.dirname(init_kwargs["path"])
|
||||||
with fs.cd(project_dir):
|
with fs.cd(project_dir):
|
||||||
return getattr(ProjectConfig(**init_kwargs), method)(*args)
|
return getattr(ProjectConfig(**init_kwargs), method)(*args)
|
||||||
@ -86,7 +85,7 @@ class ProjectRPC(object):
|
|||||||
for section in config.sections():
|
for section in config.sections():
|
||||||
if not section.startswith("env:"):
|
if not section.startswith("env:"):
|
||||||
continue
|
continue
|
||||||
data["envLibdepsDirs"].append(join(libdeps_dir, section[4:]))
|
data["envLibdepsDirs"].append(os.path.join(libdeps_dir, section[4:]))
|
||||||
if config.has_option(section, "board"):
|
if config.has_option(section, "board"):
|
||||||
data["boards"].append(config.get(section, "board"))
|
data["boards"].append(config.get(section, "board"))
|
||||||
data["libExtraDirs"].extend(config.get(section, "lib_extra_dirs", []))
|
data["libExtraDirs"].extend(config.get(section, "lib_extra_dirs", []))
|
||||||
@ -94,15 +93,15 @@ class ProjectRPC(object):
|
|||||||
# skip non existing folders and resolve full path
|
# skip non existing folders and resolve full path
|
||||||
for key in ("envLibdepsDirs", "libExtraDirs"):
|
for key in ("envLibdepsDirs", "libExtraDirs"):
|
||||||
data[key] = [
|
data[key] = [
|
||||||
fs.expanduser(d) if d.startswith("~") else realpath(d)
|
fs.expanduser(d) if d.startswith("~") else os.path.realpath(d)
|
||||||
for d in data[key]
|
for d in data[key]
|
||||||
if isdir(d)
|
if os.path.isdir(d)
|
||||||
]
|
]
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def _path_to_name(path):
|
def _path_to_name(path):
|
||||||
return (sep).join(path.split(sep)[-2:])
|
return (os.path.sep).join(path.split(os.path.sep)[-2:])
|
||||||
|
|
||||||
if not project_dirs:
|
if not project_dirs:
|
||||||
project_dirs = AppRPC.load_state()["storage"]["recentProjects"]
|
project_dirs = AppRPC.load_state()["storage"]["recentProjects"]
|
||||||
@ -110,6 +109,8 @@ class ProjectRPC(object):
|
|||||||
result = []
|
result = []
|
||||||
pm = PlatformManager()
|
pm = PlatformManager()
|
||||||
for project_dir in project_dirs:
|
for project_dir in project_dirs:
|
||||||
|
if not os.path.isdir(project_dir):
|
||||||
|
continue
|
||||||
data = {}
|
data = {}
|
||||||
boards = []
|
boards = []
|
||||||
try:
|
try:
|
||||||
@ -130,12 +131,12 @@ class ProjectRPC(object):
|
|||||||
{
|
{
|
||||||
"path": project_dir,
|
"path": project_dir,
|
||||||
"name": _path_to_name(project_dir),
|
"name": _path_to_name(project_dir),
|
||||||
"modified": int(getmtime(project_dir)),
|
"modified": int(os.path.getmtime(project_dir)),
|
||||||
"boards": boards,
|
"boards": boards,
|
||||||
"description": data.get("description"),
|
"description": data.get("description"),
|
||||||
"envs": data.get("envs", []),
|
"envs": data.get("envs", []),
|
||||||
"envLibStorages": [
|
"envLibStorages": [
|
||||||
{"name": basename(d), "path": d}
|
{"name": os.path.basename(d), "path": d}
|
||||||
for d in data.get("envLibdepsDirs", [])
|
for d in data.get("envLibdepsDirs", [])
|
||||||
],
|
],
|
||||||
"extraLibStorages": [
|
"extraLibStorages": [
|
||||||
@ -153,20 +154,20 @@ class ProjectRPC(object):
|
|||||||
def get_project_examples():
|
def get_project_examples():
|
||||||
result = []
|
result = []
|
||||||
for manifest in PlatformManager().get_installed():
|
for manifest in PlatformManager().get_installed():
|
||||||
examples_dir = join(manifest["__pkg_dir"], "examples")
|
examples_dir = os.path.join(manifest["__pkg_dir"], "examples")
|
||||||
if not isdir(examples_dir):
|
if not os.path.isdir(examples_dir):
|
||||||
continue
|
continue
|
||||||
items = []
|
items = []
|
||||||
for project_dir, _, __ in os.walk(examples_dir):
|
for project_dir, _, __ in os.walk(examples_dir):
|
||||||
project_description = None
|
project_description = None
|
||||||
try:
|
try:
|
||||||
config = ProjectConfig(join(project_dir, "platformio.ini"))
|
config = ProjectConfig(os.path.join(project_dir, "platformio.ini"))
|
||||||
config.validate(silent=True)
|
config.validate(silent=True)
|
||||||
project_description = config.get("platformio", "description")
|
project_description = config.get("platformio", "description")
|
||||||
except exception.PlatformIOProjectException:
|
except exception.PlatformIOProjectException:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
path_tokens = project_dir.split(sep)
|
path_tokens = project_dir.split(os.path.sep)
|
||||||
items.append(
|
items.append(
|
||||||
{
|
{
|
||||||
"name": "/".join(
|
"name": "/".join(
|
||||||
@ -190,7 +191,7 @@ class ProjectRPC(object):
|
|||||||
def init(self, board, framework, project_dir):
|
def init(self, board, framework, project_dir):
|
||||||
assert project_dir
|
assert project_dir
|
||||||
state = AppRPC.load_state()
|
state = AppRPC.load_state()
|
||||||
if not isdir(project_dir):
|
if not os.path.isdir(project_dir):
|
||||||
os.makedirs(project_dir)
|
os.makedirs(project_dir)
|
||||||
args = ["init", "--board", board]
|
args = ["init", "--board", board]
|
||||||
if framework:
|
if framework:
|
||||||
@ -243,10 +244,10 @@ class ProjectRPC(object):
|
|||||||
with fs.cd(project_dir):
|
with fs.cd(project_dir):
|
||||||
config = ProjectConfig()
|
config = ProjectConfig()
|
||||||
src_dir = config.get_optional_dir("src")
|
src_dir = config.get_optional_dir("src")
|
||||||
main_path = join(src_dir, "main.cpp")
|
main_path = os.path.join(src_dir, "main.cpp")
|
||||||
if isfile(main_path):
|
if os.path.isfile(main_path):
|
||||||
return project_dir
|
return project_dir
|
||||||
if not isdir(src_dir):
|
if not os.path.isdir(src_dir):
|
||||||
os.makedirs(src_dir)
|
os.makedirs(src_dir)
|
||||||
fs.write_file_contents(main_path, main_content.strip())
|
fs.write_file_contents(main_path, main_content.strip())
|
||||||
return project_dir
|
return project_dir
|
||||||
@ -261,10 +262,10 @@ class ProjectRPC(object):
|
|||||||
|
|
||||||
is_arduino_project = any(
|
is_arduino_project = any(
|
||||||
[
|
[
|
||||||
isfile(
|
os.path.isfile(
|
||||||
join(
|
os.path.join(
|
||||||
arduino_project_dir,
|
arduino_project_dir,
|
||||||
"%s.%s" % (basename(arduino_project_dir), ext),
|
"%s.%s" % (os.path.basename(arduino_project_dir), ext),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
for ext in ("ino", "pde")
|
for ext in ("ino", "pde")
|
||||||
@ -276,10 +277,10 @@ class ProjectRPC(object):
|
|||||||
)
|
)
|
||||||
|
|
||||||
state = AppRPC.load_state()
|
state = AppRPC.load_state()
|
||||||
project_dir = join(
|
project_dir = os.path.join(
|
||||||
state["storage"]["projectsDir"], time.strftime("%y%m%d-%H%M%S-") + board
|
state["storage"]["projectsDir"], time.strftime("%y%m%d-%H%M%S-") + board
|
||||||
)
|
)
|
||||||
if not isdir(project_dir):
|
if not os.path.isdir(project_dir):
|
||||||
os.makedirs(project_dir)
|
os.makedirs(project_dir)
|
||||||
args = ["init", "--board", board]
|
args = ["init", "--board", board]
|
||||||
args.extend(["--project-option", "framework = arduino"])
|
args.extend(["--project-option", "framework = arduino"])
|
||||||
@ -301,7 +302,7 @@ class ProjectRPC(object):
|
|||||||
with fs.cd(project_dir):
|
with fs.cd(project_dir):
|
||||||
config = ProjectConfig()
|
config = ProjectConfig()
|
||||||
src_dir = config.get_optional_dir("src")
|
src_dir = config.get_optional_dir("src")
|
||||||
if isdir(src_dir):
|
if os.path.isdir(src_dir):
|
||||||
fs.rmtree(src_dir)
|
fs.rmtree(src_dir)
|
||||||
shutil.copytree(arduino_project_dir, src_dir)
|
shutil.copytree(arduino_project_dir, src_dir)
|
||||||
return project_dir
|
return project_dir
|
||||||
@ -312,9 +313,9 @@ class ProjectRPC(object):
|
|||||||
raise jsonrpc.exceptions.JSONRPCDispatchException(
|
raise jsonrpc.exceptions.JSONRPCDispatchException(
|
||||||
code=4001, message="Not an PlatformIO project: %s" % project_dir
|
code=4001, message="Not an PlatformIO project: %s" % project_dir
|
||||||
)
|
)
|
||||||
new_project_dir = join(
|
new_project_dir = os.path.join(
|
||||||
AppRPC.load_state()["storage"]["projectsDir"],
|
AppRPC.load_state()["storage"]["projectsDir"],
|
||||||
time.strftime("%y%m%d-%H%M%S-") + basename(project_dir),
|
time.strftime("%y%m%d-%H%M%S-") + os.path.basename(project_dir),
|
||||||
)
|
)
|
||||||
shutil.copytree(project_dir, new_project_dir)
|
shutil.copytree(project_dir, new_project_dir)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user