Skip to content

Commit

Permalink
Update safety check command runner to handle exceptions
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Ryan <dan@danryan.co>
  • Loading branch information
techalchemy committed Mar 11, 2019
1 parent 3b7a5a1 commit f63278a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
18 changes: 13 additions & 5 deletions pipenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2569,7 +2569,9 @@ def do_check(
decode_for_output("Checking installed package safety…"), bold=True)
)
if ignore:
ignored = "--ignore {0}".format(" --ignore ".join(ignore))
if not isinstance(ignore, (tuple, list)):
ignore = [ignore]
ignored = [["--ignore", cve] for cve in ignore]
click.echo(
crayons.normal(
"Notice: Ignoring CVE(s) {0}".format(crayons.yellow(", ".join(ignore)))
Expand All @@ -2579,12 +2581,20 @@ def do_check(
else:
ignored = ""
key = "--key={0}".format(PIPENV_PYUP_API_KEY)
cmd = _cmd + [safety_path, "check", "--json", key, ignored]
c = run_command(cmd)
cmd = _cmd + [safety_path, "check", "--json", key]
if ignored:
for cve in ignored:
cmd += cve
c = run_command(cmd, catch_exceptions=False)
try:
results = simplejson.loads(c.out)
except (ValueError, JSONDecodeError):
raise exceptions.JSONParseError(c.out, c.err)
except Exception:
raise exceptions.PipenvCmdError(c.cmd, c.out, c.err, c.return_code)
if c.ok:
click.echo(crayons.green("All good!"))
sys.exit(0)
for (package, resolved, installed, description, vuln) in results:
click.echo(
"{0}: {1} {2} resolved ({3} installed)!".format(
Expand All @@ -2596,8 +2606,6 @@ def do_check(
)
click.echo("{0}".format(description))
click.echo()
if not results:
click.echo(crayons.green("All good!"))
else:
sys.exit(1)

Expand Down
11 changes: 6 additions & 5 deletions pipenv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ def convert_toml_table(section):
return parsed


def run_command(cmd, *args, **kwargs):
def run_command(cmd, *args, catch_exceptions=True, **kwargs):
"""
Take an input command and run it, handling exceptions and error codes and returning
its stdout and stderr.
:param cmd: The list of command and arguments.
:type cmd: list
:param bool catch_exceptions: Whether to catch and raise exceptions on failure
:returns: A 2-tuple of the output and error from the command
:rtype: Tuple[str, str]
:raises: exceptions.PipenvCmdError
Expand Down Expand Up @@ -153,7 +154,7 @@ def run_command(cmd, *args, **kwargs):
click_echo("Command output: {0}".format(
crayons.blue(decode_output(c.out))
), err=True)
if not c.ok:
if not c.ok and catch_exceptions:
raise PipenvCmdError(cmd_string, c.out, c.err, c.return_code)
return c

Expand Down Expand Up @@ -1873,7 +1874,7 @@ def find_python(finder, line=None):
finder = Finder(global_search=True)
if not line:
result = next(iter(finder.find_all_python_versions()), None)
elif line and line[0].digit() or re.match(r'[\d\.]+', line):
elif line and line[0].isdigit() or re.match(r'[\d\.]+', line):
result = finder.find_python_version(line)
else:
result = finder.find_python_version(name=line)
Expand Down Expand Up @@ -1907,8 +1908,8 @@ def is_python_command(line):

from pipenv.vendor.pythonfinder.utils import PYTHON_IMPLEMENTATIONS
is_version = re.match(r'[\d\.]+', line)
if line.startswith("python") or is_version or \
any(line.startswith(v) for v in PYTHON_IMPLEMENTATIONS):
if (line.startswith("python") or is_version or
any(line.startswith(v) for v in PYTHON_IMPLEMENTATIONS)):
return True
# we are less sure about this but we can guess
if line.startswith("py"):
Expand Down

0 comments on commit f63278a

Please sign in to comment.