Exemplo n.º 1
0
    def new_from_dir(path, remote_url=None):
        assert os.path.isdir(path), "Invalid directory %s" % path

        type_from_uri = ManifestFileType.from_uri(
            remote_url) if remote_url else None
        if type_from_uri and os.path.isfile(os.path.join(path, type_from_uri)):
            return ManifestParserFactory.new(
                get_file_contents(os.path.join(path, type_from_uri)),
                type_from_uri,
                remote_url=remote_url,
                package_dir=path,
            )

        file_order = [
            ManifestFileType.PLATFORM_JSON,
            ManifestFileType.LIBRARY_JSON,
            ManifestFileType.LIBRARY_PROPERTIES,
            ManifestFileType.MODULE_JSON,
            ManifestFileType.PACKAGE_JSON,
        ]
        for t in file_order:
            if not os.path.isfile(os.path.join(path, t)):
                continue
            return ManifestParserFactory.new(
                get_file_contents(os.path.join(path, t)),
                t,
                remote_url=remote_url,
                package_dir=path,
            )
        raise ManifestParserError(
            "Unknown manifest file type in %s directory" % path)
Exemplo n.º 2
0
    def get_used_frameworks(env, path):
        if any(
                isfile(join(path, fname))
                for fname in ("library.properties", "keywords.txt")):
            return ["arduino"]

        if isfile(join(path, "module.json")):
            return ["mbed"]

        include_re = re.compile(r'^#include\s+(<|")(Arduino|mbed)\.h(<|")',
                                flags=re.MULTILINE)

        # check source files
        for root, _, files in os.walk(path, followlinks=True):
            if "mbed_lib.json" in files:
                return ["mbed"]
            for fname in files:
                if not fs.path_endswith_ext(
                        fname, piotool.SRC_BUILD_EXT + piotool.SRC_HEADER_EXT):
                    continue
                content = fs.get_file_contents(join(root, fname))
                if not content:
                    continue
                if "Arduino.h" in content and include_re.search(content):
                    return ["arduino"]
                if "mbed.h" in content and include_re.search(content):
                    return ["mbed"]
        return []
Exemplo n.º 3
0
def device_monitor(ctx, **kwargs):
    project_options = {}
    try:
        with fs.cd(kwargs["project_dir"]):
            project_options = device.get_project_options(kwargs["environment"])
        kwargs = device.apply_project_monitor_options(kwargs, project_options)
    except NotPlatformIOProjectError:
        pass

    kwargs["baud"] = kwargs["baud"] or 9600

    def _tx_target(sock_dir):
        pioplus_argv = ["remote", "device", "monitor"]
        pioplus_argv.extend(device.options_to_argv(kwargs, project_options))
        pioplus_argv.extend(["--sock", sock_dir])
        try:
            pioplus_call(pioplus_argv)
        except exception.ReturnErrorCode:
            pass

    sock_dir = mkdtemp(suffix="pioplus")
    sock_file = os.path.join(sock_dir, "sock")
    try:
        t = threading.Thread(target=_tx_target, args=(sock_dir, ))
        t.start()
        while t.is_alive() and not os.path.isfile(sock_file):
            sleep(0.1)
        if not t.is_alive():
            return
        kwargs["port"] = fs.get_file_contents(sock_file)
        ctx.invoke(device.device_monitor, **kwargs)
        t.join(2)
    finally:
        fs.rmtree(sock_dir)
Exemplo n.º 4
0
 def new_from_file(path, remote_url=False):
     if not path or not os.path.isfile(path):
         raise ManifestParserError("Manifest file does not exist %s" % path)
     for t in get_class_attributes(ManifestFileType).values():
         if path.endswith(t):
             return ManifestParserFactory.new(get_file_contents(path), t,
                                              remote_url)
     raise ManifestParserError("Unknown manifest file type %s" % path)
