Skip to content

Commit be64dff

Browse files
committed
Improve file diff using Facts
1 parent 96b1646 commit be64dff

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

pyinfra/facts/files.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,18 @@ 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+
"""
541+
Returns the contents of a file as a list of lines. Works with both sha1sum and sha1. Returns
542+
``None`` if the file doest not exist.
543+
"""
544+
545+
@override
546+
def command(self, path):
547+
return make_formatted_string_command("cat {0}", QuoteString(path))
548+
549+
@override
550+
def process(self, output):
551+
return output

pyinfra/operations/files.py

Lines changed: 8 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

@@ -47,6 +47,7 @@
4747
Block,
4848
Directory,
4949
File,
50+
FileContents,
5051
FindFiles,
5152
FindInFile,
5253
Flags,
@@ -942,11 +943,13 @@ def put(
942943
# File exists, check sum and check user/group/mode if supplied
943944
else:
944945
if not _file_equal(local_sum_path, dest):
945-
current_contents = BytesIO()
946-
947946
# Generate diff when contents change
948-
host.get_file(dest, current_contents)
949-
current_lines = current_contents.getvalue().decode("utf-8").splitlines(keepends=True)
947+
current_contents = host.get_fact(FileContents, path=dest)
948+
if current_contents:
949+
current_lines = [line + "\n" for line in current_contents]
950+
else:
951+
current_lines = []
952+
950953
logger.info(f"\n Will modify {click.style(dest, bold=True)}")
951954

952955
with get_file_io(src, "r") as f:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"arg": "myfile",
3+
"command": "cat myfile",
4+
"output": ["line1", "line2"],
5+
"fact": ["line1", "line2"]
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"arg": ["test"],
3+
"command": "cat test",
4+
"output": null,
5+
"fact": null
6+
}

tests/operations/files.put/different_remote.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
},
2525
"files.Sha1File": {
2626
"path=/home/somefile.txt": "nowt"
27+
},
28+
"files.FileContents": {
29+
"path=/home/somefile.txt": []
2730
}
2831
},
2932
"commands": [

tests/operations/server.user/keys_delete.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
"files.Sha256File": {
4545
"path=homedir/.ssh/authorized_keys": null
4646
},
47+
"files.FileContents": {
48+
"path=homedir/.ssh/authorized_keys": null
49+
},
4750
"server.Groups": {}
4851
},
4952
"commands": [

0 commit comments

Comments
 (0)