Skip to content

Commit

Permalink
Render temporary requirements.txt from requirements.json
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysle authored and webknjaz committed Dec 16, 2024
1 parent 217f358 commit 77c6d67
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
10 changes: 8 additions & 2 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
install_req_from_line,
is_pinned_requirement,
key_from_ireq,
render_requirements_json_txt,
)
from ..writer import OutputWriter
from . import options
Expand Down Expand Up @@ -309,7 +310,7 @@ def cli(
# Proxy with a LocalRequirementsRepository if --upgrade is not specified
# (= default invocation)
output_file_exists = os.path.exists(output_file.name)
if not (upgrade or json) and output_file_exists:
if not upgrade and output_file_exists:
output_file_is_empty = os.path.getsize(output_file.name) == 0
if upgrade_install_reqs and output_file_is_empty:
log.warning(
Expand All @@ -320,11 +321,16 @@ def cli(
"as any existing content is truncated."
)

if json:
# Render contents of JSON output file to a temporary requirements
# file in text format in order to make it readable by ``pip``
tmpfile_name = render_requirements_json_txt(output_file.name)

# Use a temporary repository to ensure outdated(removed) options from
# existing requirements.txt wouldn't get into the current repository.
tmp_repository = PyPIRepository(pip_args, cache_dir=cache_dir)
ireqs = parse_requirements(
output_file.name,
tmpfile_name if json else output_file.name,
finder=tmp_repository.finder,
session=tmp_repository.session,
options=tmp_repository.options,
Expand Down
16 changes: 16 additions & 0 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import re
import shlex
import sys
import tempfile
from pathlib import Path
from typing import Any, Callable, Iterable, Iterator, TypeVar, cast

Expand Down Expand Up @@ -772,3 +773,18 @@ def is_path_relative_to(path1: Path, path2: Path) -> bool:
except ValueError:
return False
return True


def render_requirements_json_txt(filename: str) -> str:
"""Render a given ``requirements.json`` file to a temporary
``requirements.txt`` file and return its name.
"""
with open(filename, encoding="utf-8") as f:
reqs = json.load(f)
tmpfile = tempfile.NamedTemporaryFile(mode="w+t", encoding="utf-8", delete=False)
for req in reqs:
tmpfile.write(req["line"])
tmpfile.write("\n")
tmpfile.flush()

return tmpfile.name

0 comments on commit 77c6d67

Please sign in to comment.