Skip to content

Commit

Permalink
PSARC/2019/061 pkglint extension_path
Browse files Browse the repository at this point in the history
29704610 pkglint extension_path
  • Loading branch information
darrenmoffat committed Apr 29, 2019
1 parent 85ed1c9 commit b1d64c2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
19 changes: 16 additions & 3 deletions src/man/pkglint.1
Expand Up @@ -10,15 +10,15 @@
<refmiscinfo class="sectdesc">&man1;</refmiscinfo>
<refmiscinfo class="software">&release;</refmiscinfo>
<refmiscinfo class="arch">generic</refmiscinfo>
<refmiscinfo class="copyright">Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.</refmiscinfo>
<refmiscinfo class="copyright">Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.</refmiscinfo>
</refmeta>
<refnamediv>
<refname>pkglint</refname><refpurpose>Image Packaging System package lint</refpurpose>
</refnamediv>
<refsynopsisdiv><title></title>
<synopsis>/usr/bin/pkglint [-c <replaceable>cache_dir</replaceable>] [-r <replaceable>
repo_uri</replaceable>]... [-p <replaceable>regexp</replaceable>]
[-f <replaceable>config_file</replaceable>] [-b <replaceable>branch</replaceable>]
[-e <replaceable>extension_path</replaceable>] [-f <replaceable>config_file</replaceable>] [-b <replaceable>branch</replaceable>]
[-v] [-l <replaceable>lint_uri</replaceable>]... | <replaceable>manifest</replaceable> ...
</synopsis>
<synopsis>/usr/bin/pkglint -L [-v]</synopsis>
Expand Down Expand Up @@ -101,6 +101,11 @@ display the method that implements the check instead of the description.</para>
config_file</replaceable> configuration file.</para>
</listitem>
</varlistentry>
<varlistentry><term><option>e</option> <replaceable>extension_path</replaceable></term>
<listitem><para>Directroy to add to the Python search path when loading
<command>pkglint</command> extensions.</para>
</listitem>
</varlistentry>
<varlistentry><term><option>p</option> <replaceable>regexp</replaceable></term>
<listitem><para>Specify a regular expression used to narrow the list of packages
to be checked from the lint repository. All manifests from the reference repository
Expand Down Expand Up @@ -143,7 +148,15 @@ make sense for published packages. The default value is <literal>True</literal>.
<listitem><para>The plugin mechanism of <command>pkglint</command> allows
for additional lint modules to be added at runtime. Any key that starts with <literal>
pkglint.ext.</literal> takes a value that must be a fully-specified Python
module. See the &ldquo;Developers&rdquo; section for more information.</para>
module. See the &ldquo;Developers&rdquo; section for more information.
The <literal>extension_path</literal> option specifies additional directory
locations to search when loading extensions</para>
</listitem>
</varlistentry>
<varlistentry><term><literal>extension_path</literal></term>
<listitem><para>An list of directories, separated by ':' no most platforms, to search
for extension modules. If the -e flag is specified on the CLI it is added higher
in the search order than this config file option.</para>
</listitem>
</varlistentry>
<varlistentry><term><literal>pkglint.exclude</literal></term>
Expand Down
28 changes: 25 additions & 3 deletions src/modules/lint/engine.py
Expand Up @@ -21,7 +21,7 @@
#

#
# Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
#

import pkg.client.api
Expand Down Expand Up @@ -289,7 +289,7 @@ class LintEngine(object):
see the comment for <LintEngine>.execute()"""

def __init__(self, formatter, verbose=False, config_file=None,
use_tracker=None):
use_tracker=None, extension_path=[]):
"""Creates a lint engine a given pkg.lint.log.LogFormatter.
'verbose' overrides any log_level settings in the config file
to DEBUG
Expand Down Expand Up @@ -329,8 +329,14 @@ def __init__(self, formatter, verbose=False, config_file=None,
# whether to ignore publisher differences when comparing vers
self.ignore_pubs = True

# Ensure extension_path is always a list even if empty
self.extension_path = extension_path
if not self.extension_path:
self.extension_path = []

self.conf = self.load_config(config_file, verbose=verbose)
# overrides config_file entry

# override config_file entry
if use_tracker is not None:
self.use_tracker = use_tracker

Expand Down Expand Up @@ -359,8 +365,15 @@ def _load_checker_module(self, name, config):
instances of the checker classes the module declares,
assuming they haven't been excluded by the config object."""

# Make a copy of sys.path so we can revert it after module load
_preserved_sys_path = list(sys.path)
try:
self.logger.debug("Loading module {0}".format(name))
# Temporarily add the extension paths to sys.path
# Any paths specified in the init call take precedence
# over the config file.
sys.path.extend(self.extension_path)

# the fifth parameter is 'level', which defautls to -1
# in Python 2 and 0 in Python 3.
__import__(name, None, None, [])
Expand All @@ -369,6 +382,9 @@ def _load_checker_module(self, name, config):
return (checkers, excluded)
except (KeyError, ImportError) as err:
raise base.LintException(err)
finally:
# Reset sys.path
sys.path = _preserved_sys_path

def _unique_checkers(self):
"""Ensure that the engine has unique names for all of the loaded
Expand Down Expand Up @@ -426,6 +442,12 @@ def load_config(self, config, verbose=False):
except configparser.NoOptionError:
pass

try:
self.extension_path.extend(conf.get("pkglint",
"extension_path").split(os.pathsep))
except configparser.NoOptionError:
pass

for key, value in conf.items("pkglint"):
if "pkglint.ext" in key:
if value in excl:
Expand Down
10 changes: 7 additions & 3 deletions src/util/publish/pkglint.py
Expand Up @@ -29,6 +29,7 @@
import logging
import six
import sys
import os
import gettext
import locale
import traceback
Expand Down Expand Up @@ -72,6 +73,7 @@ def main_func():
_("\n"
" %prog [-b branch] [-c cache_dir] [-f file]\n"
" [-l uri ...] [-p regexp] [-r uri ...] [-v]\n"
" [-e extension_path ...]\n"
" manifest ...\n"
" %prog -L")
parser = OptionParser(usage=usage)
Expand All @@ -91,9 +93,10 @@ def main_func():
help=_("pattern to match FMRIs in lint URI"))
parser.add_option("-r", dest="ref_uris", metavar="uri",
action="append", help=_("reference repository URI"))
parser.add_option("-e", dest="extension_path", metavar="dir",
action="append", help=_("extension_path"))
parser.add_option("-v", dest="verbose", action="store_true",
help=_("produce verbose output, overriding settings in pkglintrc")
)
help=_("produce verbose output, overriding settings in pkglintrc"))

opts, args = parser.parse_args(sys.argv[1:])

Expand Down Expand Up @@ -126,7 +129,8 @@ def main_func():
if not opts.list_checks:
msg(_("Lint engine setup..."))
lint_engine = engine.LintEngine(lint_logger,
config_file=opts.config, verbose=opts.verbose)
config_file=opts.config, verbose=opts.verbose,
extension_path=opts.extension_path)

if opts.list_checks:
list_checks(lint_engine.checkers,
Expand Down

0 comments on commit b1d64c2

Please sign in to comment.