Пример #1
0
    def delete(self, keys=None):
        """ Keys=None, delete expired items """
        if not keys:
            keys = []
        if not isinstance(keys, list):
            keys = [keys]
        paths_for_delete = [self.get_cache_path(k) for k in keys]
        found = False
        newlines = []
        with open(self._db_path) as fp:
            for line in fp.readlines():
                if "=" not in line:
                    continue
                line = line.strip()
                expire, path = line.split("=")
                if time() < int(expire) and isfile(path) and \
                        path not in paths_for_delete:
                    newlines.append(line)
                    continue
                found = True
                if isfile(path):
                    try:
                        remove(path)
                        if not listdir(dirname(path)):
                            util.rmtree_(dirname(path))
                    except OSError:
                        pass

        if found and self._lock_dbindex():
            with open(self._db_path, "w") as fp:
                fp.write("\n".join(newlines) + "\n")
            self._unlock_dbindex()

        return True
Пример #2
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
Пример #3
0
    def uninstall(self, name, requirements=None, trigger_event=True):
        name, requirements, url = self.parse_pkg_name(name, requirements)
        package_dir = self.get_package_dir(name, requirements, url)
        if not package_dir:
            click.secho("%s @ %s is not installed" %
                        (name, requirements or "*"),
                        fg="yellow")
            return

        manifest = self.load_manifest(package_dir)
        click.echo(
            "Uninstalling %s @ %s: \t" %
            (click.style(manifest['name'], fg="cyan"), manifest['version']),
            nl=False)

        if isdir(package_dir):
            if islink(package_dir):
                os.unlink(package_dir)
            else:
                util.rmtree_(package_dir)

        click.echo("[%s]" % click.style("OK", fg="green"))

        self.reset_cache()
        if trigger_event:
            telemetry.on_event(category=self.__class__.__name__,
                               action="Uninstall",
                               label=manifest['name'])
        return True
Пример #4
0
def test_run(pioproject_dir):
    with util.cd(pioproject_dir):
        build_dir = util.get_projectbuild_dir()
        if isdir(build_dir):
            util.rmtree_(build_dir)

        env_names = []
        for section in util.load_project_config().sections():
            if section.startswith("env:"):
                env_names.append(section[4:])

        result = util.exec_command(
            ["platformio", "run", "-e",
             random.choice(env_names)])
        if result['returncode'] != 0:
            pytest.fail(result)

        assert isdir(build_dir)

        # check .elf file
        for item in listdir(build_dir):
            if not isdir(item):
                continue
            assert isfile(join(build_dir, item, "firmware.elf"))
            # check .hex or .bin files
            firmwares = []
            for ext in ("bin", "hex"):
                firmwares += glob(join(build_dir, item, "firmware*.%s" % ext))
            if not firmwares:
                pytest.fail("Missed firmware file")
            for firmware in firmwares:
                assert getsize(firmware) > 0
Пример #5
0
    def __enter__(self):
        if not self._db_path or not isfile(self._db_path):
            return self
        found = False
        newlines = []
        with open(self._db_path) as fp:
            for line in fp.readlines():
                if "=" not in line:
                    continue
                line = line.strip()
                expire, path = line.split("=")
                if time() < int(expire):
                    newlines.append(line)
                    continue
                found = True
                if isfile(path):
                    remove(path)
                    if not len(listdir(dirname(path))):
                        util.rmtree_(dirname(path))

        if found and self._lock_dbindex():
            with open(self._db_path, "w") as fp:
                fp.write("\n".join(newlines) + "\n")
            self._unlock_dbindex()

        return self
Пример #6
0
    def _install_from_url(self, name, url, requirements=None, sha1=None):
        pkg_dir = None
        tmp_dir = mkdtemp("-package", "installing-", self.package_dir)

        try:
            if url.startswith("file://"):
                url = url[7:]
                if isfile(url):
                    self.unpack(url, tmp_dir)
                else:
                    util.rmtree_(tmp_dir)
                    shutil.copytree(url, tmp_dir)
            elif url.startswith(("http://", "https://")):
                dlpath = self.download(url, tmp_dir, sha1)
                assert isfile(dlpath)
                self.unpack(dlpath, tmp_dir)
                os.remove(dlpath)
            else:
                vcs = VCSClientFactory.newClient(tmp_dir, url)
                assert vcs.export()
                with open(join(vcs.storage_dir, self.VCS_MANIFEST_NAME), "w") as fp:
                    json.dump(
                        {"name": name, "version": vcs.get_current_revision(), "url": url, "requirements": requirements},
                        fp,
                    )

            pkg_dir = self.check_pkg_structure(tmp_dir)
            pkg_dir = self._install_from_tmp_dir(pkg_dir, requirements)
        finally:
            if isdir(tmp_dir):
                util.rmtree_(tmp_dir)
        return pkg_dir
