Пример #1
0
    def _load_tplvars(self):
        tpl_vars = {"env_name": self.env_name}
        # default env configuration
        tpl_vars.update(
            ProjectConfig.get_instance(join(
                self.project_dir, "platformio.ini")).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 util.cd(self.project_dir):
            tpl_vars.update({
                "project_name": basename(self.project_dir),
                "src_files": self.get_src_files(),
                "user_home_dir": abspath(expanduser("~")),
                "project_dir": self.project_dir,
                "project_src_dir": get_project_src_dir(),
                "project_lib_dir": get_project_lib_dir(),
                "project_libdeps_dir": join(
                    get_project_libdeps_dir(), self.env_name),
                "systype": util.get_systype(),
                "platformio_path": self._fix_os_path(
                    sys.argv[0] if isfile(sys.argv[0])
                    else where_is_program("platformio")),
                "env_pathsep": os.pathsep,
                "env_path": self._fix_os_path(os.getenv("PATH"))
            })  # yapf: disable
        return tpl_vars
Пример #2
0
    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'] = 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
            return state.as_dict()
Пример #3
0
    def _demangle_report(self, output_file):
        converter_tool = os.path.join(
            get_core_package_dir("tool-pvs-studio"),
            "HtmlGenerator" if "windows" in util.get_systype() else
            os.path.join("bin", "plog-converter"),
        )

        cmd = (
            converter_tool,
            "-t",
            "xml",
            output_file,
            "-m",
            "cwe",
            "-m",
            "misra",
            "-a",
            # Enable all possible analyzers and defect levels
            "GA:1,2,3;64:1,2,3;OP:1,2,3;CS:1,2,3;MISRA:1,2,3",
            "--cerr",
        )

        result = proc.exec_command(cmd)
        if result["returncode"] != 0:
            click.echo(result["err"])
            self._bad_input = True

        return result["err"]
Пример #4
0
def ensure_udev_rules():
    from platformio.util import get_systype  # pylint: disable=import-outside-toplevel

    def _rules_to_set(rules_path):
        return set(
            l.strip()
            for l in get_file_contents(rules_path).split("\n")
            if l.strip() and not l.startswith("#")
        )

    if "linux" not in get_systype():
        return None
    installed_rules = [
        "/etc/udev/rules.d/99-platformio-udev.rules",
        "/lib/udev/rules.d/99-platformio-udev.rules",
    ]
    if not any(os.path.isfile(p) for p in installed_rules):
        raise exception.MissedUdevRules

    origin_path = os.path.abspath(
        os.path.join(get_source_dir(), "..", "scripts", "99-platformio-udev.rules")
    )
    if not os.path.isfile(origin_path):
        return None

    origin_rules = _rules_to_set(origin_path)
    for rules_path in installed_rules:
        if not os.path.isfile(rules_path):
            continue
        current_rules = _rules_to_set(rules_path)
        if not origin_rules <= current_rules:
            raise exception.OutdatedUdevRules(rules_path)

    return True
Пример #5
0
def prepare_ulp_env_vars(env):
    ulp_env.PrependENVPath("IDF_PATH", FRAMEWORK_DIR)

    additional_packages = [
        os.path.join(
            platform.get_package_dir(
                "toolchain-xtensa-esp%s"
                % ("32s2" if idf_variant == "esp32s2" else "32")
            ),
            "bin",
        ),
        os.path.join(
            platform.get_package_dir("toolchain-%sulp" % idf_variant),
            "bin",
        ),
        platform.get_package_dir("tool-ninja"),
        os.path.join(platform.get_package_dir("tool-cmake"), "bin"),
        os.path.dirname(where_is_program("python")),
    ]

    if "windows" in get_systype():
        additional_packages.append(platform.get_package_dir("tool-mconf"))

    for package in additional_packages:
        ulp_env.PrependENVPath("PATH", package)
Пример #6
0
def pioplus_call(args, **kwargs):
    if "windows" in util.get_systype() and sys.version_info < (2, 7, 6):
        raise exception.PlatformioException(
            "PlatformIO Core Plus v%s does not run under Python version %s.\n"
            "Minimum supported version is 2.7.6, please upgrade Python.\n"
            "Python 3 is not yet supported.\n" %
            (__version__, sys.version.split()[0]))

    pioplus_path = join(get_core_package_dir("tool-pioplus"), "pioplus")
    os.environ['PYTHONEXEPATH'] = util.get_pythonexe_path()
    os.environ['PYTHONPYSITEDIR'] = get_core_package_dir("pysite-pioplus")
    util.copy_pythonpath_to_osenv()
    code = subprocess.call([pioplus_path] + args, **kwargs)

    # handle remote update request
    if code == 13:
        count_attr = "_update_count"
        try:
            count_value = getattr(pioplus_call, count_attr)
        except AttributeError:
            count_value = 0
            setattr(pioplus_call, count_attr, 1)
        count_value += 1
        setattr(pioplus_call, count_attr, count_value)
        if count_value < PIOPLUS_AUTO_UPDATES_MAX:
            update_core_packages()
            return pioplus_call(args, **kwargs)

    # handle reload request
    elif code == 14:
        return pioplus_call(args, **kwargs)

    if code != 0:
        raise exception.ReturnErrorCode(1)
Пример #7
0
def pioplus_call(args, **kwargs):
    if "windows" in util.get_systype() and sys.version_info < (2, 7, 6):
        raise exception.PlatformioException(
            "PlatformIO Core Plus v%s does not run under Python version %s.\n"
            "Minimum supported version is 2.7.6, please upgrade Python.\n"
            "Python 3 is not yet supported.\n" % (__version__,
                                                  sys.version.split()[0]))

    pioplus_path = join(get_core_package_dir("tool-pioplus"), "pioplus")
    os.environ['PYTHONEXEPATH'] = util.get_pythonexe_path()
    os.environ['PYTHONPYSITEDIR'] = get_core_package_dir("pysite-pioplus")
    util.copy_pythonpath_to_osenv()
    code = subprocess.call([pioplus_path] + args, **kwargs)

    # handle remote update request
    if code == 13:
        count_attr = "_update_count"
        try:
            count_value = getattr(pioplus_call, count_attr)
        except AttributeError:
            count_value = 0
            setattr(pioplus_call, count_attr, 1)
        count_value += 1
        setattr(pioplus_call, count_attr, count_value)
        if count_value < PIOPLUS_AUTO_UPDATES_MAX:
            update_core_packages()
            return pioplus_call(args, **kwargs)

    # handle reload request
    elif code == 14:
        return pioplus_call(args, **kwargs)

    if code != 0:
        raise exception.ReturnErrorCode(1)
Пример #8
0
    def configure_default_packages(self, variables, targets):
        board = variables.get("board")
        frameworks = variables.get("pioframework", [])
        if "mbed" in frameworks:
            deprecated_boards_file = os.path.join(
                self.get_dir(), "misc", "mbed_deprecated_boards.json")
            if os.path.isfile(deprecated_boards_file):
                with open(deprecated_boards_file) as fp:
                    if board in json.load(fp):
                        self.packages["framework-mbed"][
                            "version"] = "~6.51504.0"
            self.packages["toolchain-gccarmnoneeabi"]["version"] = "~1.90201.0"

        if "zephyr" in frameworks:
            for p in self.packages:
                if p.startswith("framework-zephyr-") or p in ("tool-cmake",
                                                              "tool-dtc",
                                                              "tool-ninja"):
                    self.packages[p]["optional"] = False
            self.packages["toolchain-gccarmnoneeabi"]["version"] = "~1.80201.0"
            if "windows" not in get_systype():
                self.packages["tool-gperf"]["optional"] = False

        return PlatformBase.configure_default_packages(self, variables,
                                                       targets)
Пример #9
0
def build_contrib_pysite_deps(target_dir):
    if os.path.isdir(target_dir):
        util.rmtree_(target_dir)
    os.makedirs(target_dir)
    with open(os.path.join(target_dir, "package.json"), "w") as fp:
        json.dump(
            dict(
                name="contrib-pysite",
                version="2.%d%d.0" %
                (sys.version_info.major, sys.version_info.minor),
                system=util.get_systype(),
            ),
            fp,
        )

    pythonexe = get_pythonexe_path()
    for dep in get_contrib_pysite_deps():
        subprocess.call([
            pythonexe,
            "-m",
            "pip",
            "install",
            "--no-cache-dir",
            "--no-compile",
            "-t",
            target_dir,
            dep,
        ])
    return True
Пример #10
0
    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()
Пример #11
0
    def _gather_tplvars(self):
        src_files = self.get_src_files()
        main_src_file = self.get_main_src_file(src_files)

        if not main_src_file and self.ide == "clion":
            click.secho(
                "Warning! Can not find main source file (*.c, *.cpp). So, "
                "code auto-completion is disabled. Please add source files "
                "to `src` directory and re-initialize project or edit "
                "`CMakeLists.txt` file manually (`add_executable` command).",
                fg="yellow",
            )

        self._tplvars.update(self.get_project_env())
        self._tplvars.update(self.get_project_build_data())
        self._tplvars.update(
            {
                "project_name": self.get_project_name(),
                "src_files": src_files,
                "main_src_file": main_src_file,
                "user_home_dir": abspath(expanduser("~")),
                "project_dir": self.project_dir,
                "systype": util.get_systype(),
                "platformio_path": self._fix_os_path(util.where_is_program("platformio")),
                "env_pathsep": os.pathsep,
                "env_path": self._fix_os_path(os.getenv("PATH")),
            }
        )
Пример #12
0
    def install(self,
                name,
                requirements=None,
                silent=False,
                trigger_event=True,
                force=False):
        name, requirements, url = self.parse_pkg_uri(name, requirements)
        package_dir = self.get_package_dir(name, requirements, url)

        # avoid circle dependencies
        if not self.INSTALL_HISTORY:
            self.INSTALL_HISTORY = []
        history_key = "%s-%s-%s" % (name, requirements or "", url or "")
        if history_key in self.INSTALL_HISTORY:
            return package_dir
        self.INSTALL_HISTORY.append(history_key)

        if package_dir and force:
            self.uninstall(package_dir)
            package_dir = None

        if not package_dir or not silent:
            msg = "Installing " + click.style(name, fg="cyan")
            if requirements:
                msg += " @ " + requirements
            self.print_message(msg)
        if package_dir:
            if not silent:
                click.secho("{name} @ {version} is already installed".format(
                    **self.load_manifest(package_dir)),
                            fg="yellow")
            return package_dir

        if url:
            pkg_dir = self._install_from_url(name,
                                             url,
                                             requirements,
                                             track=True)
        else:
            pkg_dir = self._install_from_piorepo(name, requirements)

        if not pkg_dir or not self.manifest_exists(pkg_dir):
            raise exception.PackageInstallError(name, requirements or "*",
                                                util.get_systype())

        manifest = self.load_manifest(pkg_dir)
        assert manifest

        if trigger_event:
            telemetry.on_event(category=self.__class__.__name__,
                               action="Install",
                               label=manifest['name'])

        if not silent:
            click.secho(
                "{name} @ {version} has been successfully installed!".format(
                    **manifest),
                fg="green")

        return pkg_dir
Пример #13
0
    def configure_default_packages(self, variables, targets):
        if not variables.get("board"):
            return PlatformBase.configure_default_packages(
                self, variables, targets)

        board_config = self.board_config(variables.get("board"))
        mcu = variables.get("board_build.mcu",
                            board_config.get("build.mcu", "esp32"))
        frameworks = variables.get("pioframework", [])
        if "buildfs" in targets:
            self.packages["tool-mkspiffs"]["optional"] = False
        if variables.get("upload_protocol"):
            self.packages["tool-openocd-esp32"]["optional"] = False
        if os.path.isdir("ulp"):
            self.packages["toolchain-esp32ulp"]["optional"] = False
        if "espidf" in frameworks:
            for p in self.packages:
                if p in ("tool-cmake", "tool-ninja", "toolchain-%sulp" % mcu):
                    self.packages[p]["optional"] = False
                elif p in ("tool-mconf",
                           "tool-idf") and "windows" in get_systype():
                    self.packages[p]["optional"] = False
            self.packages["toolchain-xtensa32"]["version"] = "~2.80400.0"
            if "arduino" in frameworks:
                # Arduino component is not compatible with ESP-IDF >=4.1
                self.packages["framework-espidf"]["version"] = "~3.40001.0"

        build_core = variables.get("board_build.core",
                                   board_config.get("build.core",
                                                    "arduino")).lower()

        return PlatformBase.configure_default_packages(self, variables,
                                                       targets)
Пример #14
0
    def _gather_tplvars(self):
        src_files = self.get_src_files()
        main_src_file = self.get_main_src_file(src_files)

        if not main_src_file and self.ide == "clion":
            click.secho(
                "Warning! Can not find main source file (*.c, *.cpp). So, "
                "code auto-completion is disabled. Please add source files "
                "to `src` directory and re-initialize project or edit "
                "`CMakeLists.txt` file manually (`add_executable` command).",
                fg="yellow")

        self._tplvars.update(self.get_project_env())
        self._tplvars.update(self.get_project_build_data())
        self._tplvars.update({
            "project_name":
            self.get_project_name(),
            "src_files":
            src_files,
            "main_src_file":
            main_src_file,
            "user_home_dir":
            abspath(expanduser("~")),
            "project_dir":
            self.project_dir,
            "systype":
            util.get_systype(),
            "platformio_path":
            self._fix_os_path(util.where_is_program("platformio")),
            "env_pathsep":
            os.pathsep,
            "env_path":
            self._fix_os_path(os.getenv("PATH"))
        })
Пример #15
0
def get_contrib_pysite_deps():
    sys_type = util.get_systype()
    py_version = "%d%d" % (sys.version_info.major, sys.version_info.minor)

    twisted_version = "19.10.0" if PY2 else "20.3.0"
    result = [
        "twisted == %s" % twisted_version,
    ]

    # twisted[tls], see setup.py for %twisted_version%
    result.extend(
        ["pyopenssl >= 16.0.0", "service_identity >= 18.1.0", "idna >= 0.6, != 2.3"]
    )

    if "windows" in sys_type:
        result.append("pypiwin32 == 223")
        # workaround for twisted wheels
        twisted_wheel = (
            "https://download.lfd.uci.edu/pythonlibs/x2tqcw5k/Twisted-"
            "%s-cp%s-cp%s-win%s.whl"
            % (
                twisted_version,
                py_version,
                py_version,
                "_amd64" if "amd64" in sys_type else "32",
            )
        )
        result[0] = twisted_wheel
    return result
Пример #16
0
    def configure_default_packages(self, variables, targets):
        if "erase" in targets:
            self.packages["tool-nrfjprog"]["optional"] = False
        if "zephyr" in variables.get("pioframework", []):
            for p in self.packages:
                if p in ("tool-cmake", "tool-dtc", "tool-ninja"):
                    self.packages[p]["optional"] = False
            self.packages["toolchain-gccarmnoneeabi"]["version"] = "~1.80201.0"
            if "windows" not in get_systype():
                self.packages["tool-gperf"]["optional"] = False

        # configure J-LINK tool
        jlink_conds = [
            "jlink" in variables.get(option, "")
            for option in ("upload_protocol", "debug_tool")
        ]
        if variables.get("board"):
            board_config = self.board_config(variables.get("board"))
            jlink_conds.extend([
                "jlink" in board_config.get(key, "")
                for key in ("debug.default_tools", "upload.protocol")
            ])
        jlink_pkgname = "tool-jlink"
        if not any(jlink_conds) and jlink_pkgname in self.packages:
            del self.packages[jlink_pkgname]

        return PlatformBase.configure_default_packages(self, variables,
                                                       targets)
Пример #17
0
def test_get_installed(isolated_pio_core, tmpdir_factory):
    storage_dir = tmpdir_factory.mktemp("storage")
    pm = ToolPackageManager(str(storage_dir))

    # VCS package
    (storage_dir.join("pkg-vcs").mkdir().join(".git").mkdir().join(
        ".piopm").write("""
{
  "name": "pkg-via-vcs",
  "spec": {
    "id": null,
    "name": "pkg-via-vcs",
    "owner": null,
    "requirements": null,
    "url": "git+https://github.com/username/repo.git"
  },
  "type": "tool",
  "version": "0.0.0+sha.1ea4d5e"
}
"""))

    # package without metadata file
    (storage_dir.join("[email protected]").mkdir().join("package.json").write(
        '{"name": "foo", "version": "3.4.5"}'))

    # package with metadata file
    foo_dir = storage_dir.join("foo").mkdir()
    foo_dir.join("package.json").write('{"name": "foo", "version": "3.6.0"}')
    foo_dir.join(".piopm").write("""
{
  "name": "foo",
  "spec": {
    "name": "foo",
    "owner": null,
    "requirements": "^3"
  },
  "type": "tool",
  "version": "3.6.0"
}
""")

    # test "system"
    storage_dir.join("pkg-incompatible-system").mkdir(
    ).join("package.json").write(
        '{"name": "check-system", "version": "4.0.0", "system": ["unknown"]}')
    storage_dir.join("pkg-compatible-system").mkdir().join(
        "package.json").write(
            '{"name": "check-system", "version": "3.0.0", "system": "%s"}' %
            util.get_systype())

    # invalid package
    storage_dir.join("invalid-package").mkdir().join("library.json").write(
        '{"name": "SomeLib", "version": "4.0.0"}')

    installed = pm.get_installed()
    assert len(installed) == 4
    assert set(["pkg-via-vcs", "foo",
                "check-system"]) == set(p.metadata.name for p in installed)
    assert str(pm.get_package("foo").metadata.version) == "3.6.0"
    assert str(pm.get_package("check-system").metadata.version) == "3.0.0"
Пример #18
0
    def install(self,
                name,
                requirements=None,
                silent=False,
                trigger_event=True,
                force=False):
        name, requirements, url = self.parse_pkg_uri(name, requirements)
        package_dir = self.get_package_dir(name, requirements, url)

        # avoid circle dependencies
        if not self.INSTALL_HISTORY:
            self.INSTALL_HISTORY = []
        history_key = "%s-%s-%s" % (name, requirements or "", url or "")
        if history_key in self.INSTALL_HISTORY:
            return package_dir
        self.INSTALL_HISTORY.append(history_key)

        if package_dir and force:
            self.uninstall(package_dir)
            package_dir = None

        if not package_dir or not silent:
            msg = "Installing " + click.style(name, fg="cyan")
            if requirements:
                msg += " @ " + requirements
            self.print_message(msg)
        if package_dir:
            if not silent:
                click.secho(
                    "{name} @ {version} is already installed".format(
                        **self.load_manifest(package_dir)),
                    fg="yellow")
            return package_dir

        if url:
            pkg_dir = self._install_from_url(
                name, url, requirements, track=True)
        else:
            pkg_dir = self._install_from_piorepo(name, requirements)

        if not pkg_dir or not self.manifest_exists(pkg_dir):
            raise exception.PackageInstallError(name, requirements or "*",
                                                util.get_systype())

        manifest = self.load_manifest(pkg_dir)
        assert manifest

        if trigger_event:
            telemetry.on_event(
                category=self.__class__.__name__,
                action="Install",
                label=manifest['name'])

        if not silent:
            click.secho(
                "{name} @ {version} has been successfully installed!".format(
                    **manifest),
                fg="green")

        return pkg_dir
Пример #19
0
def test_install_from_registry(isolated_pio_core, tmpdir_factory):
    # Libraries
    lm = LibraryPackageManager(str(tmpdir_factory.mktemp("lib-storage")))
    # library with dependencies
    lm.install("AsyncMqttClient-esphome @ 0.8.4", silent=True)
    assert len(lm.get_installed()) == 3
    pkg = lm.get_package("AsyncTCP-esphome")
    assert pkg.metadata.spec.owner == "ottowinter"
    assert not lm.get_package("non-existing-package")
    # mbed library
    assert lm.install("wolfSSL", silent=True)
    assert len(lm.get_installed()) == 4
    # case sensitive author name
    assert lm.install("DallasTemperature", silent=True)
    assert lm.get_package("OneWire").metadata.version.major >= 2
    assert len(lm.get_installed()) == 6

    # test conflicted names
    lm = LibraryPackageManager(str(tmpdir_factory.mktemp("conflicted-storage")))
    lm.install("[email protected]", silent=True)
    lm.install("[email protected]", silent=True)
    assert len(lm.get_installed()) == 2

    # Tools
    tm = ToolPackageManager(str(tmpdir_factory.mktemp("tool-storage")))
    pkg = tm.install("platformio/tool-stlink @ ~1.10400.0", silent=True)
    manifest = tm.load_manifest(pkg)
    assert tm.is_system_compatible(manifest.get("system"))
    assert util.get_systype() in manifest.get("system", [])

    # Test unknown
    with pytest.raises(UnknownPackageError):
        tm.install("unknown-package-tool @ 9.1.1", silent=True)
    with pytest.raises(UnknownPackageError):
        tm.install("owner/unknown-package-tool", silent=True)
Пример #20
0
def populate_zephyr_env_vars(zephyr_env, board_config):
    toolchain_variant = "UNKNOWN"
    arch = get_board_architecture(board_config)
    if arch == "arm":
        toolchain_variant = "gnuarmemb"
        zephyr_env["GNUARMEMB_TOOLCHAIN_PATH"] = platform.get_package_dir(
            "toolchain-gccarmnoneeabi")
    elif arch == "riscv":
        toolchain_variant = "cross-compile"
        zephyr_env["CROSS_COMPILE"] = os.path.join(
            platform.get_package_dir("toolchain-riscv"), "bin",
            "riscv64-unknown-elf-")
    elif arch == "xtensa32":
        toolchain_variant = "espressif"
        zephyr_env["ESPRESSIF_TOOLCHAIN_PATH"] = platform.get_package_dir(
            "toolchain-xtensa32")

    zephyr_env["ZEPHYR_TOOLCHAIN_VARIANT"] = toolchain_variant
    zephyr_env["ZEPHYR_BASE"] = FRAMEWORK_DIR

    additional_packages = [
        platform.get_package_dir("tool-dtc"),
        platform.get_package_dir("tool-ninja"),
    ]

    if "windows" not in get_systype():
        additional_packages.append(platform.get_package_dir("tool-gperf"))

    zephyr_env["PATH"] = os.pathsep.join(additional_packages)
Пример #21
0
 def _prefill_custom_data(self):
     self['cd1'] = util.get_systype()
     self['cd2'] = "Python/%s %s" % (platform.python_version(),
                                     platform.platform())
     self['cd4'] = 1 if not util.is_ci() else 0
     if app.get_session_var("caller_id"):
         self['cd5'] = str(app.get_session_var("caller_id")).lower()
Пример #22
0
    def install(
        self, name, requirements=None, silent=False, trigger_event=True, interactive=False
    ):  # pylint: disable=unused-argument
        name, requirements, url = self.parse_pkg_name(name, requirements)
        package_dir = self.get_package_dir(name, requirements, url)

        if not package_dir or not silent:
            msg = "Installing " + click.style(name, fg="cyan")
            if requirements:
                msg += " @ " + requirements
            self.print_message(msg)
        if package_dir:
            if not silent:
                click.secho(
                    "{name} @ {version} is already installed".format(**self.load_manifest(package_dir)), fg="yellow"
                )
            return package_dir

        if url:
            pkg_dir = self._install_from_url(name, url, requirements)
        else:
            pkg_dir = self._install_from_piorepo(name, requirements)
        if not pkg_dir or not self.manifest_exists(pkg_dir):
            raise exception.PackageInstallError(name, requirements or "*", util.get_systype())

        self.reset_cache()
        manifest = self.load_manifest(pkg_dir)

        if trigger_event:
            telemetry.on_event(category=self.__class__.__name__, action="Install", label=manifest["name"])

        click.secho("{name} @ {version} has been successfully installed!".format(**manifest), fg="green")

        return pkg_dir
Пример #23
0
def pytest_generate_tests(metafunc):
    if "pioproject_dir" not in metafunc.fixturenames:
        return
    examples_dirs = []

    # repo examples
    examples_dirs.append(normpath(join(dirname(__file__), "..", "examples")))

    # dev/platforms
    for manifest in PlatformManager().get_installed():
        p = PlatformFactory.newPlatform(manifest['__pkg_dir'])
        if not p.is_embedded():
            continue
        # issue with "version `CXXABI_1.3.9' not found (required by sdcc)"
        if "linux" in util.get_systype() and p.name in ("intel_mcs51",
                                                        "ststm8"):
            continue
        examples_dir = join(p.get_dir(), "examples")
        assert isdir(examples_dir)
        examples_dirs.append(examples_dir)

    project_dirs = []
    for examples_dir in examples_dirs:
        platform_examples = []
        for root, _, files in walk(examples_dir):
            if "platformio.ini" not in files or ".skiptest" in files:
                continue
            platform_examples.append(root)

        # test random 3 examples
        random.shuffle(platform_examples)
        project_dirs.extend(platform_examples[:3])
    project_dirs.sort()
    metafunc.parametrize("pioproject_dir", project_dirs)
Пример #24
0
    def __init__(self, *args, **kwargs):
        self._tmp_dir = tempfile.mkdtemp(prefix="piocheck")
        self._tmp_preprocessed_file = self._generate_tmp_file_path() + ".i"
        self._tmp_output_file = self._generate_tmp_file_path() + ".pvs"
        self._tmp_cfg_file = self._generate_tmp_file_path() + ".cfg"
        self._tmp_cmd_file = self._generate_tmp_file_path() + ".cmd"
        self.tool_path = os.path.join(
            get_core_package_dir("tool-pvs-studio"),
            "x64" if "windows" in util.get_systype() else "bin",
            "pvs-studio",
        )
        super(PvsStudioCheckTool, self).__init__(*args, **kwargs)

        with open(self._tmp_cfg_file, "w") as fp:
            fp.write(
                "exclude-path = "
                + self.config.get_optional_dir("packages").replace("\\", "/")
            )

        with open(self._tmp_cmd_file, "w") as fp:
            fp.write(
                " ".join(
                    ['-I"%s"' % inc.replace("\\", "/") for inc in self.cpp_includes]
                )
            )
Пример #25
0
    def _install_from_piorepo(self, name, requirements):
        pkg_dir = None
        pkgdata = None
        versions = None
        last_exc = None
        for versions in PackageRepoIterator(name, self.repositories):
            pkgdata = self.max_satisfying_repo_version(versions, requirements)
            if not pkgdata:
                continue
            try:
                pkg_dir = self._install_from_url(name, pkgdata["url"],
                                                 requirements,
                                                 pkgdata.get("sha1"))
                break
            except Exception as e:  # pylint: disable=broad-except
                last_exc = e
                click.secho("Warning! Package Mirror: %s" % e, fg="yellow")
                click.secho("Looking for another mirror...", fg="yellow")

        if versions is None:
            util.internet_on(raise_exception=True)
            raise exception.UnknownPackage(name +
                                           (". Error -> %s" %
                                            last_exc if last_exc else ""))
        if not pkgdata:
            raise exception.UndefinedPackageVersion(requirements or "latest",
                                                    util.get_systype())
        return pkg_dir
Пример #26
0
    def configure_default_packages(self, variables, targets):
        if "buildfs" in targets:
            self.packages['tool-mkspiffs']['optional'] = False
        if variables.get("upload_protocol"):
            self.packages['tool-openocd-esp32']['optional'] = False
        if isdir("ulp"):
            self.packages['toolchain-esp32ulp']['optional'] = False
        if "espidf" in variables.get("pioframework", []):
            for p in self.packages:
                if p in ("tool-cmake", "tool-ninja", "toolchain-esp32ulp"):
                    self.packages[p]['optional'] = False
                elif p in ("tool-mconf",
                           "tool-idf") and "windows" in get_systype():
                    self.packages[p]['optional'] = False
            self.packages['toolchain-xtensa32']['version'] = "~2.80200.0"

        build_core = variables.get(
            "board_build.core",
            self.board_config(variables.get("board")).get(
                "build.core", "arduino")).lower()
        if build_core == "mbcwb":
            self.packages['framework-arduinoespressif32']['optional'] = True
            self.packages['framework-arduino-mbcwb']['optional'] = False
            self.packages['tool-mbctool']['type'] = "uploader"
            self.packages['tool-mbctool']['optional'] = False

        return PlatformBase.configure_default_packages(self, variables,
                                                       targets)
Пример #27
0
    def install(
        self, name, requirements=None, silent=False, after_update=False, force=False
    ):  # pylint: disable=unused-argument
        pkg_dir = None
        # interprocess lock
        with LockFile(self.package_dir):
            self.cache_reset()

            name, requirements, url = self.parse_pkg_uri(name, requirements)
            package_dir = self.get_package_dir(name, requirements, url)

            # avoid circle dependencies
            if not self.INSTALL_HISTORY:
                self.INSTALL_HISTORY = []
            history_key = "%s-%s-%s" % (name, requirements or "", url or "")
            if history_key in self.INSTALL_HISTORY:
                return package_dir
            self.INSTALL_HISTORY.append(history_key)

            if package_dir and force:
                self.uninstall(package_dir)
                package_dir = None

            if not package_dir or not silent:
                msg = "Installing " + click.style(name, fg="cyan")
                if requirements:
                    msg += " @ " + requirements
                self.print_message(msg)
            if package_dir:
                if not silent:
                    click.secho(
                        "{name} @ {version} is already installed".format(
                            **self.load_manifest(package_dir)
                        ),
                        fg="yellow",
                    )
                return package_dir

            if url:
                pkg_dir = self._install_from_url(name, url, requirements, track=True)
            else:
                pkg_dir = self._install_from_piorepo(name, requirements)

            if not pkg_dir or not self.manifest_exists(pkg_dir):
                raise exception.PackageInstallError(
                    name, requirements or "*", util.get_systype()
                )

            manifest = self.load_manifest(pkg_dir)
            assert manifest

            click.secho(
                "{name} @ {version} has been successfully installed!".format(
                    **manifest
                ),
                fg="green",
            )

        return pkg_dir
Пример #28
0
def LoadPioPlatform(env, variables):
    p = env.PioPlatform()
    installed_packages = p.get_installed_packages()

    # Ensure real platform name
    env['PIOPLATFORM'] = p.name

    # Add toolchains and uploaders to $PATH and $*_LIBRARY_PATH
    systype = util.get_systype()
    for name in installed_packages:
        type_ = p.get_package_type(name)
        if type_ not in ("toolchain", "uploader", "debugger"):
            continue
        pkg_dir = p.get_package_dir(name)
        env.PrependENVPath(
            "PATH",
            join(pkg_dir, "bin") if isdir(join(pkg_dir, "bin")) else pkg_dir)
        if ("windows" not in systype and isdir(join(pkg_dir, "lib"))
                and type_ != "toolchain"):
            env.PrependENVPath(
                "DYLD_LIBRARY_PATH"
                if "darwin" in systype else "LD_LIBRARY_PATH",
                join(pkg_dir, "lib"))

    # Platform specific LD Scripts
    if isdir(join(p.get_dir(), "ldscripts")):
        env.Prepend(LIBPATH=[join(p.get_dir(), "ldscripts")])

    if "BOARD" not in env:
        # handle _MCU and _F_CPU variables for AVR native
        for key, value in variables.UnknownVariables().items():
            if not key.startswith("BOARD_"):
                continue
            env.Replace(**{
                key.upper().replace("BUILD.", ""):
                base64.b64decode(value)
            })
        return

    # update board manifest with a custom data
    board_config = env.BoardConfig()
    for key, value in variables.UnknownVariables().items():
        if not key.startswith("BOARD_"):
            continue
        board_config.update(key.lower()[6:], base64.b64decode(value))

    # update default environment variables
    for key in variables.keys():
        if key in env or \
                not any([key.startswith("BOARD_"), key.startswith("UPLOAD_")]):
            continue
        _opt, _val = key.lower().split("_", 1)
        if _opt == "board":
            _opt = "build"
        if _val in board_config.get(_opt):
            env.Replace(**{key: board_config.get("%s.%s" % (_opt, _val))})

    if "build.ldscript" in board_config:
        env.Replace(LDSCRIPT_PATH=board_config.get("build.ldscript"))
Пример #29
0
def cli():
    last = get_latest_version()
    if __version__ == last:
        return click.secho(
            "You're up-to-date!\nPlatformIO %s is currently the "
            "newest version available." % __version__, fg="green"
        )
    else:
        click.secho("Please wait while upgrading PlatformIO ...",
                    fg="yellow")

        cmds = (
            ["pip", "install", "--upgrade", "platformio"],
            ["platformio", "--version"]
        )

        cmd = None
        r = None
        try:
            for cmd in cmds:
                r = None
                r = util.exec_command(cmd)

                # try pip with disabled cache
                if r['returncode'] != 0 and cmd[0] == "pip":
                    r = util.exec_command(["pip", "--no-cache-dir"] + cmd[1:])

                assert r['returncode'] == 0
            assert last in r['out'].strip()
            click.secho(
                "PlatformIO has been successfully upgraded to %s" % last,
                fg="green")
            click.echo("Release notes: ", nl=False)
            click.secho("http://docs.platformio.org/en/latest/history.html",
                        fg="cyan")
        except Exception as e:  # pylint: disable=W0703
            if not r:
                raise exception.UpgradeError(
                    "\n".join([str(cmd), str(e)]))
            permission_errors = (
                "permission denied",
                "not permitted"
            )
            if (any([m in r['err'].lower() for m in permission_errors]) and
                    "windows" not in util.get_systype()):
                click.secho("""
-----------------
Permission denied
-----------------
You need the `sudo` permission to install Python packages. Try

> sudo pip install -U platformio

WARNING! Don't use `sudo` for the rest PlatformIO commands.
""", fg="yellow", err=True)
                raise exception.ReturnErrorCode()
            else:
                raise exception.UpgradeError(
                    "\n".join([str(cmd), r['out'], r['err']]))
Пример #30
0
def install_python_deps():
    def _get_installed_pip_packages():
        result = {}
        packages = {}
        pip_output = subprocess.check_output(
            [env.subst("$PYTHONEXE"), "-m", "pip", "list", "--format=json"])
        try:
            packages = json.loads(pip_output)
        except:
            print(
                "Warning! Couldn't extract the list of installed Python packages."
            )
            return {}
        for p in packages:
            result[p["name"]] = pepver_to_semver(p["version"])

        return result

    deps = {
        # https://github.com/platformio/platform-espressif32/issues/635
        "cryptography": ">=2.1.4,<35.0.0",
        "future": ">=0.15.2",
        "pyparsing": ">=2.0.3,<2.4.0",
        "kconfiglib": "==13.7.1",
    }

    installed_packages = _get_installed_pip_packages()
    packages_to_install = []
    for package, spec in deps.items():
        if package not in installed_packages:
            packages_to_install.append(package)
        else:
            version_spec = semantic_version.Spec(spec)
            if not version_spec.match(installed_packages[package]):
                packages_to_install.append(package)

    if packages_to_install:
        env.Execute(
            env.VerboseAction(
                ('"$PYTHONEXE" -m pip install -U --force-reinstall ' +
                 " ".join(
                     ['"%s%s"' % (p, deps[p]) for p in packages_to_install])),
                "Installing ESP-IDF's Python dependencies",
            ))

    # a special "esp-windows-curses" python package is required on Windows for Menuconfig
    if "windows" in get_systype():
        import pkg_resources

        if "esp-windows-curses" not in {
                pkg.key
                for pkg in pkg_resources.working_set
        }:
            env.Execute(
                env.VerboseAction(
                    '$PYTHONEXE -m pip install "file://%s/tools/kconfig_new/esp-windows-curses" windows-curses'
                    % FRAMEWORK_DIR,
                    "Installing windows-curses package",
                ))
Пример #31
0
def main():
    platforms = json.loads(
        subprocess.check_output(
            ["platformio", "platform", "search", "--json-output"]))
    for platform in platforms:
        if platform['forDesktop']:
            continue
        # RISC-V GAP does not support Windows 86
        if (util.get_systype() == "windows_x86"
                and platform['name'] == "riscv_gap"):
            continue
        # unknown issue on Linux
        if ("linux" in util.get_systype()
                and platform['name'] == "aceinna_imu"):
            continue
        subprocess.check_call(
            ["platformio", "platform", "install", platform['repository']])
Пример #32
0
def LoadPioPlatform(env, variables):
    p = env.PioPlatform()
    installed_packages = p.get_installed_packages()

    # Ensure real platform name
    env['PIOPLATFORM'] = p.name

    # Add toolchains and uploaders to $PATH and $*_LIBRARY_PATH
    systype = util.get_systype()
    for name in installed_packages:
        type_ = p.get_package_type(name)
        if type_ not in ("toolchain", "uploader", "debugger"):
            continue
        pkg_dir = p.get_package_dir(name)
        env.PrependENVPath(
            "PATH",
            join(pkg_dir, "bin") if isdir(join(pkg_dir, "bin")) else pkg_dir)
        if ("windows" not in systype and isdir(join(pkg_dir, "lib"))
                and type_ != "toolchain"):
            env.PrependENVPath(
                "DYLD_LIBRARY_PATH"
                if "darwin" in systype else "LD_LIBRARY_PATH",
                join(pkg_dir, "lib"))

    # Platform specific LD Scripts
    if isdir(join(p.get_dir(), "ldscripts")):
        env.Prepend(LIBPATH=[join(p.get_dir(), "ldscripts")])

    if "BOARD" not in env:
        # handle _MCU and _F_CPU variables for AVR native
        for key, value in variables.UnknownVariables().items():
            if not key.startswith("BOARD_"):
                continue
            env.Replace(
                **{key.upper().replace("BUILD.", ""): base64.b64decode(value)})
        return

    # update board manifest with a custom data
    board_config = env.BoardConfig()
    for key, value in variables.UnknownVariables().items():
        if not key.startswith("BOARD_"):
            continue
        board_config.update(key.lower()[6:], base64.b64decode(value))

    # update default environment variables
    for key in variables.keys():
        if key in env or \
                not any([key.startswith("BOARD_"), key.startswith("UPLOAD_")]):
            continue
        _opt, _val = key.lower().split("_", 1)
        if _opt == "board":
            _opt = "build"
        if _val in board_config.get(_opt):
            env.Replace(**{key: board_config.get("%s.%s" % (_opt, _val))})

    if "build.ldscript" in board_config:
        env.Replace(LDSCRIPT_PATH=board_config.get("build.ldscript"))
Пример #33
0
 def _prefill_custom_data(self):
     caller_id = str(app.get_session_var("caller_id"))
     self['cd1'] = util.get_systype()
     self['cd2'] = "Python/%s %s" % (platform.python_version(),
                                     platform.platform())
     self['cd4'] = 1 if (not util.is_ci() and
                         (caller_id or not util.is_container())) else 0
     if caller_id:
         self['cd5'] = caller_id.lower()
Пример #34
0
    def _add_default_debug_tools(self, board):
        debug = board.manifest.get("debug", {})
        upload_protocols = board.manifest.get("upload", {}).get(
            "protocols", [])
        if "tools" not in debug:
            debug["tools"] = {}

        # CMSIS-DAP / BlackMagic Probe
        for link in ("blackmagic", "cmsis-dap", "jlink"):
            if link not in upload_protocols or link in debug["tools"]:
                continue

            if link == "blackmagic":
                debug["tools"]["blackmagic"] = {
                    "hwids": [["0x1d50", "0x6018"]],
                    "require_debug_port": True
                }

            elif link == "cmsis-dap":
                pyocd_target = debug.get("pyocd_target")
                assert pyocd_target

                debug["tools"][link] = {
                    "onboard": True,
                    "server": {
                        "package": "tool-pyocd",
                        "executable": "$PYTHONEXE",
                        "arguments": [
                            "pyocd-gdbserver.py",
                            "-t",
                            pyocd_target
                        ],
                        "ready_pattern": "GDB server started on port"
                    }
                }

            elif link == "jlink":
                assert debug.get("jlink_device"), (
                    "Missed J-Link Device ID for %s" % board.id)
                debug["tools"][link] = {
                    "server": {
                        "package": "tool-jlink",
                        "arguments": [
                            "-singlerun",
                            "-if", "SWD",
                            "-select", "USB",
                            "-device", debug.get("jlink_device"),
                            "-port", "2331"
                        ],
                        "executable": ("JLinkGDBServerCL.exe"
                                       if "windows" in get_systype() else
                                       "JLinkGDBServer")
                    }
                }

        board.manifest["debug"] = debug
        return board
Пример #35
0
def system_info(json_output):
    project_config = ProjectConfig()
    data = {}
    data["core_version"] = {"title": "PlatformIO Core", "value": __version__}
    data["python_version"] = {
        "title": "Python",
        "value": "{0}.{1}.{2}-{3}.{4}".format(*list(sys.version_info)),
    }
    data["system"] = {"title": "System Type", "value": util.get_systype()}
    data["platform"] = {
        "title": "Platform",
        "value": platform.platform(terse=True)
    }
    data["filesystem_encoding"] = {
        "title": "File System Encoding",
        "value": compat.get_filesystem_encoding(),
    }
    data["locale_encoding"] = {
        "title": "Locale Encoding",
        "value": compat.get_locale_encoding(),
    }
    data["core_dir"] = {
        "title": "PlatformIO Core Directory",
        "value": project_config.get_optional_dir("core"),
    }
    data["platformio_exe"] = {
        "title":
        "PlatformIO Core Executable",
        "value":
        proc.where_is_program(
            "platformio.exe" if proc.WINDOWS else "platformio"),
    }
    data["python_exe"] = {
        "title": "Python Executable",
        "value": proc.get_pythonexe_path(),
    }
    data["global_lib_nums"] = {
        "title": "Global Libraries",
        "value": len(LibraryPackageManager().get_installed()),
    }
    data["dev_platform_nums"] = {
        "title": "Development Platforms",
        "value": len(PlatformPackageManager().get_installed()),
    }
    data["package_tool_nums"] = {
        "title":
        "Tools & Toolchains",
        "value":
        len(
            ToolPackageManager(
                project_config.get_optional_dir("packages")).get_installed()),
    }

    click.echo(
        json.dumps(data) if json_output else tabulate([(
            item["title"], item["value"]) for item in data.values()]))
Пример #36
0
def TouchSerialPort(env, port, baudrate):
    if "windows" not in get_systype():
        try:
            s = Serial(env.subst(port))
            s.close()
        except:  # pylint: disable=W0702
            pass
    s = Serial(port=env.subst(port), baudrate=baudrate)
    s.setDTR(False)
    s.close()
    sleep(0.4)
Пример #37
0
    def configure_default_packages(self, envoptions, targets):
        if (envoptions.get("framework") == "wiringpi" and
                "linux_arm" not in util.get_systype()):
            raise exception.PlatformioException(
                "PlatformIO does not support temporary cross-compilation "
                "for WiringPi framework. Please run PlatformIO directly on "
                "Raspberry Pi"
            )

        return BasePlatform.configure_default_packages(
            self, envoptions, targets)
Пример #38
0
def update_core_packages(only_check=False, silent=False):
    pm = CorePackageManager()
    for name, requirements in CORE_PACKAGES.items():
        pkg_dir = pm.get_package_dir(name)
        if not pkg_dir:
            continue
        if not silent or pm.outdated(pkg_dir, requirements):
            if name == "tool-pioplus" and not only_check:
                shutdown_piohome_servers()
                if "windows" in util.get_systype():
                    sleep(1)
            pm.update(name, requirements, only_check=only_check)
    return True
Пример #39
0
 def _gather_tplvars(self):
     self._tplvars.update(self.get_project_env())
     self._tplvars.update(self.get_project_build_data())
     self._tplvars.update({
         "project_name": self.get_project_name(),
         "srcfiles": self.get_srcfiles(),
         "user_home_dir": abspath(expanduser("~")),
         "project_dir": self.project_dir,
         "systype": util.get_systype(),
         "platformio_path": self._fix_os_path(
             util.where_is_program("platformio")),
         "env_pathsep": os.pathsep,
         "env_path": self._fix_os_path(os.getenv("PATH"))
     })
Пример #40
0
    def _install_from_piorepo(self, name, requirements):
        assert name.startswith("id="), name
        version = self.get_latest_repo_version(name, requirements)
        if not version:
            raise exception.UndefinedPackageVersion(requirements or "latest",
                                                    util.get_systype())
        dl_data = util.get_api_result(
            "/lib/download/" + str(name[3:]),
            dict(version=version),
            cache_valid="30d")
        assert dl_data

        return self._install_from_url(
            name, dl_data['url'].replace("http://", "https://")
            if app.get_setting("enable_ssl") else dl_data['url'], requirements)
Пример #41
0
def cli():
    last = get_latest_version()
    if __version__ == last:
        return click.secho(
            "You're up-to-date!\nPlatformIO %s is currently the " "newest version available." % __version__, fg="green"
        )
    else:
        click.secho("Please wait while upgrading PlatformIO ...", fg="yellow")

        cmds = (
            ["pip", "install", "--upgrade", "pip", "setuptools"],
            ["pip", "install", "--upgrade", "platformio"],
            ["platformio", "--version"],
        )

        cmd = None
        r = None
        try:
            for cmd in cmds:
                r = None
                r = util.exec_command(cmd)
                assert r["returncode"] == 0
            assert last in r["out"].strip()
            click.secho("PlatformIO has been successfully upgraded to %s" % last, fg="green")
            click.echo("Release notes: ", nl=False)
            click.secho("http://docs.platformio.org/en/latest/history.html", fg="cyan")
        except (OSError, AssertionError) as e:
            if not r:
                raise exception.PlatformioUpgradeError("\n".join([str(cmd), str(e)]))
            if "Permission denied" in r["err"] and "windows" not in util.get_systype():
                click.secho(
                    """
-----------------
Permission denied
-----------------
You need the `sudo` permission to install Python packages. Try

> sudo platformio upgrade

WARNING! Don't use `sudo` for the rest PlatformIO commands.
""",
                    fg="yellow",
                    err=True,
                )
                raise exception.ReturnErrorCode()
            else:
                raise exception.PlatformioUpgradeError("\n".join([str(cmd), r["out"], r["err"]]))
Пример #42
0
def calculate_project_hash():
    check_suffixes = (".c", ".cc", ".cpp", ".h", ".hpp", ".s", ".S")
    chunks = [__version__]
    for d in (util.get_projectsrc_dir(), util.get_projectlib_dir()):
        if not isdir(d):
            continue
        for root, _, files in walk(d):
            for f in files:
                path = join(root, f)
                if path.endswith(check_suffixes):
                    chunks.append(path)
    chunks_to_str = ",".join(sorted(chunks))
    if "windows" in util.get_systype():
        # Fix issue with useless project rebuilding for case insensitive FS.
        # A case of disk drive can differ...
        chunks_to_str = chunks_to_str.lower()
    return sha1(chunks_to_str).hexdigest()
Пример #43
0
    def get_info(self, name, version=None):
        manifest = self.get_manifest()
        if name not in manifest:
            raise exception.UnknownPackage(name)

        # check system platform
        systype = util.get_systype()
        builds = ([b for b in manifest[name] if b['system'] == "all" or systype
                   in b['system']])
        if not builds:
            raise exception.NonSystemPackage(name, systype)

        if version:
            for b in builds:
                if b['version'] == version:
                    return b
            raise exception.InvalidPackageVersion(name, version)
        else:
            return sorted(builds, key=lambda s: s['version'])[-1]
Пример #44
0
    def max_satisfying_repo_version(versions, requirements=None):
        item = None
        systype = util.get_systype()
        reqspec = None
        if requirements:
            try:
                reqspec = semantic_version.Spec(requirements)
            except ValueError:
                pass

        for v in versions:
            if "system" in v and v["system"] not in ("all", "*") and systype not in v["system"]:
                continue
            specver = semantic_version.Version(v["version"])
            if reqspec and specver not in reqspec:
                continue
            if not item or semantic_version.Version(item["version"]) < specver:
                item = v
        return item
Пример #45
0
 def _look_for_serial_port():
     port = None
     board_hwids = []
     upload_protocol = env.subst("$UPLOAD_PROTOCOL")
     if "BOARD" in env and "build.hwids" in env.BoardConfig():
         board_hwids = env.BoardConfig().get("build.hwids")
     for item in util.get_serial_ports(filter_hwid=True):
         if not _is_match_pattern(item['port']):
             continue
         port = item['port']
         if upload_protocol.startswith("blackmagic") \
                 and "GDB" in item['description']:
             return ("\\\\.\\%s" % port if "windows" in util.get_systype()
                     and port.startswith("COM") and len(port) > 4 else port)
         for hwid in board_hwids:
             hwid_str = ("%s:%s" % (hwid[0], hwid[1])).replace("0x", "")
             if hwid_str in item['hwid']:
                 return port
     return port
Пример #46
0
 def _gather_tplvars(self):
     self._tplvars.update(self.get_project_env())
     self._tplvars.update(self.get_project_build_data())
     with util.cd(self.project_dir):
         self._tplvars.update({
             "project_name": self.get_project_name(),
             "src_files": self.get_src_files(),
             "user_home_dir": abspath(expanduser("~")),
             "project_dir": self.project_dir,
             "project_src_dir": util.get_projectsrc_dir(),
             "project_lib_dir": util.get_projectlib_dir(),
             "project_libdeps_dir": util.get_projectlibdeps_dir(),
             "systype": util.get_systype(),
             "platformio_path": self._fix_os_path(
                 sys.argv[0] if isfile(sys.argv[0])
                 else util.where_is_program("platformio")),
             "env_pathsep": os.pathsep,
             "env_path": self._fix_os_path(os.getenv("PATH"))
         })  # yapf: disable
Пример #47
0
    def _install_from_piorepo(self, name, requirements):
        pkg_dir = None
        pkgdata = None
        versions = None
        for versions in PackageRepoIterator(name, self.repositories):
            pkgdata = self.max_satisfying_repo_version(versions, requirements)
            if not pkgdata:
                continue
            try:
                pkg_dir = self._install_from_url(name, pkgdata["url"], requirements, pkgdata.get("sha1"))
                break
            except Exception as e:  # pylint: disable=broad-except
                click.secho("Warning! Package Mirror: %s" % e, fg="yellow")
                click.secho("Looking for other mirror...", fg="yellow")

        if versions is None:
            raise exception.UnknownPackage(name)
        elif not pkgdata:
            raise exception.UndefinedPackageVersion(requirements or "latest", util.get_systype())
        return pkg_dir
Пример #48
0
    def _prefill_custom_data(self):

        def _filter_args(items):
            result = []
            stop = False
            for item in items:
                item = str(item).lower()
                result.append(item)
                if stop:
                    break
                if item == "account":
                    stop = True
            return result

        caller_id = str(app.get_session_var("caller_id"))
        self['cd1'] = util.get_systype()
        self['cd2'] = "Python/%s %s" % (platform.python_version(),
                                        platform.platform())
        # self['cd3'] = " ".join(_filter_args(sys.argv[1:]))
        self['cd4'] = 1 if (not util.is_ci() and
                            (caller_id or not util.is_container())) else 0
        if caller_id:
            self['cd5'] = caller_id.lower()
Пример #49
0
    def max_satisfying_repo_version(versions, requirements=None):
        item = None
        systype = util.get_systype()
        reqspec = None
        if requirements:
            try:
                reqspec = semantic_version.Spec(requirements)
            except ValueError:
                pass

        for v in versions:
            if "system" in v and v['system'] not in ("all", "*") and \
                    systype not in v['system']:
                continue
            if "platformio" in v.get("engines", {}):
                if PkgRepoMixin.PIO_VERSION not in semantic_version.Spec(
                        v['engines']['platformio']):
                    continue
            specver = semantic_version.Version(v['version'])
            if reqspec and specver not in reqspec:
                continue
            if not item or semantic_version.Version(item['version']) < specver:
                item = v
        return item
Пример #50
0
 def _fix_os_path(path):
     return (re.sub(r"[\\]+", '\\' * 4, path)
             if "windows" in util.get_systype() else path)
Пример #51
0
def cli():
    last = get_latest_version()
    if __version__ == last:
        return click.secho(
            "You're up-to-date!\nPlatformIO %s is currently the " "newest version available." % __version__, fg="green"
        )
    else:
        click.secho("Please wait while upgrading PlatformIO ...", fg="yellow")

        to_develop = False
        try:
            from pkg_resources import parse_version

            to_develop = parse_version(last) < parse_version(__version__)
        except ImportError:
            pass

        cmds = (
            [
                "pip",
                "install",
                "--upgrade",
                "https://github.com/platformio/platformio/archive/develop.zip" if to_develop else "platformio",
            ],
            ["platformio", "--version"],
        )

        cmd = None
        r = None
        try:
            for cmd in cmds:
                cmd = [os.path.normpath(sys.executable), "-m"] + cmd
                r = None
                r = util.exec_command(cmd)

                # try pip with disabled cache
                if r["returncode"] != 0 and cmd[2] == "pip":
                    cmd.insert(3, "--no-cache-dir")
                    r = util.exec_command(cmd)

                assert r["returncode"] == 0
            assert "version" in r["out"]
            actual_version = r["out"].strip().split("version", 1)[1].strip()
            click.secho("PlatformIO has been successfully upgraded to %s" % actual_version, fg="green")
            click.echo("Release notes: ", nl=False)
            click.secho("http://docs.platformio.org/en/latest/history.html", fg="cyan")
        except Exception as e:  # pylint: disable=W0703
            if not r:
                raise exception.UpgradeError("\n".join([str(cmd), str(e)]))
            permission_errors = ("permission denied", "not permitted")
            if any([m in r["err"].lower() for m in permission_errors]) and "windows" not in util.get_systype():
                click.secho(
                    """
-----------------
Permission denied
-----------------
You need the `sudo` permission to install Python packages. Try

> sudo pip install -U platformio

WARNING! Don't use `sudo` for the rest PlatformIO commands.
""",
                    fg="yellow",
                    err=True,
                )
                raise exception.ReturnErrorCode()
            else:
                raise exception.UpgradeError("\n".join([str(cmd), r["out"], r["err"]]))
Пример #52
0
def cli():
    # Update PlatformIO's Core packages
    update_core_packages(silent=True)

    latest = get_latest_version()
    if __version__ == latest:
        return click.secho(
            "You're up-to-date!\nPlatformIO %s is currently the "
            "newest version available." % __version__,
            fg="green")
    else:
        click.secho("Please wait while upgrading PlatformIO ...", fg="yellow")

        to_develop = not all([c.isdigit() for c in latest if c != "."])
        cmds = ([
            "pip", "install", "--upgrade",
            "https://github.com/platformio/platformio-core/archive/develop.zip"
            if to_develop else "platformio"
        ], ["platformio", "--version"])

        cmd = None
        r = None
        try:
            for cmd in cmds:
                cmd = [util.get_pythonexe_path(), "-m"] + cmd
                r = None
                r = util.exec_command(cmd)

                # try pip with disabled cache
                if r['returncode'] != 0 and cmd[2] == "pip":
                    cmd.insert(3, "--no-cache-dir")
                    r = util.exec_command(cmd)

                assert r['returncode'] == 0
            assert "version" in r['out']
            actual_version = r['out'].strip().split("version", 1)[1].strip()
            click.secho(
                "PlatformIO has been successfully upgraded to %s" %
                actual_version,
                fg="green")
            click.echo("Release notes: ", nl=False)
            click.secho(
                "http://docs.platformio.org/en/latest/history.html", fg="cyan")
        except Exception as e:  # pylint: disable=W0703
            if not r:
                raise exception.UpgradeError("\n".join([str(cmd), str(e)]))
            permission_errors = ("permission denied", "not permitted")
            if (any([m in r['err'].lower() for m in permission_errors])
                    and "windows" not in util.get_systype()):
                click.secho(
                    """
-----------------
Permission denied
-----------------
You need the `sudo` permission to install Python packages. Try

> sudo pip install -U platformio

WARNING! Don't use `sudo` for the rest PlatformIO commands.
""",
                    fg="yellow",
                    err=True)
                raise exception.ReturnErrorCode(1)
            else:
                raise exception.UpgradeError(
                    "\n".join([str(cmd), r['out'], r['err']]))
Пример #53
0
def AutodetectUploadPort(*args, **kwargs):  # pylint: disable=unused-argument
    env = args[0]

    def _get_pattern():
        if "UPLOAD_PORT" not in env:
            return None
        if set(["*", "?", "[", "]"]) & set(env['UPLOAD_PORT']):
            return env['UPLOAD_PORT']
        return None

    def _is_match_pattern(port):
        pattern = _get_pattern()
        if not pattern:
            return True
        return fnmatch(port, pattern)

    def _look_for_mbed_disk():
        msdlabels = ("mbed", "nucleo", "frdm", "microbit")
        for item in util.get_logical_devices():
            if item['path'].startswith("/net") or not _is_match_pattern(
                    item['path']):
                continue
            mbed_pages = [
                join(item['path'], n) for n in ("mbed.htm", "mbed.html")
            ]
            if any(isfile(p) for p in mbed_pages):
                return item['path']
            if item['name'] \
                    and any(l in item['name'].lower() for l in msdlabels):
                return item['path']
        return None

    def _look_for_serial_port():
        port = None
        board_hwids = []
        upload_protocol = env.subst("$UPLOAD_PROTOCOL")
        if "BOARD" in env and "build.hwids" in env.BoardConfig():
            board_hwids = env.BoardConfig().get("build.hwids")
        for item in util.get_serial_ports(filter_hwid=True):
            if not _is_match_pattern(item['port']):
                continue
            port = item['port']
            if upload_protocol.startswith("blackmagic") \
                    and "GDB" in item['description']:
                return ("\\\\.\\%s" % port if "windows" in util.get_systype()
                        and port.startswith("COM") and len(port) > 4 else port)
            for hwid in board_hwids:
                hwid_str = ("%s:%s" % (hwid[0], hwid[1])).replace("0x", "")
                if hwid_str in item['hwid']:
                    return port
        return port

    if "UPLOAD_PORT" in env and not _get_pattern():
        print env.subst("Use manually specified: $UPLOAD_PORT")
        return

    if (env.subst("$UPLOAD_PROTOCOL") == "mbed"
            or ("mbed" in env.subst("$PIOFRAMEWORK")
                and not env.subst("$UPLOAD_PROTOCOL"))):
        env.Replace(UPLOAD_PORT=_look_for_mbed_disk())
    else:
        if ("linux" in util.get_systype() and not any([
                isfile("/etc/udev/rules.d/99-platformio-udev.rules"),
                isfile("/lib/udev/rules.d/99-platformio-udev.rules")
        ])):
            sys.stderr.write(
                "\nWarning! Please install `99-platformio-udev.rules` and "
                "check that your board's PID and VID are listed in the rules."
                "\n http://docs.platformio.org/en/latest/faq.html"
                "#platformio-udev-rules\n")
        env.Replace(UPLOAD_PORT=_look_for_serial_port())

    if env.subst("$UPLOAD_PORT"):
        print env.subst("Auto-detected: $UPLOAD_PORT")
    else:
        sys.stderr.write(
            "Error: Please specify `upload_port` for environment or use "
            "global `--upload-port` option.\n"
            "For some development platforms it can be a USB flash "
            "drive (i.e. /media/<user>/<device name>)\n")
        env.Exit(1)
Пример #54
0
 def _prefill_custom_data(self):
     self['cd1'] = util.get_systype()
     self['cd2'] = "Python/%s %s" % (platform.python_version(),
                                     platform.platform())
     self['cd4'] = (1 if app.get_setting("enable_prompts") or
                    app.get_session_var("caller_id") else 0)
Пример #55
0
env = DefaultEnvironment()

env.Replace(
    _BINPREFIX="",
    AR="${_BINPREFIX}ar",
    AS="${_BINPREFIX}as",
    CC="${_BINPREFIX}gcc",
    CXX="${_BINPREFIX}g++",
    OBJCOPY="${_BINPREFIX}objcopy",
    RANLIB="${_BINPREFIX}ranlib",
    SIZETOOL="${_BINPREFIX}size",

    SIZEPRINTCMD='"$SIZETOOL" $SOURCES'
)

if get_systype() == "darwin_x86_64":
    env.Replace(
        _BINPREFIX="arm-linux-gnueabihf-"
    )

#
# Target: Build executable program
#

target_bin = env.BuildProgram()

#
# Target: Print binary size
#

target_size = env.Alias("size", target_bin, "$SIZEPRINTCMD")
Пример #56
0
 def is_system_compatible(valid_systems):
     if valid_systems in (None, "all", "*"):
         return True
     if not isinstance(valid_systems, list):
         valid_systems = list([valid_systems])
     return util.get_systype() in valid_systems
Пример #57
0
 def _prefill_custom_data(self):
     self['cd1'] = get_systype()
     self['cd2'] = "Python/%s %s" % (platform.python_version(),
                                     platform.platform())
     self['cd4'] = 1 if app.get_setting("enable_prompts") else 0