Skip to content

Commit

Permalink
Use unlink to remove temporary file. Trick needed for Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Jan 4, 2024
1 parent 14ce40c commit b62885f
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions fawltydeps/extract_declared_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
import tokenize
from dataclasses import replace
from os import unlink
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Callable, Iterable, Iterator, NamedTuple, Optional, Tuple
Expand Down Expand Up @@ -149,11 +150,20 @@ def parse_value(value: str) -> Iterator[DeclaredDependency]:
# TODO: try leveraging RequirementsFile.from_string once
# pip-requirements-parser updates.
# See: https://github.com/nexB/pip-requirements-parser/pull/17
with NamedTemporaryFile(mode="wt", delete=False) as tmp:
tmp.write(value)
tmp.flush()
for dep in parse_requirements_txt(Path(tmp.name)):

# https://github.com/nexB/pip-requirements-parser/pull/19#discussion_r1379279880
temp_file = NamedTemporaryFile('wt', delete=False,
# we prefer utf8 encoded strings, but ...
# - must not change newlines
# - must not change encoding, fallback to system encoding for compatibility
newline='', encoding=None)
temp_file.write(value)
temp_file.close()
try:
for dep in parse_requirements_txt(Path(temp_file.name)):
yield replace(dep, source=source)
finally:
unlink(Path(temp_file.name))

def extract_section(section: str) -> Iterator[DeclaredDependency]:
if section in parser:
Expand Down

0 comments on commit b62885f

Please sign in to comment.