Пример #7
0
    def delete(self, keys=None):
        """ Keys=None, delete expired items """
        if not keys:
            keys = []
        if not isinstance(keys, list):
            keys = [keys]
        paths_for_delete = [self.get_cache_path(k) for k in keys]
        found = False
        newlines = []
        with open(self._db_path) as fp:
            for line in fp.readlines():
                if "=" not in line:
                    continue
                line = line.strip()
                expire, path = line.split("=")
                if time() < int(expire) and isfile(path) and \
                        path not in paths_for_delete:
                    newlines.append(line)
                    continue
                found = True
                if isfile(path):
                    try:
                        remove(path)
                        if not listdir(dirname(path)):
                            util.rmtree_(dirname(path))
                    except OSError:
                        pass

        if found and self._lock_dbindex():
            with open(self._db_path, "w") as fp:
                fp.write("\n".join(newlines) + "\n")
            self._unlock_dbindex()

        return True
Пример #8
0
def test_run(pioproject_dir):
    with util.cd(pioproject_dir):
        build_dir = util.get_projectbuild_dir()
        if isdir(build_dir):
            util.rmtree_(build_dir)

        result = util.exec_command(["platformio", "--force", "run"])
        if result['returncode'] != 0:
            pytest.fail(result)

        assert isdir(build_dir)

        # check .elf file
        for item in listdir(build_dir):
            if not isdir(item):
                continue
            assert isfile(join(build_dir, item, "firmware.elf"))
            # check .hex or .bin files
            firmwares = []
            for ext in ("bin", "hex"):
                firmwares += glob(join(build_dir, item, "firmware*.%s" % ext))
            if not firmwares:
                pytest.fail("Missed firmware file")
            for firmware in firmwares:
                assert getsize(firmware) > 0
Пример #9
0
    def __enter__(self):
        if not self._db_path or not isfile(self._db_path):
            return self
        found = False
        newlines = []
        with open(self._db_path) as fp:
            for line in fp.readlines():
                if "=" not in line:
                    continue
                line = line.strip()
                expire, path = line.split("=")
                if time() < int(expire) and isfile(path):
                    newlines.append(line)
                    continue
                found = True
                if isfile(path):
                    remove(path)
                    if not len(listdir(dirname(path))):
                        util.rmtree_(dirname(path))

        if found and self._lock_dbindex():
            with open(self._db_path, "w") as fp:
                fp.write("\n".join(newlines) + "\n")
            self._unlock_dbindex()

        return self
Пример #10
0
def test_run(pioproject_dir):
    with util.cd(pioproject_dir):
        build_dir = util.get_projectbuild_dir()
        if isdir(build_dir):
            util.rmtree_(build_dir)

        env_names = []
        for section in util.load_project_config().sections():
            if section.startswith("env:"):
                env_names.append(section[4:])

        result = util.exec_command(
            ["platformio", "run", "-e",
             random.choice(env_names)])
        if result['returncode'] != 0:
            pytest.fail(result)

        assert isdir(build_dir)

        # check .elf file
        for item in listdir(build_dir):
            if not isdir(item):
                continue
            assert isfile(join(build_dir, item, "firmware.elf"))
            # check .hex or .bin files
            firmwares = []
            for ext in ("bin", "hex"):
                firmwares += glob(join(build_dir, item, "firmware*.%s" % ext))
            if not firmwares:
                pytest.fail("Missed firmware file")
            for firmware in firmwares:
                assert getsize(firmware) > 0
Пример #11
0
def test_run(pioproject_dir):
    with util.cd(pioproject_dir):
        build_dir = get_project_build_dir()
        if isdir(build_dir):
            util.rmtree_(build_dir)

        env_names = ProjectConfig(join(pioproject_dir,
                                       "platformio.ini")).envs()
        result = util.exec_command(
            ["platformio", "run", "-e",
             random.choice(env_names)])
        if result['returncode'] != 0:
            pytest.fail(str(result))

        assert isdir(build_dir)

        # check .elf file
        for item in listdir(build_dir):
            if not isdir(item):
                continue
            assert isfile(join(build_dir, item, "firmware.elf"))
            # check .hex or .bin files
            firmwares = []
            for ext in ("bin", "hex"):
                firmwares += glob(join(build_dir, item, "firmware*.%s" % ext))
            if not firmwares:
                pytest.fail("Missed firmware file")
            for firmware in firmwares:
                assert getsize(firmware) > 0
Пример #12
0
 def _finalize_arduino_import(_, project_dir, arduino_project_dir):
     with util.cd(project_dir):
         src_dir = get_project_src_dir()
         if isdir(src_dir):
             util.rmtree_(src_dir)
         shutil.copytree(arduino_project_dir, src_dir)
     return project_dir
Пример #13
0
    def processEnded(self, reason):  # pylint: disable=unused-argument
        self._unlock_session()
        if self._gdbsrc_dir and isdir(self._gdbsrc_dir):
            util.rmtree_(self._gdbsrc_dir)
        if self._debug_server:
            self._debug_server.terminate()

        reactor.stop()
