#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
import os
import re
import shlex
import six
import sys
"""This routine adds a transform tuple to the list used
to process actions."""
# strip off transform
s = transform[10:]
# make error messages familiar
try:
except ValueError:
raise RuntimeError(_("Missing -> in transform"))
for a in attrdict:
try:
raise RuntimeError(
_("transform ({transform}) has regexp error "
"({err}) in matching clause"
# use closures to encapsulate desired operation
raise RuntimeError(
_("transform ({0}) has 'drop' operation syntax error"
operation = lambda a, m, p, f, l: None
try:
except ValueError:
raise RuntimeError(
_("transform ({0}) has 'set' operation syntax error"
if newattr == "action.hash":
else:
return action
try:
except ValueError:
raise RuntimeError(
_("transform ({0}) has 'default' operation syntax error"
return action
raise RuntimeError(_("transform ({0}) has 'abort' "
exitval = 0
msg = None
try:
except ValueError:
raise RuntimeError(_("transform ({0}) has 'exit' "
"operation syntax error: illegal exit value").format(
if msg:
try:
except ValueError:
raise RuntimeError(
_("transform ({0}) has 'add' operation syntax error"
else:
else:
return action
raise RuntimeError(
_("transform ({0}) has 'edit' operation syntax error"
raise RuntimeError(
_("transform ({0}) has 'edit' operation syntax error"
# Run args[1] (the regexp) through substitute_values() with a
# bunch of bogus values to see whether it triggers certain
# exceptions. If it does, then substitution would have
# occurred, and we can't compile the regex now, but wait until
# we can correctly run substitute_values().
try:
except (AttributeError, RuntimeError):
raise RuntimeError(
_("transform ({transform}) has 'edit' operation "
"with malformed regexp ({err})").format(
else:
replace = ""
if not val:
return action
# It's now appropriate to compile the regexp, if there
# are substitutions to be made. So do the substitution
# and compile the result.
else:
try:
for v in val
]
raise RuntimeError(
_("transform ({transform}) has edit "
"operation with replacement string regexp "
"error {err}").format(
return action
raise RuntimeError(
_("transform ({0}) has 'delete' operation syntax error"
raise RuntimeError(
_("transform ({0}) has 'delete' operation syntax error"
try:
raise RuntimeError(
_("transform ({transform}) has 'delete' operation"
"with malformed regexp ({err})").format(
if not val:
return action
try:
new_val = [
v
for v in val
]
if new_val:
else:
raise RuntimeError(
_("transform ({transform}) has delete "
"operation with replacement string regexp "
"error {err}").format(
return action
raise RuntimeError(_("transform ({0}) has 'print' "
msg = ""
else:
return action
raise RuntimeError(_("transform ({0}) has 'emit' "
msg = ""
else:
try:
raise RuntimeError(e)
else:
"""Substitute tokens in messages which can be expanded to the action's
attribute values."""
newmsg = ""
prevend = 0
assert m[1] in "({"
if m[1] == "(":
group = 1
elif m[1] == "{":
group = 2
d = {}
if tok == ";":
else:
assert(eq == "=")
else:
if d.get("noquote", None):
d["quote"] = False
if group == 2:
else:
if attrname == "pkg.manifest.lineno":
elif attrname == "pkg.manifest.filename":
elif attrname == "action.hash":
d.get("notfound", None))
elif attrname == "action.key":
d.get("notfound", None))
elif attrname == "action.name":
else:
d.get("notfound", None))
if attr is None:
attrname))
def q(s):
if " " in s or "'" in s or "\"" in s or s == "":
if "\"" not in s:
return '"{0}"'.format(s)
elif "'" not in s:
return "'{0}'".format(s)
else:
else:
return s
if not d["quote"]:
q = lambda x: x
else:
for v in attr
])
# Now see if there are any backreferences to match groups
newmsg = ""
prevend = 0
for group in (
)
), (None,))
raise RuntimeError(_("no match group {group:d} "
"(max {maxgroups:d})").format(
raise RuntimeError(_("Error\nInvalid backreference: "
"%<{ref}> refers to an unmatched string"
return newmsg
"""Return specified attribute as list;
an empty list if no such attribute exists"""
return []
return val
"""Apply all transforms to action, returning modified action
or None if action is dropped"""
comments = []
newactions = []
if verbose:
if action is None:
# skip if types are specified and none match
continue
# skip if some attrs don't exist
continue
# Check to make sure all matching attrs actually match. The
# order is effectively arbitrary, since they come from a dict.
matches = [
]
continue
# Map each pattern to its position in the original match string.
matchorder = {}
# Attributes might be quoted even if they don't need it,
# and lead to a mis-match. These three patterns are all
# safe to try. If we fail to find the match expression,
# it's probably because it used different quoting rules
# than the action code does, or from these three rules.
# It might very well be okay, so we go ahead, but these
# oddly quoted patterns will sort at the beginning, and
# backref matching may be off.
if pos != -1:
break
# Then sort the matches list by those positions.
# time to apply transform operation
try:
if verbose:
except RuntimeError as e:
if verbose:
if not action or \
break
# Any newly-created actions need to have the transforms applied, too.
newnewactions = []
for act in newactions:
if c:
newnewactions += [a for a in al if a is not None]
else:
comments = []
else:
return (comments, [None] + newnewactions)
""" implement include hierarchy """
if filename == "-":
try:
except IOError as e:
for i in includes:
try:
return f, open(f)
except IOError as e:
"""Apply macro subs defined on command line... keep applying
macros until no translations are found."""
while s and "$(" in s:
if key in s:
break # look for more substitutions
else:
break # no more substitutable tokens
return s
error_print_cb=None):
""" return the lines in the file as a list of tuples containing
(line, filename, line number); handle continuation and <include "path">
"""
ret = []
accumulate = ""
if not line: # preserve blanks
continue
continue
elif accumulate:
accumulate = ""
if line:
if not line:
continue
try:
if not ignoreincludes:
else:
else:
raise RuntimeError(
_("unknown command {0}").format(
line))
else:
except RuntimeError as e:
if error_print_cb:
error_print_cb(_("File {file}, line {line:d}: "
exception=e),
exitcode=None)
raise RuntimeError("<included from>")
f.close()
return ret
"""Print the error message or raise the actual exception if no
error printing callback specified."""
if error_cb:
else:
raise
"""Entry point for mogrify logic.
file_args: input files to be mogrified. If not provided, use stdin
instead.
ingoreincludes: whether to ignore <include ...> directives in input
files.
verbose: whether to include verbose action processing information
in mogrify output. Useful for debug.
includes: a list of directory paths used for searching include files.
macros: a list of macros for substitution.
printinfo: used to collect a list print info along processing. Could be
empty initially.
output: used to collect mogrify output. Empty initially.
error_cb: used to supply a error printing callback.
sys_supply_files: used for other systems or modules to supply
additional input files.
"""
transforms = []
try:
if file_args:
else:
if sys_supply_files:
except RuntimeError as e:
process_error(_("Error processing input arguments: {0}"
try:
lines = []
for f in infiles:
except RuntimeError as e:
raise
pkg_attrs = {}
if line is None:
if "pkg.fmri" in pkg_attrs:
None, pkg_attrs,
pkg_attrs = {}
continue
continue
# doesn't handle nested macros
else:
prepended_macro = None
try:
try:
else:
except RuntimeError as e: