Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _optenv(self, parts):
if len(parts) != 2:
raise SubstitutionError("optenv expects at least one argument")
args = parts[1].split(None, 1)
self.env_depends.add(args[0])
default = args[1] if len(args) == 2 else ""
return self.environment.get(parts[1], default)
def _analyse_tree(self, tree, scope, sub):
for tag in tree.children:
if tag.tag == "error":
self.errors.append(tag.text)
continue
try:
condition = self._condition(tag.condition, sub,
tag.line, tag.column)
if condition is False:
continue
handler = getattr(self, "_" + tag.tag + "_tag")
handler(tag, condition, scope, sub)
except (ConfigurationError, SubstitutionError) as e:
self.errors.append(e.value)
def to_int(self, value):
try:
return int(value)
except ValueError as e:
raise SubstitutionError("invalid int value: " + value)
def _arg(self, parts):
if len(parts) != 2:
raise SubstitutionError("arg takes exactly one argument")
name = parts[1]
if name in self.arguments:
value = self.arguments[name]
if value is None or isinstance(value, UnresolvedValue):
return ("arg", name)
return value
raise SubstitutionError("undeclared arg: " + name)
def _arg(self, parts):
if len(parts) != 2:
raise SubstitutionError("arg takes exactly one argument")
name = parts[1]
if name in self.arguments:
value = self.arguments[name]
if value is None or isinstance(value, UnresolvedValue):
return ("arg", name)
return value
raise SubstitutionError("undeclared arg: " + name)
def _env(self, parts):
if len(parts) != 2:
raise SubstitutionError("env takes exactly one argument")
self.env_depends.add(parts[1])
return self.environment.get(parts[1], tuple(parts))
def __getitem__(self, key):
try:
return self._functions[key]
except KeyError:
if key in self._args:
value = self._args[key]
if value is None or isinstance(value, UnresolvedValue):
return ("arg", key)
return _convert_value(value)
raise SubstitutionError("undeclared arg: " + key)
def _find(self, parts):
if len(parts) != 2:
raise SubstitutionError("find takes exactly one argument")
name = parts[1]
self.pkg_depends.add(name)
try:
package = self.packages.get("package:" + name)
except KeyError:
package = None
if package:
if package.path:
return package.path
return ("find", name)
raise SubstitutionError("unknown package: " + name)
value = parser.sub("$$(arg test)$")
assert value == "$value$"
parser.environment["TEST"] = "value"
value = parser.sub("$(env TEST)")
assert value == "value"
value = parser.sub("$(optenv TEST)")
assert value == "value"
try:
parser.sub("$(arg $(arg name))")
assert False
except SubstitutionError as e:
pass
try:
parser.sub("$($)")
assert False
except SubstitutionError as e:
pass
try:
parser.sub("va$(eval 'lue')")
assert False
except SubstitutionError as e:
pass
try:
parser.sub("value$(arg name)$(env VAR)")
assert False
except SubstitutionError as e:
pass