Пример #14
0
    def patched_clean_build_dir(build_dir, *args):
        platformio_ini = join(get_project_dir(), "platformio.ini")

        # if project's config is modified
        if isdir(build_dir) and getmtime(platformio_ini) > getmtime(build_dir):
            util.rmtree_(build_dir)

        if not isdir(build_dir):
            makedirs(build_dir)
Пример #15
0
def _exclude_contents(dst_dir, patterns):
    contents = []
    for p in patterns:
        contents += glob(join(util.glob_escape(dst_dir), p))
    for path in contents:
        path = abspath(path)
        if isdir(path):
            util.rmtree_(path)
        elif isfile(path):
            remove(path)
Пример #16
0
def _exclude_contents(dst_dir, patterns):
    contents = []
    for p in patterns:
        contents += glob(join(util.glob_escape(dst_dir), p))
    for path in contents:
        path = abspath(path)
        if isdir(path):
            util.rmtree_(path)
        elif isfile(path):
            remove(path)
Пример #17
0
def build_autotiler(build_dir, generator, model_path):
    if isdir(build_dir):
        util.rmtree_(build_dir)

    # parse custom library path from `platformio.ini`
    tmpenv = env.Clone()
    tmpenv.ProcessFlags(env.get("BUILD_FLAGS"))

    genv = env.Environment(
        tools=["ar", "gas", "gcc", "g++", "gnulink"],
        CC="gcc",
        CPPPATH=[
            join(AUTOTILER_DIR, "include"),
            join(AUTOTILER_DIR, "generators", generator, "generator",
                 "include")
        ],
        LIBPATH=[join(AUTOTILER_DIR, "lib"),
                 get_project_lib_dir()] + tmpenv.get("LIBPATH", []),
        LIBS=["tile"])

    # CHECK "libtile.a"
    found_libtile = False
    for d in genv['LIBPATH']:
        if isfile(genv.subst(join(d, "libtile.a"))):
            found_libtile = True
            break

    if not found_libtile:
        sys.stderr.write(
            "Error: AutoTiler library has not been found. Please read => "
            "https://docs.platformio.org/page/platforms/riscv_gap.html"
            "#autotiler\n")
        env.Exit(1)

    variant_dirs = [(join(build_dir, "model"), dirname(model_path)),
                    (join(build_dir, "generator"),
                     join(AUTOTILER_DIR, "generators", generator, "generator",
                          "src"))]
    for (var_dir, src_dir) in variant_dirs:
        if not isdir(genv.subst(var_dir)):
            makedirs(genv.subst(var_dir))
        genv.VariantDir(var_dir, src_dir, duplicate=0)

    src_files = [join(build_dir, "model", basename(model_path))]
    src_files.extend(genv.Glob(join(build_dir, "generator", "*Generator?.c")))

    for o in genv.Object(src_files):
        if not int(ARGUMENTS.get("PIOVERBOSE", 0)):
            genv.Replace(CCCOMSTR="Compiling %s" % relpath(str(o)))
        o.build()

    if not int(ARGUMENTS.get("PIOVERBOSE", 0)):
        genv.Replace(LINKCOMSTR="Linking AutoTiler")

    return genv.Program(join(build_dir, "program"), src_files)[0].build()
Пример #18
0
def PioClean(env, clean_dir):
    if not isdir(clean_dir):
        print "Build environment is clean"
        env.Exit(0)
    for root, _, files in walk(clean_dir):
        for file_ in files:
            remove(join(root, file_))
            print "Removed %s" % relpath(join(root, file_))
    print "Done cleaning"
    util.rmtree_(clean_dir)
    env.Exit(0)
Пример #19
0
def PioClean(env, clean_dir):
    if not isdir(clean_dir):
        print "Build environment is clean"
        env.Exit(0)
    for root, _, files in walk(clean_dir):
        for file_ in files:
            remove(join(root, file_))
            print "Removed %s" % relpath(join(root, file_))
    print "Done cleaning"
    util.rmtree_(clean_dir)
    env.Exit(0)
Пример #20
0
def PioClean(env, clean_dir):
    if not isdir(clean_dir):
        print("Build environment is clean")
        env.Exit(0)
    clean_rel_path = relpath(clean_dir)
    for root, _, files in walk(clean_dir):
        for f in files:
            dst = join(root, f)
            remove(dst)
            print("Removed %s" %
                  (dst if clean_rel_path.startswith(".") else relpath(dst)))
    print("Done cleaning")
    util.rmtree_(clean_dir)
    env.Exit(0)
