Skip to content

Commit c4d16c8

Browse files
committed
Improve file diff using Facts
1 parent ed1f1df commit c4d16c8

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

pyinfra/facts/files.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,13 @@ def process(self, output):
534534
if output and (output[0] == f"{MISSING}{self.path}"):
535535
return None
536536
return output
537+
538+
539+
class FileContents(FactBase):
540+
@override
541+
def command(self, path):
542+
return make_formatted_string_command( "cat {0}", QuoteString(path))
543+
544+
@override
545+
def process(self, output):
546+
return output

pyinfra/operations/files.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import traceback
1111
from datetime import timedelta
1212
from fnmatch import fnmatch
13-
from io import BytesIO, StringIO
13+
from io import StringIO
1414
from pathlib import Path
1515
from typing import IO, Any, Union
1616

@@ -45,6 +45,7 @@
4545
Block,
4646
Directory,
4747
File,
48+
FileContents,
4849
FindFiles,
4950
FindInFile,
5051
Flags,
@@ -897,6 +898,7 @@ def put(
897898
if create_remote_dir:
898899
yield from _create_remote_dir(dest, user, group)
899900

901+
900902
# No remote file, always upload and user/group/mode if supplied
901903
if not remote_file or force:
902904
yield FileUploadCommand(
@@ -917,11 +919,9 @@ def put(
917919

918920
# Check sha1sum, upload if needed
919921
if local_sum != remote_sum:
920-
current_contents = BytesIO()
921-
922922
# Generate diff when contents change
923-
host.get_file(dest, current_contents)
924-
current_lines = current_contents.getvalue().decode("utf-8").splitlines(keepends=True)
923+
current_contents = host.get_fact(FileContents, path=dest)
924+
current_lines = [line + "\n" for line in current_contents]
925925
logger.info(f"\n Will modify {click.style(dest, bold=True)}")
926926

927927
with get_file_io(src, "r") as f:

pyinfra/operations/util/files.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,7 @@ def adjust_regex(line: str, escape_regex_characters: bool) -> str:
178178
return match_line
179179

180180

181-
def generate_color_diff(
182-
current_lines: list[str], desired_lines: list[str]
183-
) -> Generator[str, None, None]:
181+
def generate_color_diff(current_lines: list[str], desired_lines: list[str]) -> Generator[str, None, None]:
184182
def _format_range_unified(start: int, stop: int) -> str:
185183
beginning = start + 1 # lines start numbering with one
186184
length = stop - start

0 commit comments

Comments
 (0)