userland.py revision 464
45N/A#!/usr/bin/python
45N/A#
45N/A# CDDL HEADER START
45N/A#
45N/A# The contents of this file are subject to the terms of the
45N/A# Common Development and Distribution License (the "License").
45N/A# You may not use this file except in compliance with the License.
45N/A#
45N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
45N/A# or http://www.opensolaris.org/os/licensing.
45N/A# See the License for the specific language governing permissions
45N/A# and limitations under the License.
45N/A#
45N/A# When distributing Covered Code, include this CDDL HEADER in each
45N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
45N/A# If applicable, add the following below this CDDL HEADER, with the
45N/A# fields enclosed by brackets "[]" replaced with your own identifying
45N/A# information: Portions Copyright [yyyy] [name of copyright owner]
45N/A#
45N/A# CDDL HEADER END
45N/A#
45N/A
45N/A#
84N/A# Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
45N/A#
45N/A
45N/A# Some userland consolidation specific lint checks
45N/A
45N/Aimport pkg.lint.base as base
186N/Afrom pkg.lint.engine import lint_fmri_successor
84N/Aimport pkg.elf as elf
45N/Aimport re
45N/Aimport os.path
45N/A
45N/Aclass UserlandActionChecker(base.ActionChecker):
45N/A """An opensolaris.org-specific class to check actions."""
45N/A
45N/A name = "userland.action"
45N/A
45N/A def __init__(self, config):
45N/A self.description = _(
45N/A "checks Userland packages for common content errors")
117N/A path = os.getenv('PROTO_PATH')
117N/A if path != None:
117N/A self.proto_path = path.split()
117N/A else:
117N/A self.proto_path = None
45N/A self.runpath_re = [
99N/A re.compile('^/lib(/.*)?$'),
45N/A re.compile('^/usr/'),
45N/A re.compile('^\$ORIGIN/')
45N/A ]
84N/A self.initscript_re = re.compile("^etc/(rc.|init)\.d")
186N/A
186N/A self.lint_paths = {}
186N/A self.ref_paths = {}
186N/A
45N/A super(UserlandActionChecker, self).__init__(config)
45N/A
186N/A def startup(self, engine):
186N/A """Initialize the checker with a dictionary of paths, so that we
186N/A can do link resolution.
186N/A
186N/A This is copied from the core pkglint code, but should eventually
186N/A be made common.
186N/A """
186N/A
186N/A def seed_dict(mf, attr, dic, atype=None, verbose=False):
186N/A """Updates a dictionary of { attr: [(fmri, action), ..]}
186N/A where attr is the value of that attribute from
186N/A actions of a given type atype, in the given
186N/A manifest."""
186N/A
186N/A pkg_vars = mf.get_all_variants()
186N/A
186N/A if atype:
186N/A mfg = (a for a in mf.gen_actions_by_type(atype))
186N/A else:
186N/A mfg = (a for a in mf.gen_actions())
186N/A
186N/A for action in mfg:
186N/A if atype and action.name != atype:
186N/A continue
186N/A if attr not in action.attrs:
186N/A continue
186N/A
186N/A variants = action.get_variant_template()
186N/A variants.merge_unknown(pkg_vars)
186N/A action.attrs.update(variants)
186N/A
186N/A p = action.attrs[attr]
186N/A dic.setdefault(p, []).append((mf.fmri, action))
186N/A
186N/A # construct a set of FMRIs being presented for linting, and
186N/A # avoid seeding the reference dictionary with any for which
186N/A # we're delivering new packages.
186N/A lint_fmris = {}
186N/A for m in engine.gen_manifests(engine.lint_api_inst,
186N/A release=engine.release, pattern=engine.pattern):
186N/A lint_fmris.setdefault(m.fmri.get_name(), []).append(m.fmri)
186N/A for m in engine.lint_manifests:
186N/A lint_fmris.setdefault(m.fmri.get_name(), []).append(m.fmri)
186N/A
186N/A engine.logger.debug(
186N/A _("Seeding reference action path dictionaries."))
186N/A
186N/A for manifest in engine.gen_manifests(engine.ref_api_inst,
186N/A release=engine.release):
186N/A # Only put this manifest into the reference dictionary
186N/A # if it's not an older version of the same package.
186N/A if not any(
186N/A lint_fmri_successor(fmri, manifest.fmri)
186N/A for fmri
186N/A in lint_fmris.get(manifest.fmri.get_name(), [])
186N/A ):
186N/A seed_dict(manifest, "path", self.ref_paths)
186N/A
186N/A engine.logger.debug(
186N/A _("Seeding lint action path dictionaries."))
186N/A
186N/A # we provide a search pattern, to allow users to lint a
186N/A # subset of the packages in the lint_repository
186N/A for manifest in engine.gen_manifests(engine.lint_api_inst,
186N/A release=engine.release, pattern=engine.pattern):
186N/A seed_dict(manifest, "path", self.lint_paths)
186N/A
186N/A engine.logger.debug(
186N/A _("Seeding local action path dictionaries."))
186N/A
186N/A for manifest in engine.lint_manifests:
186N/A seed_dict(manifest, "path", self.lint_paths)
186N/A
186N/A self.__merge_dict(self.lint_paths, self.ref_paths,
186N/A ignore_pubs=engine.ignore_pubs)
186N/A
186N/A def __merge_dict(self, src, target, ignore_pubs=True):
186N/A """Merges the given src dictionary into the target
186N/A dictionary, giving us the target content as it would appear,
186N/A were the packages in src to get published to the
186N/A repositories that made up target.
186N/A
186N/A We need to only merge packages at the same or successive
186N/A version from the src dictionary into the target dictionary.
186N/A If the src dictionary contains a package with no version
186N/A information, it is assumed to be more recent than the same
186N/A package with no version in the target."""
186N/A
186N/A for p in src:
186N/A if p not in target:
186N/A target[p] = src[p]
186N/A continue
186N/A
186N/A def build_dic(arr):
186N/A """Builds a dictionary of fmri:action entries"""
186N/A dic = {}
186N/A for (pfmri, action) in arr:
186N/A if pfmri in dic:
186N/A dic[pfmri].append(action)
186N/A else:
186N/A dic[pfmri] = [action]
186N/A return dic
186N/A
186N/A src_dic = build_dic(src[p])
186N/A targ_dic = build_dic(target[p])
186N/A
186N/A for src_pfmri in src_dic:
186N/A # we want to remove entries deemed older than
186N/A # src_pfmri from targ_dic.
186N/A for targ_pfmri in targ_dic.copy():
186N/A sname = src_pfmri.get_name()
186N/A tname = targ_pfmri.get_name()
186N/A if lint_fmri_successor(src_pfmri,
186N/A targ_pfmri,
186N/A ignore_pubs=ignore_pubs):
186N/A targ_dic.pop(targ_pfmri)
186N/A targ_dic.update(src_dic)
186N/A l = []
186N/A for pfmri in targ_dic:
186N/A for action in targ_dic[pfmri]:
186N/A l.append((pfmri, action))
186N/A target[p] = l
84N/A
84N/A def __realpath(self, path, target):
84N/A """Combine path and target to get the real path."""
84N/A
84N/A result = os.path.dirname(path)
84N/A
84N/A for frag in target.split(os.sep):
84N/A if frag == '..':
84N/A result = os.path.dirname(result)
84N/A elif frag == '.':
84N/A pass
84N/A else:
84N/A result = os.path.join(result, frag)
84N/A
84N/A return result
84N/A
84N/A def __elf_runpath_check(self, path):
84N/A result = None
84N/A list = []
84N/A
84N/A ed = elf.get_dynamic(path)
84N/A for dir in ed.get("runpath", "").split(":"):
84N/A if dir == None or dir == '':
84N/A continue
45N/A
84N/A match = False
84N/A for expr in self.runpath_re:
84N/A if expr.match(dir):
84N/A match = True
84N/A break
84N/A
84N/A if match == False:
84N/A list.append(dir)
84N/A
84N/A if len(list) > 0:
84N/A result = _("bad RUNPATH, '%%s' includes '%s'" %
84N/A ":".join(list))
84N/A
84N/A return result
84N/A
84N/A def __elf_wrong_location_check(self, path):
84N/A result = None
84N/A
84N/A ei = elf.get_info(path)
84N/A bits = ei.get("bits")
168N/A elems = os.path.dirname(path).split("/")
84N/A
168N/A if ("amd64" in elems) or ("sparcv9" in elems) or ("64" in elems):
168N/A path64 = True
168N/A else:
168N/A path64 = False
168N/A
168N/A if bits == 32 and path64:
84N/A result = _("32-bit object '%s' in 64-bit path")
168N/A elif bits == 64 and not path64:
84N/A result = _("64-bit object '%s' in 32-bit path")
84N/A return result
84N/A
84N/A def file_action(self, action, manifest, engine, pkglint_id="001"):
45N/A """Checks for existence in the proto area."""
45N/A
84N/A if action.name not in ["file"]:
45N/A return
45N/A
117N/A path = action.hash
117N/A if path == None or path == 'NOHASH':
117N/A path = action.attrs["path"]
84N/A
84N/A # check for writable files without a preserve attribute
145N/A if "mode" in action.attrs:
84N/A mode = action.attrs["mode"]
84N/A
84N/A if (int(mode, 8) & 0222) != 0 and "preserve" not in action.attrs:
84N/A engine.error(
84N/A _("%(path)s is writable (%(mode)s), but missing a preserve"
84N/A " attribute") % {"path": path, "mode": mode},
84N/A msgid="%s%s.0" % (self.name, pkglint_id))
145N/A elif "preserve" in action.attrs:
145N/A if "mode" in action.attrs:
145N/A mode = action.attrs["mode"]
145N/A if (int(mode, 8) & 0222) == 0:
145N/A engine.error(
145N/A _("%(path)s has a preserve action, but is not writable (%(mode)s)") % {"path": path, "mode": mode},
145N/A msgid="%s%s.4" % (self.name, pkglint_id))
145N/A else:
145N/A engine.error(
145N/A _("%(path)s has a preserve action, but no mode") % {"path": path, "mode": mode},
145N/A msgid="%s%s.3" % (self.name, pkglint_id))
84N/A
84N/A # checks that require a physical file to look at
117N/A if self.proto_path is not None:
117N/A for directory in self.proto_path:
117N/A fullpath = directory + "/" + path
117N/A
117N/A if os.path.exists(fullpath):
117N/A break
84N/A
84N/A if not os.path.exists(fullpath):
84N/A engine.info(
84N/A _("%s missing from proto area, skipping"
84N/A " content checks") % path,
84N/A msgid="%s%s.1" % (self.name, pkglint_id))
84N/A elif elf.is_elf_object(fullpath):
84N/A # 32/64 bit in wrong place
84N/A result = self.__elf_wrong_location_check(fullpath)
84N/A if result != None:
84N/A engine.error(result % path,
84N/A msgid="%s%s.2" % (self.name, pkglint_id))
84N/A result = self.__elf_runpath_check(fullpath)
84N/A if result != None:
84N/A engine.error(result % path,
84N/A msgid="%s%s.3" % (self.name, pkglint_id))
84N/A
84N/A file_action.pkglint_desc = _("Paths should exist in the proto area.")
84N/A
84N/A def link_resolves(self, action, manifest, engine, pkglint_id="002"):
84N/A """Checks for link resolution."""
84N/A
84N/A if action.name not in ["link", "hardlink"]:
84N/A return
84N/A
84N/A path = action.attrs["path"]
84N/A target = action.attrs["target"]
84N/A realtarget = self.__realpath(path, target)
84N/A
186N/A # Check against the target image (ref_paths), since links might
186N/A # resolve outside the packages delivering a particular
186N/A # component.
186N/A if not self.ref_paths.get(realtarget, None):
84N/A engine.error(
84N/A _("%s %s has unresolvable target '%s'") %
84N/A (action.name, path, target),
84N/A msgid="%s%s.0" % (self.name, pkglint_id))
84N/A
84N/A link_resolves.pkglint_desc = _("links should resolve.")
84N/A
84N/A def init_script(self, action, manifest, engine, pkglint_id="003"):
84N/A """Checks for SVR4 startup scripts."""
84N/A
45N/A if action.name not in ["file", "dir", "link", "hardlink"]:
45N/A return
45N/A
45N/A path = action.attrs["path"]
84N/A if self.initscript_re.match(path):
84N/A engine.warning(
84N/A _("SVR4 startup '%s', deliver SMF"
84N/A " service instead") % path,
45N/A msgid="%s%s.0" % (self.name, pkglint_id))
45N/A
84N/A init_script.pkglint_desc = _(
84N/A "SVR4 startup scripts should not be delivered.")
45N/A
45N/Aclass UserlandManifestChecker(base.ManifestChecker):
45N/A """An opensolaris.org-specific class to check manifests."""
45N/A
45N/A name = "userland.manifest"
45N/A
45N/A def __init__(self, config):
45N/A super(UserlandManifestChecker, self).__init__(config)
45N/A
181N/A def component_check(self, manifest, engine, pkglint_id="001"):
84N/A manifest_paths = []
84N/A files = False
84N/A license = False
84N/A
84N/A for action in manifest.gen_actions_by_type("file"):
84N/A files = True
84N/A break
84N/A
84N/A if files == False:
45N/A return
45N/A
84N/A for action in manifest.gen_actions_by_type("license"):
181N/A license = True
181N/A break
181N/A
181N/A if license == False:
181N/A engine.error( _("missing license action"),
181N/A msgid="%s%s.0" % (self.name, pkglint_id))
45N/A
464N/A if 'org.opensolaris.arc-caseid' not in manifest:
464N/A engine.error( _("missing ARC data (org.opensolaris.arc-caseid)"),
181N/A msgid="%s%s.0" % (self.name, pkglint_id))
45N/A
181N/A component_check.pkglint_dest = _(
464N/A "license actions and ARC information are required if you deliver files.")