Пример #21
0
def cli(ctx,  # pylint: disable=R0913
        src,
        lib,
        exclude,
        board,
        build_dir,
        keep_build_dir,
        project_conf,
        project_option,
        verbose):

    if not src and getenv("PLATFORMIO_CI_SRC"):
        src = validate_path(ctx, None, getenv("PLATFORMIO_CI_SRC").split(":"))
    if not src:
        raise click.BadParameter("Missing argument 'src'")

    try:
        app.set_session_var("force_option", True)
        _clean_dir(build_dir)

        for dir_name, patterns in dict(lib=lib, src=src).iteritems():
            if not patterns:
                continue
            contents = []
            for p in patterns:
                contents += glob(p)
            _copy_contents(join(build_dir, dir_name), contents)

        if project_conf and isfile(project_conf):
            _copy_project_conf(build_dir, project_conf)
        elif not board:
            raise CIBuildEnvsEmpty()

        if exclude:
            _exclude_contents(build_dir, exclude)

        # initialise project
        ctx.invoke(
            cmd_init,
            project_dir=build_dir,
            board=board,
            project_option=project_option)

        # process project
        ctx.invoke(cmd_run, project_dir=build_dir, verbose=verbose)
    finally:
        if not keep_build_dir:
            util.rmtree_(build_dir)
Пример #22
0
    def uninstall(self, package, requirements=None, after_update=False):
        # interprocess lock
        with LockFile(self.package_dir):
            self.cache_reset()

            if isdir(package) and self.get_package_by_dir(package):
                pkg_dir = package
            else:
                name, requirements, url = self.parse_pkg_uri(
                    package, requirements)
                pkg_dir = self.get_package_dir(name, requirements, url)

            if not pkg_dir:
                raise exception.UnknownPackage(
                    "%s @ %s" % (package, requirements or "*"))

            manifest = self.load_manifest(pkg_dir)
            click.echo(
                "Uninstalling %s @ %s: \t" % (click.style(
                    manifest['name'], fg="cyan"), manifest['version']),
                nl=False)

            if islink(pkg_dir):
                os.unlink(pkg_dir)
            else:
                util.rmtree_(pkg_dir)
            self.cache_reset()

            # unfix package with the same name
            pkg_dir = self.get_package_dir(manifest['name'])
            if pkg_dir and "@" in pkg_dir:
                shutil.move(
                    pkg_dir,
                    join(self.package_dir, self.get_install_dirname(manifest)))
                self.cache_reset()

            click.echo("[%s]" % click.style("OK", fg="green"))

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

        return True
Пример #23
0
def cli(  # pylint: disable=too-many-arguments, too-many-branches
        ctx, src, lib, exclude, board, build_dir, keep_build_dir, project_conf,
        project_option, verbose):

    if not src and getenv("PLATFORMIO_CI_SRC"):
        src = validate_path(ctx, None, getenv("PLATFORMIO_CI_SRC").split(":"))
    if not src:
        raise click.BadParameter("Missing argument 'src'")

    try:
        app.set_session_var("force_option", True)

        if not keep_build_dir and isdir(build_dir):
            util.rmtree_(build_dir)
        if not isdir(build_dir):
            makedirs(build_dir)

        for dir_name, patterns in dict(lib=lib, src=src).items():
            if not patterns:
                continue
            contents = []
            for p in patterns:
                contents += glob(p)
            _copy_contents(join(build_dir, dir_name), contents)

        if project_conf and isfile(project_conf):
            _copy_project_conf(build_dir, project_conf)
        elif not board:
            raise CIBuildEnvsEmpty()

        if exclude:
            _exclude_contents(build_dir, exclude)

        # initialise project
        ctx.invoke(
            cmd_init,
            project_dir=build_dir,
            board=board,
            project_option=project_option)

        # process project
        ctx.invoke(cmd_run, project_dir=build_dir, verbose=verbose)
    finally:
        if not keep_build_dir:
            util.rmtree_(build_dir)
Пример #24
0
    def uninstall(self, package, requirements=None, after_update=False):
        # interprocess lock
        with LockFile(self.package_dir):
            self.cache_reset()

            if isdir(package) and self.get_package_by_dir(package):
                pkg_dir = package
            else:
                name, requirements, url = self.parse_pkg_uri(
                    package, requirements)
                pkg_dir = self.get_package_dir(name, requirements, url)

            if not pkg_dir:
                raise exception.UnknownPackage("%s @ %s" %
                                               (package, requirements or "*"))

            manifest = self.load_manifest(pkg_dir)
            click.echo("Uninstalling %s @ %s: \t" % (click.style(
                manifest['name'], fg="cyan"), manifest['version']),
                       nl=False)

            if islink(pkg_dir):
                os.unlink(pkg_dir)
            else:
                util.rmtree_(pkg_dir)
            self.cache_reset()

            # unfix package with the same name
            pkg_dir = self.get_package_dir(manifest['name'])
            if pkg_dir and "@" in pkg_dir:
                shutil.move(
                    pkg_dir,
                    join(self.package_dir, self.get_install_dirname(manifest)))
                self.cache_reset()

            click.echo("[%s]" % click.style("OK", fg="green"))

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

        return True
