Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pip-audit allow directories to be installed #518

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions assets/pip-audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import tomllib

from collections.abc import Iterator
from os import environ, path
from os import chdir, environ, getcwd, path

from pip_audit._audit import Auditor
from pip_audit._cli import VulnerabilityServiceChoice
Expand Down Expand Up @@ -39,10 +39,19 @@ def main():
extra_install_args.extend(["--trusted-host", host])

for lock_path in changed_lock_files:
for install_cmd, line_number in install_commands(lock_path):
install(lock_path, auditor, extra_install_args, index_url)


def install(lock_path, auditor, extra_install_args, index_url=None):
install_commands_by_line = install_commands(lock_path)
try:
original_cwd = getcwd()
tmpdir = path.join(original_cwd, "./.venv-deleteme")
chdir(path.dirname(lock_path))
for install_cmd, line_number in install_commands_by_line:
venv = VirtualEnv(install_cmd + extra_install_args, index_url=index_url)
try:
venv.create("./.venv-deleteme")
venv.create(tmpdir)
except VirtualEnvError as e:
print(e)
continue
Expand All @@ -63,7 +72,9 @@ def main():
print(e)
continue
finally:
venv.clear_directory("./.venv-deleteme")
venv.clear_directory(tmpdir)
finally:
chdir(original_cwd)


def install_commands(lock_path: str) -> Iterator[tuple[list[str], int]]:
Expand All @@ -90,13 +101,16 @@ def install_commands_for_requirements_txt(lock_file_lines: list[str], diff_lines
zero_indexed_lineno = 0
while zero_indexed_lineno < len(lock_file_lines):
line = lock_file_lines[zero_indexed_lineno]
if line and line in diff_lines and not line.startswith(("#", "--", "-e ")):
if line and line in diff_lines and not line.startswith(("#", "--")):
while line.endswith("\\"):
zero_indexed_lineno += 1
line = line[:-1].strip() + " " + lock_file_lines[zero_indexed_lineno]
# There could be quoted or escaped spaces, but unlikely in 1st word.
install_cmd = [line.strip().split(" ", 1)[0]]
yield (install_cmd, zero_indexed_lineno + 1)
# There could be quoted or escaped spaces, but unlikely to be affected.
# Ignore --hash= and anything commented out but allow @ and ;sys_platform
install_cmd = line.strip().split("#", 1)[0].split(" --", 1)[0].strip()
if install_cmd.startswith("-e "):
install_cmd = install_cmd[3:].strip()
yield ([install_cmd], zero_indexed_lineno + 1)
zero_indexed_lineno += 1


Expand Down
Loading