Skip to content

Commit

Permalink
Fix tempfiles on windows, fixes #646 (#647)
Browse files Browse the repository at this point in the history
* Fix tempfiles on windows, fixes #646

* Docstring for CustomNamedTemporaryFile

* Use tempdir to remove tempfiles on windows

* Chained with statements

* pylint formatting fix

* CHANGELOG: record changes

Signed-off-by: William Woodruff <[email protected]>

---------

Signed-off-by: William Woodruff <[email protected]>
Co-authored-by: William Woodruff <[email protected]>
  • Loading branch information
marickmanrho and woodruffw authored Jul 24, 2023
1 parent 7f8ce7e commit 4485d24
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ All versions prior to 0.0.9 are untracked.

## [Unreleased]

### Fixed

* Fixed a crash on Windows caused by `pip-audit`'s use of temporary files
([#647](https://github.com/pypa/pip-audit/pull/647))

## [2.6.0]

### Added
Expand Down
8 changes: 7 additions & 1 deletion pip_audit/_dependency_source/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ def collect(self) -> Iterator[Dependency]:
# dependency resolution now, we can think about doing `pip install <local-project-dir>`
# regardless of whether the project has a `pyproject.toml` or not. And if it doesn't
# have a `pyproject.toml`, we can raise an error if the user provides `--fix`.
with NamedTemporaryFile() as req_file, TemporaryDirectory() as ve_dir:
with TemporaryDirectory() as ve_dir, NamedTemporaryFile(
dir=ve_dir, delete=False
) as req_file:
# We use delete=False in creating the tempfile to allow it to be
# closed and opened multiple times within the context scope on
# windows, see GitHub issue #646.

# Write the dependencies to a temporary requirements file.
req_file.write(os.linesep.join(deps).encode())
req_file.flush()
Expand Down
8 changes: 6 additions & 2 deletions pip_audit/_virtual_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import logging
import venv
from tempfile import NamedTemporaryFile
from tempfile import NamedTemporaryFile, TemporaryDirectory
from types import SimpleNamespace
from typing import Iterator

Expand Down Expand Up @@ -110,7 +110,11 @@ def post_setup(self, context: SimpleNamespace) -> None:

self._state.update_state("Installing package in isolated environment")

with NamedTemporaryFile() as tmp:
with TemporaryDirectory() as ve_dir, NamedTemporaryFile(dir=ve_dir, delete=False) as tmp:
# We use delete=False in creating the tempfile to allow it to be
# closed and opened multiple times within the context scope on
# windows, see GitHub issue #646.

# Install our packages
package_install_cmd = [
context.env_exe,
Expand Down

0 comments on commit 4485d24

Please sign in to comment.