Пример #25
0
    def _install_from_tmp_dir(self, tmp_dir, requirements=None):
        tmpmanifest = self.load_manifest(tmp_dir)
        assert set(["name", "version"]) <= set(tmpmanifest.keys())
        name = tmpmanifest['name']
        pkg_dir = join(self.package_dir, name)
        if "id" in tmpmanifest:
            name += "_ID%d" % tmpmanifest['id']
            pkg_dir = join(self.package_dir, name)

        # package should satisfy requirements
        if requirements:
            mismatch_error = (
                "Package version %s doesn't satisfy requirements %s" %
                (tmpmanifest['version'], requirements))
            try:
                reqspec = semantic_version.Spec(requirements)
                tmpmanver = semantic_version.Version(tmpmanifest['version'],
                                                     partial=True)
                assert tmpmanver in reqspec, mismatch_error

                if self.manifest_exists(pkg_dir):
                    curmanifest = self.load_manifest(pkg_dir)
                    curmanver = semantic_version.Version(
                        curmanifest['version'], partial=True)
                    # if current package version < new package, backup it
                    if tmpmanver > curmanver:
                        os.rename(
                            pkg_dir,
                            join(self.package_dir,
                                 "%s@%s" % (name, curmanifest['version'])))
                    elif tmpmanver < curmanver:
                        pkg_dir = join(
                            self.package_dir,
                            "%s@%s" % (name, tmpmanifest['version']))
            except ValueError:
                assert tmpmanifest['version'] == requirements, mismatch_error

        # remove previous/not-satisfied package
        if isdir(pkg_dir):
            util.rmtree_(pkg_dir)
        os.rename(tmp_dir, pkg_dir)
        assert isdir(pkg_dir)
        return pkg_dir
Пример #26
0
    def _install_from_url(self,
                          name,
                          url,
                          requirements=None,
                          sha1=None,
                          track=False):
        tmp_dir = mkdtemp("-package", self.TMP_FOLDER_PREFIX, self.package_dir)
        src_manifest_dir = None
        src_manifest = {"name": name, "url": url, "requirements": requirements}

        try:
            if url.startswith("file://"):
                _url = url[7:]
                if isfile(_url):
                    self.unpack(_url, tmp_dir)
                else:
                    util.rmtree_(tmp_dir)
                    shutil.copytree(_url, tmp_dir)
            elif url.startswith(("http://", "https://")):
                dlpath = self.download(url, tmp_dir, sha1)
                assert isfile(dlpath)
                self.unpack(dlpath, tmp_dir)
                os.remove(dlpath)
            else:
                vcs = VCSClientFactory.newClient(tmp_dir, url)
                assert vcs.export()
                src_manifest_dir = vcs.storage_dir
                src_manifest['version'] = vcs.get_current_revision()

            _tmp_dir = tmp_dir
            if not src_manifest_dir:
                _tmp_dir = self.find_pkg_root(tmp_dir)
                src_manifest_dir = join(_tmp_dir, ".pio")

            # write source data to a special manifest
            if track:
                self._update_src_manifest(src_manifest, src_manifest_dir)

            return self._install_from_tmp_dir(_tmp_dir, requirements)
        finally:
            if isdir(tmp_dir):
                util.rmtree_(tmp_dir)
        return None
Пример #27
0
    def _install_from_url(self,
                          name,
                          url,
                          requirements=None,
                          sha1=None,
                          track=False):
        tmp_dir = mkdtemp("-package", self.TMP_FOLDER_PREFIX, self.package_dir)
        src_manifest_dir = None
        src_manifest = {"name": name, "url": url, "requirements": requirements}

        try:
            if url.startswith("file://"):
                _url = url[7:]
                if isfile(_url):
                    self.unpack(_url, tmp_dir)
                else:
                    util.rmtree_(tmp_dir)
                    shutil.copytree(_url, tmp_dir)
            elif url.startswith(("http://", "https://")):
                dlpath = self.download(url, tmp_dir, sha1)
                assert isfile(dlpath)
                self.unpack(dlpath, tmp_dir)
                os.remove(dlpath)
            else:
                vcs = VCSClientFactory.newClient(tmp_dir, url)
                assert vcs.export()
                src_manifest_dir = vcs.storage_dir
                src_manifest['version'] = vcs.get_current_revision()

            _tmp_dir = tmp_dir
            if not src_manifest_dir:
                _tmp_dir = self.find_pkg_root(tmp_dir)
                src_manifest_dir = join(_tmp_dir, ".pio")

            # write source data to a special manifest
            if track:
                self._update_src_manifest(src_manifest, src_manifest_dir)

            return self._install_from_tmp_dir(_tmp_dir, requirements)
        finally:
            if isdir(tmp_dir):
                util.rmtree_(tmp_dir)
        return None
Пример #28
0
def _clean_pioenvs_dir(pioenvs_dir):
    structhash_file = join(pioenvs_dir, "structure.hash")
    proj_hash = calculate_project_hash()

    # if project's config is modified
    if isdir(pioenvs_dir) and getmtime(join(util.get_project_dir(), "platformio.ini")) > getmtime(pioenvs_dir):
        util.rmtree_(pioenvs_dir)

    # check project structure
    if isdir(pioenvs_dir) and isfile(structhash_file):
        with open(structhash_file) as f:
            if f.read() == proj_hash:
                return
        util.rmtree_(pioenvs_dir)

    if not isdir(pioenvs_dir):
        makedirs(pioenvs_dir)

    with open(structhash_file, "w") as f:
        f.write(proj_hash)
