mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-31 18:44:27 +02:00
Init new project using new ProjectConfig API
This commit is contained in:
@@ -165,5 +165,4 @@ def _copy_project_conf(build_dir, project_conf):
|
|||||||
config = ProjectConfig(project_conf, parse_extra=False)
|
config = ProjectConfig(project_conf, parse_extra=False)
|
||||||
if config.has_section("platformio"):
|
if config.has_section("platformio"):
|
||||||
config.remove_section("platformio")
|
config.remove_section("platformio")
|
||||||
with open(join(build_dir, "platformio.ini"), "wb") as fp:
|
config.save(join(build_dir, "platformio.ini"))
|
||||||
config.write(fp)
|
|
||||||
|
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
from os import getcwd, makedirs
|
from os import getcwd, makedirs
|
||||||
from os.path import isdir, isfile, join
|
from os.path import isdir, isfile, join
|
||||||
from shutil import copyfile
|
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
@@ -89,6 +88,7 @@ def cli(
|
|||||||
"platformio.ini", fg="cyan"))
|
"platformio.ini", fg="cyan"))
|
||||||
|
|
||||||
is_new_project = not util.is_platformio_project(project_dir)
|
is_new_project = not util.is_platformio_project(project_dir)
|
||||||
|
if is_new_project:
|
||||||
init_base_project(project_dir)
|
init_base_project(project_dir)
|
||||||
|
|
||||||
if board:
|
if board:
|
||||||
@@ -144,13 +144,7 @@ def get_best_envname(project_dir, boards=None):
|
|||||||
|
|
||||||
|
|
||||||
def init_base_project(project_dir):
|
def init_base_project(project_dir):
|
||||||
if util.is_platformio_project(project_dir):
|
ProjectConfig(join(project_dir, "platformio.ini")).save()
|
||||||
return
|
|
||||||
|
|
||||||
copyfile(
|
|
||||||
join(util.get_source_dir(), "projectconftpl.ini"),
|
|
||||||
join(project_dir, "platformio.ini"))
|
|
||||||
|
|
||||||
with util.cd(project_dir):
|
with util.cd(project_dir):
|
||||||
dir_to_readme = [
|
dir_to_readme = [
|
||||||
(util.get_projectsrc_dir(), None),
|
(util.get_projectsrc_dir(), None),
|
||||||
@@ -362,12 +356,9 @@ def init_cvs_ignore(project_dir):
|
|||||||
|
|
||||||
def fill_project_envs(ctx, project_dir, board_ids, project_option, env_prefix,
|
def fill_project_envs(ctx, project_dir, board_ids, project_option, env_prefix,
|
||||||
force_download):
|
force_download):
|
||||||
content = []
|
config = ProjectConfig(
|
||||||
|
join(project_dir, "platformio.ini"), parse_extra=False)
|
||||||
used_boards = []
|
used_boards = []
|
||||||
used_platforms = []
|
|
||||||
|
|
||||||
config_path = join(project_dir, "platformio.ini")
|
|
||||||
config = util.load_project_config(config_path)
|
|
||||||
for section in config.sections():
|
for section in config.sections():
|
||||||
cond = [
|
cond = [
|
||||||
section.startswith("env:"),
|
section.startswith("env:"),
|
||||||
@@ -377,12 +368,15 @@ def fill_project_envs(ctx, project_dir, board_ids, project_option, env_prefix,
|
|||||||
used_boards.append(config.get(section, "board"))
|
used_boards.append(config.get(section, "board"))
|
||||||
|
|
||||||
pm = PlatformManager()
|
pm = PlatformManager()
|
||||||
|
used_platforms = []
|
||||||
|
modified = False
|
||||||
for id_ in board_ids:
|
for id_ in board_ids:
|
||||||
board_config = pm.board_config(id_)
|
board_config = pm.board_config(id_)
|
||||||
used_platforms.append(board_config['platform'])
|
used_platforms.append(board_config['platform'])
|
||||||
if id_ in used_boards:
|
if id_ in used_boards:
|
||||||
continue
|
continue
|
||||||
used_boards.append(id_)
|
used_boards.append(id_)
|
||||||
|
modified = True
|
||||||
|
|
||||||
envopts = {"platform": board_config['platform'], "board": id_}
|
envopts = {"platform": board_config['platform'], "board": id_}
|
||||||
# find default framework for board
|
# find default framework for board
|
||||||
@@ -396,22 +390,18 @@ def fill_project_envs(ctx, project_dir, board_ids, project_option, env_prefix,
|
|||||||
_name, _value = item.split("=", 1)
|
_name, _value = item.split("=", 1)
|
||||||
envopts[_name.strip()] = _value.strip()
|
envopts[_name.strip()] = _value.strip()
|
||||||
|
|
||||||
content.append("")
|
section = "env:%s%s" % (env_prefix, id_)
|
||||||
content.append("[env:%s%s]" % (env_prefix, id_))
|
config.add_section(section)
|
||||||
for name, value in envopts.items():
|
|
||||||
content.append("%s = %s" % (name, value))
|
for option, value in envopts.items():
|
||||||
|
config.set(section, option, value)
|
||||||
|
|
||||||
if force_download and used_platforms:
|
if force_download and used_platforms:
|
||||||
_install_dependent_platforms(ctx, used_platforms)
|
_install_dependent_platforms(ctx, used_platforms)
|
||||||
|
|
||||||
if not content:
|
if modified:
|
||||||
return
|
config.save()
|
||||||
|
config.reset_instances()
|
||||||
with open(config_path, "a") as f:
|
|
||||||
content.append("")
|
|
||||||
f.write("\n".join(content))
|
|
||||||
|
|
||||||
ProjectConfig.reset_instances()
|
|
||||||
|
|
||||||
|
|
||||||
def _install_dependent_platforms(ctx, platforms):
|
def _install_dependent_platforms(ctx, platforms):
|
||||||
|
@@ -26,6 +26,18 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
import configparser as ConfigParser
|
import configparser as ConfigParser
|
||||||
|
|
||||||
|
CONFIG_HEADER = """
|
||||||
|
; PlatformIO Project Configuration File
|
||||||
|
;
|
||||||
|
; Build options: build flags, source filter
|
||||||
|
; Upload options: custom upload port, speed and extra flags
|
||||||
|
; Library options: dependencies, extra library storages
|
||||||
|
; Advanced options: extra scripting
|
||||||
|
;
|
||||||
|
; Please visit documentation for the other options and examples
|
||||||
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
"""
|
||||||
|
|
||||||
KNOWN_PLATFORMIO_OPTIONS = [
|
KNOWN_PLATFORMIO_OPTIONS = [
|
||||||
"description",
|
"description",
|
||||||
"env_default",
|
"env_default",
|
||||||
@@ -118,6 +130,7 @@ class ProjectConfig(object):
|
|||||||
|
|
||||||
VARTPL_RE = re.compile(r"\$\{([^\.\}]+)\.([^\}]+)\}")
|
VARTPL_RE = re.compile(r"\$\{([^\.\}]+)\.([^\}]+)\}")
|
||||||
|
|
||||||
|
expand_interpolations = True
|
||||||
_instances = {}
|
_instances = {}
|
||||||
_parser = None
|
_parser = None
|
||||||
_parsed = []
|
_parsed = []
|
||||||
@@ -140,8 +153,6 @@ class ProjectConfig(object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_instance(path):
|
def get_instance(path):
|
||||||
if not isfile(path):
|
|
||||||
raise exception.NotPlatformIOProject(path)
|
|
||||||
if path not in ProjectConfig._instances:
|
if path not in ProjectConfig._instances:
|
||||||
ProjectConfig._instances[path] = ProjectConfig(path)
|
ProjectConfig._instances[path] = ProjectConfig(path)
|
||||||
return ProjectConfig._instances[path]
|
return ProjectConfig._instances[path]
|
||||||
@@ -150,12 +161,12 @@ class ProjectConfig(object):
|
|||||||
def reset_instances():
|
def reset_instances():
|
||||||
ProjectConfig._instances = {}
|
ProjectConfig._instances = {}
|
||||||
|
|
||||||
def __init__(self, path, parse_extra=True):
|
def __init__(self, path, parse_extra=True, expand_interpolations=True):
|
||||||
if not isfile(path):
|
|
||||||
raise exception.NotPlatformIOProject(path)
|
|
||||||
self.path = path
|
self.path = path
|
||||||
|
self.expand_interpolations = expand_interpolations
|
||||||
self._parsed = []
|
self._parsed = []
|
||||||
self._parser = ConfigParser.ConfigParser()
|
self._parser = ConfigParser.ConfigParser()
|
||||||
|
if isfile(path):
|
||||||
self.read(path, parse_extra)
|
self.read(path, parse_extra)
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
@@ -197,6 +208,8 @@ class ProjectConfig(object):
|
|||||||
for option in self.options(section)]
|
for option in self.options(section)]
|
||||||
|
|
||||||
def get(self, section, option):
|
def get(self, section, option):
|
||||||
|
if not self.expand_interpolations:
|
||||||
|
return self._parser.get(section, option)
|
||||||
try:
|
try:
|
||||||
value = self._parser.get(section, option)
|
value = self._parser.get(section, option)
|
||||||
except ConfigParser.Error as e:
|
except ConfigParser.Error as e:
|
||||||
@@ -227,6 +240,8 @@ class ProjectConfig(object):
|
|||||||
return self.parse_multi_values(self.get("platformio", "env_default"))
|
return self.parse_multi_values(self.get("platformio", "env_default"))
|
||||||
|
|
||||||
def validate(self, envs=None):
|
def validate(self, envs=None):
|
||||||
|
if not isfile(self.path):
|
||||||
|
raise exception.NotPlatformIOProject(self.path)
|
||||||
# check envs
|
# check envs
|
||||||
known = set(self.envs())
|
known = set(self.envs())
|
||||||
if not known:
|
if not known:
|
||||||
@@ -246,7 +261,7 @@ class ProjectConfig(object):
|
|||||||
KNOWN_PLATFORMIO_OPTIONS)
|
KNOWN_PLATFORMIO_OPTIONS)
|
||||||
if unknown:
|
if unknown:
|
||||||
warnings.add(
|
warnings.add(
|
||||||
"Ignore unknown `%s` options in `[platformio]` section" %
|
"Ignore unknown `%s` options in section `[platformio]`" %
|
||||||
", ".join(unknown))
|
", ".join(unknown))
|
||||||
|
|
||||||
# check [env:*] sections
|
# check [env:*] sections
|
||||||
@@ -257,7 +272,7 @@ class ProjectConfig(object):
|
|||||||
# obsolete
|
# obsolete
|
||||||
if option in RENAMED_OPTIONS:
|
if option in RENAMED_OPTIONS:
|
||||||
warnings.add(
|
warnings.add(
|
||||||
"`%s` option in `[%s]` section is deprecated and will "
|
"`%s` option in section `[%s]` is deprecated and will "
|
||||||
"be removed in the next release! Please use `%s` "
|
"be removed in the next release! Please use `%s` "
|
||||||
"instead" % (option, section, RENAMED_OPTIONS[option]))
|
"instead" % (option, section, RENAMED_OPTIONS[option]))
|
||||||
# rename on-the-fly
|
# rename on-the-fly
|
||||||
@@ -281,3 +296,9 @@ class ProjectConfig(object):
|
|||||||
click.secho("Warning! %s" % warning, fg="yellow")
|
click.secho("Warning! %s" % warning, fg="yellow")
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def save(self, path=None):
|
||||||
|
with open(path or self.path, "wb") as fp:
|
||||||
|
fp.write(CONFIG_HEADER.strip())
|
||||||
|
fp.write("\n\n")
|
||||||
|
self._parser.write(fp)
|
||||||
|
@@ -1,9 +0,0 @@
|
|||||||
; PlatformIO Project Configuration File
|
|
||||||
;
|
|
||||||
; Build options: build flags, source filter
|
|
||||||
; Upload options: custom upload port, speed and extra flags
|
|
||||||
; Library options: dependencies, extra library storages
|
|
||||||
; Advanced options: extra scripting
|
|
||||||
;
|
|
||||||
; Please visit documentation for the other options and examples
|
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
|
@@ -179,15 +179,11 @@ def get_project_optional_dir(name, default=None):
|
|||||||
if var_name in os.environ:
|
if var_name in os.environ:
|
||||||
paths = os.getenv(var_name)
|
paths = os.getenv(var_name)
|
||||||
else:
|
else:
|
||||||
try:
|
|
||||||
config = ProjectConfig.get_instance(
|
config = ProjectConfig.get_instance(
|
||||||
join(get_project_dir(), "platformio.ini"))
|
join(get_project_dir(), "platformio.ini"))
|
||||||
if (config.has_section("platformio")
|
if (config.has_section("platformio")
|
||||||
and config.has_option("platformio", name)):
|
and config.has_option("platformio", name)):
|
||||||
paths = config.get("platformio", name)
|
paths = config.get("platformio", name)
|
||||||
except exception.NotPlatformIOProject:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if not paths:
|
if not paths:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
1
setup.py
1
setup.py
@@ -41,7 +41,6 @@ setup(
|
|||||||
packages=find_packages() + ["scripts"],
|
packages=find_packages() + ["scripts"],
|
||||||
package_data={
|
package_data={
|
||||||
"platformio": [
|
"platformio": [
|
||||||
"projectconftpl.ini",
|
|
||||||
"ide/tpls/*/.*.tpl",
|
"ide/tpls/*/.*.tpl",
|
||||||
"ide/tpls/*/*.tpl",
|
"ide/tpls/*/*.tpl",
|
||||||
"ide/tpls/*/*/*.tpl",
|
"ide/tpls/*/*/*.tpl",
|
||||||
|
Reference in New Issue
Block a user