def check(self, _file, stash):
     res = []
     for item in stash.GetItemsFor(filename=_file,
                                   classifier=Include.CLASSIFIER):
         if item.Statement == "include":
             _path = expand_term(stash, _file, item.IncName)
             if not find_local_or_in_layer(_path, os.path.dirname(_file)):
                 res += self.finding(
                     item.Origin, item.InFileLine,
                     self.Msg.replace("{FILE}", item.IncName))
     return res
Exemple #2
0
 def check(self, _file, stash):
     res = []
     _items = stash.GetItemsFor(filename=_file, classifier=Variable.CLASSIFIER,
                                attribute=Variable.ATTR_VAR, attributeValue="LIC_FILES_CHKSUM")
     _var_whitelist = ["${WORKDIR}", "${S}", "${B}"]
     for i in _items:
         components = get_scr_components(expand_term(stash, _file, i.VarValueStripped))
         if any(components) and components["scheme"] == "file":
             _clean = components["src"]
             for x in _var_whitelist:
                 _clean = _clean.replace(x, "")
             if "${" in _clean:
                 res += self.finding(i.Origin, i.InFileLine)
     return res
Exemple #3
0
 def check(self, _file, stash):
     res = []
     items = stash.GetItemsFor(filename=_file,
                               classifier=Variable.CLASSIFIER,
                               attribute=Variable.ATTR_VAR,
                               attributeValue="inherit")
     for i in items:
         for subi in [
                 expand_term(stash, _file, x) for x in i.get_items()
                 if x and x != INLINE_BLOCK
         ]:
             if not re.match(r"^[A-Za-z0-9_.-]+$", subi):
                 res += self.finding(i.Origin, i.InFileLine,
                                     self.Msg.replace("{INH}", subi))
     return res
Exemple #4
0
def get_items(stash, _file, lineOffset=0):
    res = []
    __regex_var = r"^(?P<varname>([A-Z0-9a-z_-]|\$|\{|\})+?)(\[(?P<ident>(\w|-)+)\])*(?P<varop>(\s|\t)*(\+|\?|\:|\.)*=(\+|\.)*(\s|\t)*)(?P<varval>.*)"
    __regex_func = r"^((?P<py>python)\s+|(?P<fr>fakeroot\s+))*(?P<func>[\w\.\-\+\{\}\$]+)?\s*\(\s*\)\s*\{(?P<funcbody>.*)\s*\}"
    __regex_inherit = r"^.*?inherit(\s+|\t+)(?P<inhname>.+)"
    __regex_comments = r"^(\s|\t)*#+\s*(?P<body>.*)"
    __regex_python = r"^(\s*|\t*)def(\s+|\t+)(?P<funcname>[a-z0-9_]+)(\s*|\t*)\(.*\)\:"
    __regex_include = r"^(\s*|\t*)(?P<statement>include|require)(\s+|\t+)(?P<incname>[A-za-z0-9\-\./\$\{\}]+)"
    __regex_addtask = r"^(\s*|\t*)addtask\s+(?P<func>\w+)\s*((before\s*(?P<before>((.*(?=after))|(.*))))|(after\s*(?P<after>((.*(?=before))|(.*)))))*"
    __regex_taskass = r"^(\s*|\t*)(?P<func>[a-z0-9_-]+)\[(?P<ident>\w+)\](\s+|\t+)=(\s+|\t+)(?P<varval>.*)"

    _order = collections.OrderedDict([("comment", __regex_comments),
                                      ("func", __regex_func),
                                      ("inherit", __regex_inherit),
                                      ("python", __regex_python),
                                      ("include", __regex_include),
                                      ("addtask", __regex_addtask),
                                      ("taskassign", __regex_taskass),
                                      ("vars", __regex_var)])

    includeOffset = 0

    for line in prepare_lines(_file, lineOffset):
        good = False
        for k, v in _order.items():
            m = re.match(v, line["cnt"], re.MULTILINE)
            if m:
                if k == "python":
                    res.append(
                        PythonBlock(_file, line["line"] + includeOffset,
                                    line["line"] - lineOffset, line["raw"],
                                    m.group("funcname")))
                    good = True
                    break
                elif k == "vars":
                    res.append(
                        Variable(_file, line["line"] + includeOffset,
                                 line["line"] - lineOffset, line["raw"],
                                 m.group("varname"), m.group("varval"),
                                 m.group("varop"), m.group("ident")))
                    good = True
                    break
                elif k == "func":
                    res.append(
                        Function(_file, line["line"] + includeOffset,
                                 line["line"] - lineOffset, line["raw"],
                                 m.group("func"), m.group("funcbody"),
                                 m.group("py"), m.group("fr")))
                    good = True
                    break
                elif k == "comment":
                    res.append(
                        Comment(_file, line["line"] + includeOffset,
                                line["line"] - lineOffset, line["raw"]))
                    good = True
                    break
                elif k == "inherit":
                    res.append(
                        Variable(_file, line["line"] + includeOffset,
                                 line["line"] - lineOffset, line["raw"],
                                 "inherit", m.group("inhname"), "", ""))
                    good = True
                    break
                elif k == "taskassign":
                    res.append(
                        TaskAssignment(_file, line["line"] + includeOffset,
                                       line["line"] - lineOffset, line["raw"],
                                       m.group("func"), m.group("ident"),
                                       m.group("varval")))
                    good = True
                    break
                elif k == "addtask":
                    # treat the following as variables
                    if any([
                            m.group("func").startswith(x) for x in [
                                'pkg_preinst', 'pkg_postinst', 'pkg_prerm',
                                'pkg_postrm'
                            ]
                    ]):
                        continue
                    _g = m.groupdict()
                    if "before" in _g.keys():
                        _b = _g["before"]
                    else:
                        _b = ""
                    if "after" in _g.keys():
                        _a = _g["after"]
                    else:
                        _a = ""
                    res.append(
                        TaskAdd(_file, line["line"] + includeOffset,
                                line["line"] - lineOffset, line["raw"],
                                m.group("func"), _b, _a))
                    break
                elif k == "include":
                    _path = find_local_or_in_layer(
                        expand_term(stash, _file, m.group("incname")),
                        os.path.dirname(_file))
                    if _path:
                        tmp = stash.AddFile(_path,
                                            lineOffset=line["line"],
                                            forcedLink=_file)
                        if any(tmp):
                            includeOffset += max([x.InFileLine for x in tmp])
                    res.append(
                        Include(_file, line["line"], line["line"] - lineOffset,
                                line["raw"], m.group("incname"),
                                m.group("statement")))
                    good = True
                    break
        if not good:
            res.append(
                Item(_file, line["line"], line["line"] - lineOffset,
                     line["raw"]))
    return res