Пример #29
0
    def patched_clean_build_dir(build_dir):
        structhash_file = join(build_dir, "structure.hash")
        platformio_ini = join(get_project_dir(), "platformio.ini")

        # if project's config is modified
        if isdir(build_dir) and getmtime(platformio_ini) > getmtime(build_dir):
            util.rmtree_(build_dir)

        if not isdir(build_dir):
            makedirs(build_dir)

        proj_hash = run.calculate_project_hash()

        # check project structure
        if isdir(build_dir) and isfile(structhash_file):
            with open(structhash_file) as f:
                if f.read() == proj_hash:
                    return

        with open(structhash_file, "w") as f:
            f.write(proj_hash)
Пример #30
0
def device_monitor(ctx, **kwargs):
    def _tx_target(sock_dir):
        try:
            pioplus_call(sys.argv[1:] + ["--sock", sock_dir])
        except exception.ReturnErrorCode:
            pass

    sock_dir = mkdtemp(suffix="pioplus")
    sock_file = join(sock_dir, "sock")
    try:
        t = threading.Thread(target=_tx_target, args=(sock_dir, ))
        t.start()
        while t.is_alive() and not isfile(sock_file):
            sleep(0.1)
        if not t.is_alive():
            return
        kwargs['port'] = open(sock_file).read()
        ctx.invoke(cmd_device_monitor, **kwargs)
        t.join(2)
    finally:
        util.rmtree_(sock_dir)
Пример #31
0
def clean_build_dir(build_dir):
    # remove legacy ".pioenvs" folder
    legacy_build_dir = join(get_project_dir(), ".pioenvs")
    if isdir(legacy_build_dir) and legacy_build_dir != build_dir:
        util.rmtree_(legacy_build_dir)

    structhash_file = join(build_dir, "structure.hash")
    proj_hash = calculate_project_hash()

    # if project's config is modified
    if (isdir(build_dir) and getmtime(join(
            get_project_dir(), "platformio.ini")) > getmtime(build_dir)):
        util.rmtree_(build_dir)

    # check project structure
    if isdir(build_dir) and isfile(structhash_file):
        with open(structhash_file) as f:
            if f.read() == proj_hash:
                return
        util.rmtree_(build_dir)

    if not isdir(build_dir):
        makedirs(build_dir)

    with open(structhash_file, "w") as f:
        f.write(proj_hash)
Пример #32
0
def device_monitor(ctx, **kwargs):

    def _tx_target(sock_dir):
        try:
            pioplus_call(sys.argv[1:] + ["--sock", sock_dir])
        except exception.ReturnErrorCode:
            pass

    sock_dir = mkdtemp(suffix="pioplus")
    sock_file = join(sock_dir, "sock")
    try:
        t = threading.Thread(target=_tx_target, args=(sock_dir, ))
        t.start()
        while t.is_alive() and not isfile(sock_file):
            sleep(0.1)
        if not t.is_alive():
            return
        kwargs['port'] = open(sock_file).read()
        ctx.invoke(cmd_device_monitor, **kwargs)
        t.join(2)
    finally:
        util.rmtree_(sock_dir)
Пример #33
0
def _clean_pioenvs_dir(pioenvs_dir):
    structhash_file = join(pioenvs_dir, "structure.hash")
    proj_hash = calculate_project_hash()

    # if project's config is modified
    if (isdir(pioenvs_dir)
            and getmtime(join(util.get_project_dir(),
                              "platformio.ini")) > getmtime(pioenvs_dir)):
        util.rmtree_(pioenvs_dir)

    # check project structure
    if isdir(pioenvs_dir) and isfile(structhash_file):
        with open(structhash_file) as f:
            if f.read() == proj_hash:
                return
        util.rmtree_(pioenvs_dir)

    if not isdir(pioenvs_dir):
        makedirs(pioenvs_dir)

    with open(structhash_file, "w") as f:
        f.write(proj_hash)
Пример #34
0
    def _install_from_tmp_dir(self, tmp_dir, requirements=None):
        tmpmanifest = self.load_manifest(tmp_dir)
        assert set(["name", "version"]) <= set(tmpmanifest.keys())
        name = tmpmanifest["name"]
        pkg_dir = join(self.package_dir, name)
        if "id" in tmpmanifest:
            name += "_ID%d" % tmpmanifest["id"]
            pkg_dir = join(self.package_dir, name)

        # package should satisfy requirements
        if requirements:
            mismatch_error = "Package version %s doesn't satisfy requirements %s" % (
                tmpmanifest["version"],
                requirements,
            )
            try:
                reqspec = semantic_version.Spec(requirements)
                tmpmanver = semantic_version.Version(tmpmanifest["version"], partial=True)
                assert tmpmanver in reqspec, mismatch_error

                if self.manifest_exists(pkg_dir):
                    curmanifest = self.load_manifest(pkg_dir)
                    curmanver = semantic_version.Version(curmanifest["version"], partial=True)
                    # if current package version < new package, backup it
                    if tmpmanver > curmanver:
                        os.rename(pkg_dir, join(self.package_dir, "%s@%s" % (name, curmanifest["version"])))
                    elif tmpmanver < curmanver:
                        pkg_dir = join(self.package_dir, "%s@%s" % (name, tmpmanifest["version"]))
            except ValueError:
                assert tmpmanifest["version"] == requirements, mismatch_error

        # remove previous/not-satisfied package
        if isdir(pkg_dir):
            util.rmtree_(pkg_dir)
        os.rename(tmp_dir, pkg_dir)
        assert isdir(pkg_dir)
        return pkg_dir
