Esempio n. 1
0
    def parse(self, value, kic_path=None):
        command_value = value[9:].strip()
        if not command_value:
            return {}

        paths = []
        if command_value[0] == "[" and command_value[-1] == "]":
            try:
                paths = simplejson.loads(command_value.replace("\\", "/"))
            except:
                pass

        if not paths:
            # if path has whitespace, quote it. and support multi paths
            paths = get_content_parts(command_value)

        if len(paths) == 1:
            raise Exception("target not found")

        args = []

        for path in paths[:-1]:
            # the <src> allow remote file URL
            if "http://" in path or "https://" in path:
                urlinfo = urlparse.urlparse(path)
                filename = os.path.split(urlinfo["path"])[1]

                if (
                    ":" in filename or
                    "?" in filename or
                    "*" in filename or
                    "\"" in filename or
                    "<" in filename or
                    ">" in filename or
                    "/" in filename or
                    "\\" in filename or
                    "|" in filename
                ):
                    raise Exception("the filename has special character")

                # if <dest> ends with a trailing slash /, it will be considered a directory
                if paths[-1][-1] == "/":
                    to = "%s%s" % (paths[-1], filename)
                    args.append({"from": path, "to": to})
                elif len(paths) > 2:
                    to = "%s/%s" % (paths[-1], filename)
                    args.append({"from": path, "to": to})
                else:
                    args.append({"from": path, "to": paths[-1]})
                continue

            args.append({"from": path, "to": paths[-1]})

        return {
            "action": "ADDONRUN",
            "args": args,
            "images": [],
            "files": []
        }
Esempio n. 2
0
    def _parse_env_value(self, content):
        has_backslashes = True if "\ " in content else False
        parts = get_content_parts(content if not has_backslashes else content.replace("\ ", "/_\\"))

        envs = []
        last_key = None

        for part in parts:
            if part == "=":
                continue

            if last_key is None:
                if part[-1] == "=":
                    last_key = part[:-1]

                elif part[0] == "=":
                    last_key = part
                elif "=" in part:
                    first_equal_pos = part.find("=")
                    if len(re.findall("[^\w_]", part[:first_equal_pos])) > 0:
                        raise Exception("invalid env variable name")

                    v = part[first_equal_pos + 1:]
                    if has_backslashes and "/_\\" in v:
                        v = v.replace("/_\\", " ")
                    envs.append(
                        {
                            "key": part[:first_equal_pos],
                            "value": v
                        }
                    )
                else:
                    last_key = part
            else:
                if len(re.findall("[^\w_]", last_key)) > 0:
                    raise Exception("invalid env variable name")

                # support backslashes
                if has_backslashes and "/_\\" in part:
                    part = part.replace("/_\\", " ")
                envs.append(
                    {
                        "key": last_key,
                        "value": part
                    }
                )
                last_key = None
        return envs
Esempio n. 3
0
    def parse(self, value, kic_path=None):
        command_value = value[4:].strip()
        if not command_value:
            return {}

        paths = []
        if command_value[0] == "[" and command_value[-1] == "]":
            try:
                paths = simplejson.loads(command_value.replace("\\", "\\\\"))
            except:
                raise Exception("invalid json array")

        if not paths:
            # if path has whitespace, quote it. and support multi paths
            paths = get_content_parts(command_value)

        if len(paths) == 1:
            raise Exception("target not found")

        args = []
        files = []

        for path in paths[:-1]:
            # the <src> allow remote file URL
            if "http://" in path or "https://" in path:
                urlinfo = urlparse.urlparse(path)
                filename = os.path.split(urlinfo["path"])[1]

                if (
                    ":" in filename or
                    "?" in filename or
                    "*" in filename or
                    "\"" in filename or
                    "<" in filename or
                    ">" in filename or
                    "/" in filename or
                    "\\" in filename or
                    "|" in filename
                ):
                    raise Exception("the filename has special character")

                files.append(
                    {"name": filename, "url": path}
                )

                # if <dest> ends with a trailing slash /, it will be considered a directory
                if paths[-1][-1] == "/":
                    to = "%s%s" % (paths[-1], filename)
                    args.append({"from": filename, "to": to})
                else:
                    args.append({"from": filename, "to": paths[-1]})
                continue

            if not os.path.isdir(path) and not os.path.isfile(path):
                kic_folder = os.path.dirname(kic_path)
                path = os.path.join(kic_folder, path)

            if not os.path.exists(path):
                raise Exception("%s not found" % path)

            # the <src> allow directory
            if os.path.isdir(path):
                files_info = get_files_info(path)

                files.extend(files_info)

                for file_info in files_info:
                    # if <dest> ends with a trailing slash /, it will be considered a directory
                    if paths[-1][-1] == "/":
                        to = "%s%s" % (paths[-1], file_info["name"])
                        args.append({"from": file_info["name"], "to": to})
                    elif len(files_info) > 1:
                        to = "%s/%s" % (paths[-1], file_info["name"])
                        args.append({"from": file_info["name"], "to": to})
                    else:
                        args.append({"from": file_info["name"], "to": paths[-1]})
            elif os.path.isfile(path):
                filedir, filename = os.path.split(path)
                files.append(
                    {"name": filename, "url": path}
                )

                # if <dest> ends with a trailing slash /, it will be considered a directory
                if paths[-1][-1] == "/":
                    to = "%s%s" % (paths[-1], filename)
                    args.append({"from": filename, "to": to})
                elif len(paths) > 2:
                    to = "%s/%s" % (paths[-1], filename)
                    args.append({"from": filename, "to": to})
                else:
                    args.append({"from": filename, "to": paths[-1]})

        return {
            "action": "ADD",
            "args": args,
            "images": [],
            "files": files
        }