Exemple #5
0
    def ExpandVar(self,
                  filename=None,
                  attribute=None,
                  attributeValue=None,
                  nolink=False):
        """Expand variable to dictionary

        Args:
            filename {str} -- Full path to file (default: {None})
            attribute {str} -- class attribute name (default: {None})
            attributeValue {str} -- value of the class attribute name (default: {None})
            nolink {bool} -- Consider linked files (default: {False})

        Returns:
            {dict}: expanded variables from call + base set of variables
        """
        _res = self.GetItemsFor(filename=filename,
                                classifier=Variable.CLASSIFIER,
                                attribute=attribute,
                                attributeValue=attributeValue,
                                nolink=nolink)
        _exp = {
            "PN": guess_recipe_name(filename),
            "PV": guess_recipe_version(filename),
            "BPN": guess_base_recipe_name(filename)
        }
        _exp = {**_exp, **get_base_varset()}
        for item in sorted(_res, key=lambda x: x.Line):
            varop = item.VarOp
            name = item.VarName
            if item.Flag:
                continue
            if name not in _exp.keys():
                _exp[name] = None
            if varop in [" = ", " := "]:
                if not item.IsAppend() and "remove" not in item.SubItems:
                    _exp[name] = item.VarValueStripped
            elif varop == " ?= " and _exp[name] is None:
                _exp[name] = item.VarValueStripped
            elif varop == " ??= " and _exp[name] is None:
                _exp[name] = item.VarValueStripped
            elif varop == " += ":
                if _exp[name] is None:
                    _exp[name] = ""
                _exp[name] += " " + item.VarValueStripped
            elif varop == " =+ ":
                if _exp[name] is None:
                    _exp[name] = ""
                _exp[name] = item.VarValueStripped + " " + _exp[name]
            elif varop in [" .= "]:
                if _exp[name] is None:
                    _exp[name] = ""
                _exp[name] += item.VarValueStripped
            elif varop in [" =. "]:
                if _exp[name] is None:
                    _exp[name] = ""
                _exp[name] = item.VarValueStripped + _exp[name]
        # and now for a second run with special
        for item in sorted(_res, key=lambda x: x.Line):
            varop = item.VarOp
            name = item.VarName
            if item.Flag:
                continue
            if name not in _exp.keys():
                _exp[name] = None
                if _exp[name] is None:
                    _exp[name] = ""
                _exp[name] += item.VarValueStripped
            elif "append" in item.SubItems:
                if _exp[name] is None:
                    _exp[name] = ""
                _exp[name] += item.VarValueStripped
            elif "prepend" in item.SubItems:
                if _exp[name] is None:
                    _exp[name] = ""
                _exp[name] = item.VarValueStripped + _exp[name]
        # and now for the run with remove
        for item in sorted(_res, key=lambda x: x.Line):
            varop = item.VarOp
            name = item.VarName
            if item.Flag:
                continue
            if name not in _exp.keys():
                _exp[name] = None
            if "remove" in item.SubItems:
                if _exp[name] is None:
                    _exp[name] = ""
                _exp[name] = _exp[name].replace(item.VarValueStripped, "")
        # final run and explode the settings
        _finalexp = {}
        for k, v in _exp.items():
            _newkey = expand_term(self, filename, k)
            if _newkey not in _finalexp:
                _finalexp[_newkey] = []
            _finalexp[_newkey] += Item.safe_linesplit(
                expand_term(self, filename, v or ""))
        return _finalexp