Пример #35
0
def test_run(pioproject_dir):
    if isdir(join(pioproject_dir, ".pioenvs")):
        util.rmtree_(join(pioproject_dir, ".pioenvs"))

    result = util.exec_command(
        ["platformio", "--force", "run", "--project-dir", pioproject_dir]
    )
    if result['returncode'] != 0:
        pytest.fail(result)

    # check .elf file
    pioenvs_dir = join(pioproject_dir, ".pioenvs")
    for item in listdir(pioenvs_dir):
        if not isdir(item):
            continue
        assert isfile(join(pioenvs_dir, item, "firmware.elf"))
        # check .hex or .bin files
        firmwares = []
        for ext in ("bin", "hex"):
            firmwares += glob(join(pioenvs_dir, item, "firmware*.%s" % ext))
        if not firmwares:
            pytest.fail("Missed firmware file")
        for firmware in firmwares:
            assert getsize(firmware) > 0
Пример #36
0
    def uninstall(self, name, requirements=None, trigger_event=True):
        name, requirements, url = self.parse_pkg_name(name, requirements)
        package_dir = self.get_package_dir(name, requirements, url)
        if not package_dir:
            click.secho("%s @ %s is not installed" % (name, requirements or "*"), fg="yellow")
            return

        manifest = self.load_manifest(package_dir)
        click.echo(
            "Uninstalling %s @ %s: \t" % (click.style(manifest["name"], fg="cyan"), manifest["version"]), nl=False
        )

        if isdir(package_dir):
            if islink(package_dir):
                os.unlink(package_dir)
            else:
                util.rmtree_(package_dir)

        click.echo("[%s]" % click.style("OK", fg="green"))

        self.reset_cache()
        if trigger_event:
            telemetry.on_event(category=self.__class__.__name__, action="Uninstall", label=manifest["name"])
        return True
Пример #37
0
    def _install_from_url(self, name, url, requirements=None, sha1=None):
        pkg_dir = None
        tmp_dir = mkdtemp("-package", "installing-", self.package_dir)

        try:
            if url.startswith("file://"):
                url = url[7:]
                if isfile(url):
                    self.unpack(url, tmp_dir)
                else:
                    util.rmtree_(tmp_dir)
                    copytree(url, tmp_dir)
            elif url.startswith(("http://", "https://")):
                dlpath = self.download(url, tmp_dir, sha1)
                assert isfile(dlpath)
                self.unpack(dlpath, tmp_dir)
                os.remove(dlpath)
            else:
                vcs = VCSClientFactory.newClient(tmp_dir, url)
                assert vcs.export()
                with open(join(vcs.storage_dir, self.VCS_MANIFEST_NAME),
                          "w") as fp:
                    json.dump(
                        {
                            "name": name,
                            "version": vcs.get_current_revision(),
                            "url": url,
                            "requirements": requirements
                        }, fp)

            pkg_dir = self.check_pkg_structure(tmp_dir)
            pkg_dir = self._install_from_tmp_dir(pkg_dir, requirements)
        finally:
            if isdir(tmp_dir):
                util.rmtree_(tmp_dir)
        return pkg_dir
Пример #38
0
 def clean(self):
     if isdir(self.cache_dir):
         util.rmtree_(self.cache_dir)
Пример #39
0
def _clean_dir(dirpath):
    util.rmtree_(dirpath)
    makedirs(dirpath)
