Esempio n. 1
0
    def _maintain_renaimed_options(self):
        # legacy `lib_extra_dirs` in [platformio]
        if self._parser.has_section("platformio") and self._parser.has_option(
                "platformio", "lib_extra_dirs"):
            if not self._parser.has_section("env"):
                self._parser.add_section("env")
            self._parser.set(
                "env",
                "lib_extra_dirs",
                self._parser.get("platformio", "lib_extra_dirs"),
            )
            self._parser.remove_option("platformio", "lib_extra_dirs")
            self.warnings.append(
                "`lib_extra_dirs` configuration option is deprecated in "
                "section [platformio]! Please move it to global `env` section")

        renamed_options = {}
        for option in ProjectOptions.values():
            if option.oldnames:
                renamed_options.update(
                    {name: option.name
                     for name in option.oldnames})

        for section in self._parser.sections():
            scope = section.split(":", 1)[0]
            if scope not in ("platformio", "env"):
                continue
            for option in self._parser.options(section):
                if option in renamed_options:
                    self.warnings.append(
                        "`%s` configuration option in section [%s] is "
                        "deprecated and will be removed in the next release! "
                        "Please use `%s` instead" %
                        (option, section, renamed_options[option]))
                    # rename on-the-fly
                    self._parser.set(
                        section,
                        renamed_options[option],
                        self._parser.get(section, option),
                    )
                    self._parser.remove_option(section, option)
                    continue

                # unknown
                unknown_conditions = [
                    ("%s.%s" % (scope, option)) not in ProjectOptions,
                    scope != "env" or not option.startswith(
                        ("custom_", "board_")),
                ]
                if all(unknown_conditions):
                    self.warnings.append(
                        "Ignore unknown configuration option `%s` "
                        "in section [%s]" % (option, section))
        return True
Esempio n. 2
0
    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
Esempio n. 3
0
    def get(self, section, option, default=None):  # pylint: disable=too-many-branches
        value = None
        try:
            value = self.getraw(section, option)
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            pass  # handle value from system environment
        except ConfigParser.Error as e:
            raise exception.InvalidProjectConf(self.path, str(e))

        option_meta = ProjectOptions.get("%s.%s" %
                                         (section.split(":", 1)[0], option))
        if not option_meta:
            return value or default

        if option_meta.multiple:
            value = self.parse_multi_values(value)

        if option_meta.sysenvvar:
            envvar_value = os.getenv(option_meta.sysenvvar)
            if not envvar_value and option_meta.oldnames:
                for oldoption in option_meta.oldnames:
                    envvar_value = os.getenv("PLATFORMIO_" + oldoption.upper())
                    if envvar_value:
                        break
            if envvar_value and option_meta.multiple:
                value = value or []
                value.extend(self.parse_multi_values(envvar_value))
            elif envvar_value and not value:
                value = envvar_value

        # option is not specified by user
        if value is None or (option_meta.multiple and value == []
                             and option_meta.default):
            return default if default is not None else option_meta.default

        try:
            return self.cast_to(value, option_meta.type)
        except click.BadParameter as e:
            if not self.expand_interpolations:
                return value
            raise exception.ProjectOptionValueError(e.format_message(), option,
                                                    section)
Esempio n. 4
0
    def getraw(  # pylint: disable=too-many-branches
            self,
            section,
            option,
            default=MISSING):
        if not self.expand_interpolations:
            return self._parser.get(section, option)

        value = MISSING
        for sec, opt in self.walk_options(section):
            if opt == option:
                value = self._parser.get(sec, option)
                break

        option_meta = ProjectOptions.get("%s.%s" %
                                         (section.split(":", 1)[0], option))
        if not option_meta:
            if value == MISSING:
                value = (default if default != MISSING else self._parser.get(
                    section, option))
            return self._expand_interpolations(value)

        if option_meta.sysenvvar:
            envvar_value = os.getenv(option_meta.sysenvvar)
            if not envvar_value and option_meta.oldnames:
                for oldoption in option_meta.oldnames:
                    envvar_value = os.getenv("PLATFORMIO_" + oldoption.upper())
                    if envvar_value:
                        break
            if envvar_value and option_meta.multiple:
                if value == MISSING:
                    value = ""
                value += ("\n" if value else "") + envvar_value
            elif envvar_value and value == MISSING:
                value = envvar_value

        if value == MISSING:
            value = default if default != MISSING else option_meta.default
        if value == MISSING:
            return None

        return self._expand_interpolations(value)
Esempio n. 5
0
    def get(self, section, option, default=MISSING):
        value = None
        try:
            value = self.getraw(section, option, default)
        except ConfigParser.Error as e:
            raise exception.InvalidProjectConfError(self.path, str(e))

        option_meta = ProjectOptions.get("%s.%s" %
                                         (section.split(":", 1)[0], option))
        if not option_meta:
            return value

        if option_meta.multiple:
            value = self.parse_multi_values(value or [])
        try:
            return self.cast_to(value, option_meta.type)
        except click.BadParameter as e:
            if not self.expand_interpolations:
                return value
            raise exception.ProjectOptionValueError(e.format_message(), option,
                                                    section)
Esempio n. 6
0
    def options(self, section=None, env=None):
        result = []
        assert section or env
        if not section:
            section = "env:" + env

        if not self.expand_interpolations:
            return self._parser.options(section)

        for _, option in self.walk_options(section):
            if option not in result:
                result.append(option)

        # handle system environment variables
        scope = section.split(":", 1)[0]
        for option_meta in ProjectOptions.values():
            if option_meta.scope != scope or option_meta.name in result:
                continue
            if option_meta.sysenvvar and option_meta.sysenvvar in os.environ:
                result.append(option_meta.name)

        return result
Esempio n. 7
0
    def options(self, section=None, env=None):
        assert section or env
        if not section:
            section = "env:" + env
        options = self._parser.options(section)

        # handle global options from [env]
        if ((env or section.startswith("env:"))
                and self._parser.has_section("env")):
            for option in self._parser.options("env"):
                if option not in options:
                    options.append(option)

        # handle system environment variables
        scope = section.split(":", 1)[0]
        for option_meta in ProjectOptions.values():
            if option_meta.scope != scope or option_meta.name in options:
                continue
            if option_meta.sysenvvar and option_meta.sysenvvar in os.environ:
                options.append(option_meta.name)

        return options