Exemplo n.º 5
0
 def new_from_file(path, remote_url=False):
     if not path or not os.path.isfile(path):
         raise UnknownManifestError("Manifest file does not exist %s" % path)
     type_from_uri = ManifestFileType.from_uri(path)
     if not type_from_uri:
         raise UnknownManifestError("Unknown manifest file type %s" % path)
     return ManifestParserFactory.new(
         get_file_contents(path, encoding="utf8"), type_from_uri, remote_url
     )
Exemplo n.º 6
0
 def process(self, contents):
     out_file = self._main_ino + ".cpp"
     assert self._gcc_preprocess(contents, out_file)
     contents = fs.get_file_contents(out_file)
     contents = self._join_multiline_strings(contents)
     fs.write_file_contents(
         out_file, self.append_prototypes(contents), errors="backslashreplace"
     )
     return out_file
Exemplo n.º 7
0
    def new_from_dir(path, remote_url=None):
        assert os.path.isdir(path), "Invalid directory %s" % path

        type_from_uri = ManifestFileType.from_uri(remote_url) if remote_url else None
        if type_from_uri and os.path.isfile(os.path.join(path, type_from_uri)):
            return ManifestParserFactory.new(
                get_file_contents(os.path.join(path, type_from_uri), encoding="utf8"),
                type_from_uri,
                remote_url=remote_url,
                package_dir=path,
            )

        type_from_dir = ManifestFileType.from_dir(path)
        if not type_from_dir:
            raise UnknownManifestError(
                "Unknown manifest file type in %s directory" % path
            )
        return ManifestParserFactory.new(
            get_file_contents(os.path.join(path, type_from_dir), encoding="utf8"),
            type_from_dir,
            remote_url=remote_url,
            package_dir=path,
        )
Exemplo n.º 8
0
    def merge(self, nodes):
        assert nodes
        lines = []
        for node in nodes:
            contents = fs.get_file_contents(node.get_path())
            _lines = ['# 1 "%s"' % node.get_path().replace("\\", "/"), contents]
            if self.is_main_node(contents):
                lines = _lines + lines
                self._main_ino = node.get_path()
            else:
                lines.extend(_lines)

        if not self._main_ino:
            self._main_ino = nodes[0].get_path()

        return "\n".join(["#include <Arduino.h>"] + lines) if lines else None
Exemplo n.º 9
0
def clean_build_dir(build_dir, config):
    # remove legacy ".pioenvs" folder
    legacy_build_dir = join(get_project_dir(), ".pioenvs")
    if isdir(legacy_build_dir) and legacy_build_dir != build_dir:
        fs.rmtree(legacy_build_dir)

    checksum_file = join(build_dir, "project.checksum")
    checksum = compute_project_checksum(config)

    if isdir(build_dir):
        # check project structure
        if isfile(checksum_file) and fs.get_file_contents(checksum_file) == checksum:
            return
        fs.rmtree(build_dir)

    makedirs(build_dir)
    fs.write_file_contents(checksum_file, checksum)
Exemplo n.º 10
0
def is_prog_obsolete(prog_path):
    prog_hash_path = prog_path + ".sha1"
    if not isfile(prog_path):
        return True
    shasum = sha1()
    with open(prog_path, "rb") as fp:
        while True:
            data = fp.read(1024)
            if not data:
                break
            shasum.update(data)
    new_digest = shasum.hexdigest()
    old_digest = (fs.get_file_contents(prog_hash_path)
                  if isfile(prog_hash_path) else None)
    if new_digest == old_digest:
        return False
    fs.write_file_contents(prog_hash_path, new_digest)
    return True
Exemplo n.º 11
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"] = fs.get_file_contents(sock_file)
        ctx.invoke(cmd_device_monitor, **kwargs)
        t.join(2)
    finally:
        fs.rmtree(sock_dir)
Exemplo n.º 12
0
 def _render_tpl(tpl_path, tpl_vars):
     return bottle.template(fs.get_file_contents(tpl_path), **tpl_vars)
Exemplo n.º 13
0
 def request_content(self, uri, data=None, headers=None, cache_valid=None):
     if uri.startswith("http"):
         return self.fetch_content(uri, data, headers, cache_valid)
     if os.path.isfile(uri):
         return fs.get_file_contents(uri, encoding="utf8")
     return None