Пример #40
0
    def _install_from_tmp_dir(  # pylint: disable=too-many-branches
            self, tmp_dir, requirements=None):
        tmp_manifest = self.load_manifest(tmp_dir)
        assert set(["name", "version"]) <= set(tmp_manifest.keys())

        pkg_dirname = self.get_install_dirname(tmp_manifest)
        pkg_dir = join(self.package_dir, pkg_dirname)
        cur_manifest = self.load_manifest(pkg_dir)

        tmp_semver = self.parse_semver_version(tmp_manifest['version'])
        cur_semver = None
        if cur_manifest:
            cur_semver = self.parse_semver_version(cur_manifest['version'])

        # package should satisfy requirements
        if requirements:
            mismatch_error = (
                "Package version %s doesn't satisfy requirements %s" %
                (tmp_manifest['version'], requirements))
            try:
                assert tmp_semver and tmp_semver in semantic_version.Spec(
                    requirements), mismatch_error
            except (AssertionError, ValueError):
                assert tmp_manifest['version'] == requirements, mismatch_error

        # check if package already exists
        if cur_manifest:
            # 0-overwrite, 1-rename, 2-fix to a version
            action = 0
            if "__src_url" in cur_manifest:
                if cur_manifest['__src_url'] != tmp_manifest.get("__src_url"):
                    action = 1
            elif "__src_url" in tmp_manifest:
                action = 2
            else:
                if tmp_semver and (not cur_semver or tmp_semver > cur_semver):
                    action = 1
                elif tmp_semver and cur_semver and tmp_semver != cur_semver:
                    action = 2

            # rename
            if action == 1:
                target_dirname = "%s@%s" % (pkg_dirname,
                                            cur_manifest['version'])
                if "__src_url" in cur_manifest:
                    target_dirname = "%s@src-%s" % (
                        pkg_dirname, hashlib.md5(
                            cur_manifest['__src_url']).hexdigest())
                os.rename(pkg_dir, join(self.package_dir, target_dirname))
            # fix to a version
            elif action == 2:
                target_dirname = "%s@%s" % (pkg_dirname,
                                            tmp_manifest['version'])
                if "__src_url" in tmp_manifest:
                    target_dirname = "%s@src-%s" % (
                        pkg_dirname, hashlib.md5(
                            tmp_manifest['__src_url']).hexdigest())
                pkg_dir = join(self.package_dir, target_dirname)

        # remove previous/not-satisfied package
        if isdir(pkg_dir):
            util.rmtree_(pkg_dir)
        os.rename(tmp_dir, pkg_dir)
        assert isdir(pkg_dir)
        self.cache_reset()
        return pkg_dir
Пример #41
0
def _clean_dir(dirpath):
    util.rmtree_(dirpath)
    makedirs(dirpath)
Пример #42
0
 def clean(self):
     if not self.cache_dir or not isdir(self.cache_dir):
         return
     util.rmtree_(self.cache_dir)
Пример #43
0
 def clean(self):
     if not self.cache_dir or not isdir(self.cache_dir):
         return
     util.rmtree_(self.cache_dir)
Пример #44
0
 def clean(self):
     if self.cache_dir and isdir(self.cache_dir):
         util.rmtree_(self.cache_dir)
Пример #45
0
    def _install_from_tmp_dir(  # pylint: disable=too-many-branches
            self, tmp_dir, requirements=None):
        tmp_manifest = self.load_manifest(tmp_dir)
        assert set(["name", "version"]) <= set(tmp_manifest.keys())

        pkg_dirname = self.get_install_dirname(tmp_manifest)
        pkg_dir = join(self.package_dir, pkg_dirname)
        cur_manifest = self.load_manifest(pkg_dir)

        tmp_semver = self.parse_semver_version(tmp_manifest['version'])
        cur_semver = None
        if cur_manifest:
            cur_semver = self.parse_semver_version(cur_manifest['version'])

        # package should satisfy requirements
        if requirements:
            mismatch_error = (
                "Package version %s doesn't satisfy requirements %s" %
                (tmp_manifest['version'], requirements))
            try:
                assert tmp_semver and tmp_semver in self.parse_semver_spec(
                    requirements, raise_exception=True), mismatch_error
            except (AssertionError, ValueError):
                assert tmp_manifest['version'] == requirements, mismatch_error

        # check if package already exists
        if cur_manifest:
            # 0-overwrite, 1-rename, 2-fix to a version
            action = 0
            if "__src_url" in cur_manifest:
                if cur_manifest['__src_url'] != tmp_manifest.get("__src_url"):
                    action = 1
            elif "__src_url" in tmp_manifest:
                action = 2
            else:
                if tmp_semver and (not cur_semver or tmp_semver > cur_semver):
                    action = 1
                elif tmp_semver and cur_semver and tmp_semver != cur_semver:
                    action = 2

            # rename
            if action == 1:
                target_dirname = "%s@%s" % (pkg_dirname,
                                            cur_manifest['version'])
                if "__src_url" in cur_manifest:
                    target_dirname = "%s@src-%s" % (
                        pkg_dirname, hashlib.md5(
                            cur_manifest['__src_url']).hexdigest())
                shutil.move(pkg_dir, join(self.package_dir, target_dirname))
            # fix to a version
            elif action == 2:
                target_dirname = "%s@%s" % (pkg_dirname,
                                            tmp_manifest['version'])
                if "__src_url" in tmp_manifest:
                    target_dirname = "%s@src-%s" % (
                        pkg_dirname, hashlib.md5(
                            tmp_manifest['__src_url']).hexdigest())
                pkg_dir = join(self.package_dir, target_dirname)

        # remove previous/not-satisfied package
        if isdir(pkg_dir):
            util.rmtree_(pkg_dir)
        shutil.move(tmp_dir, pkg_dir)
        assert isdir(pkg_dir)
        self.cache_reset()
        return pkg_dir