Skip to content

Commit

Permalink
Move linesep determination to a dedicated function, with improvements
Browse files Browse the repository at this point in the history
- skip decode to save time
- catch FileNotFoundError to avoid potential race condition

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
  • Loading branch information
AndydeCleyre and graingert committed Oct 3, 2022
1 parent 16ce7ca commit a55c423
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,35 @@ def _get_default_option(option_name: str) -> Any:
return getattr(default_values, option_name)


def _determine_linesep(
strategy: str = "preserve", filenames: Tuple[str, ...] = ()
) -> str:
"""
Determine and return linesep string for OutputWriter to use.
Valid strategies: "LF", "CRLF", "native", "preserve"
When preserving, files are checked in order for existing newlines.
"""
if strategy == "preserve":
for fname in filenames:
try:
with open(fname, "rb") as existing_file:
existing_text = existing_file.read()
except FileNotFoundError:
continue
if b"\r\n" in existing_text:
strategy = "CRLF"
break
elif b"\n" in existing_text:
strategy = "LF"
break
return {
"native": os.linesep,
"LF": "\n",
"CRLF": "\r\n",
"preserve": "\n",
}[strategy]


@click.command(context_settings={"help_option_names": ("-h", "--help")})
@click.version_option(**version_option_kwargs)
@click.pass_context
Expand Down Expand Up @@ -510,24 +539,9 @@ def cli(

log.debug("")

# Determine linesep for OutputWriter to use
if newline == "preserve":
for fname in (output_file.name, *src_files):
if os.path.exists(fname):
with open(fname, "rb") as existing_file:
existing_text = existing_file.read().decode()
if "\r\n" in existing_text:
newline = "CRLF"
break
elif "\n" in existing_text:
newline = "LF"
break
linesep = {
"native": os.linesep,
"LF": "\n",
"CRLF": "\r\n",
"preserve": "\n",
}[newline]
linesep = _determine_linesep(
strategy=newline, filenames=(output_file.name, *src_files)
)

##
# Output
Expand Down

0 comments on commit a55c423

Please sign in to comment.