Skip to content

Commit

Permalink
🐛 Fix package reports on Google Colab (#132)
Browse files Browse the repository at this point in the history
* 🐛 Fix package reports on Google Colab

* ✅ Fix test
  • Loading branch information
ddelange authored Nov 30, 2023
1 parent cc060d8 commit c01f4dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/pipgrip/pipper.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,15 @@ def _get_package_report(
"--trusted-host",
urlparse(extra_index_url).hostname,
]
args += ["--report", "-", package]

# Windows disallows opening fp a second time (within the pip subprocess)
# So close it here, and delete it manually
with NamedTemporaryFile(delete=False, mode="w+") as fp:
report_file = fp.name

args += ["--report", report_file, package]
try:
out = stream_bash_command(args)
stream_bash_command(args)
except subprocess.CalledProcessError as err:
output = getattr(err, "output") or ""
logger.error(
Expand All @@ -320,8 +326,11 @@ def _get_package_report(
)
)
raise RuntimeError("Failed to get report for {}".format(package))
report = json.loads(out)
return report
else:
with io.open(report_file, "r", encoding="utf-8") as fp:
return json.load(fp)
finally:
os.remove(report_file)


def _download_wheel(
Expand Down
5 changes: 4 additions & 1 deletion tests/test_pipper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import subprocess

Expand Down Expand Up @@ -258,7 +259,9 @@ def patch_getcwd():
)
def test_get_package_report(package, pip_output, expected, monkeypatch):
def patch_pip_output(*args, **kwargs):
return pip_output
file_path = args[0][-2]
with open(file_path, "w") as fp:
json.dump(json.loads(pip_output), fp)

def patch_getcwd():
return os.path.expanduser("~")
Expand Down

0 comments on commit c01f4dc

Please sign in to comment.