Skip to content

Commit 3e9798b

Browse files
committed
operations/files.copy: compare files locally
1 parent 823719d commit 3e9798b

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/pyinfra/operations/files.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,15 @@ def _file_equal(local_path: str | IO[Any] | None, remote_path: str) -> bool:
751751
return False
752752

753753

754+
def _remote_file_equal(remote_path_a: str, remote_path_b: str) -> bool:
755+
for fact in [Sha1File, Md5File, Sha256File]:
756+
sum_a = host.get_fact(fact, path=remote_path_a)
757+
sum_b = host.get_fact(fact, path=remote_path_b)
758+
if sum_a and sum_b:
759+
return sum_a == sum_b
760+
return False
761+
762+
754763
@operation(
755764
# We don't (currently) cache the local state, so there's nothing we can
756765
# update to flag the local file as present.
@@ -1331,7 +1340,7 @@ def copy(src: str, dest: str, overwrite=False):
13311340
dest_file_path = os.path.join(dest, os.path.basename(src))
13321341
dest_file_exists = host.get_fact(File, dest_file_path)
13331342
if dest_file_exists and not overwrite:
1334-
if _file_equal(src, dest_file_path):
1343+
if _remote_file_equal(src, dest_file_path):
13351344
host.noop(f"{dest_file_path} already exists")
13361345
return
13371346
else:

tests/operations/files.copy/invalid_overwrite.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
"dest": "/tmp/dest_dir",
55
"overwrite": false
66
},
7-
"local_files": {
8-
"files": {
9-
"/tmp/src_dir/file": "some content"
10-
}
11-
},
127
"facts": {
138
"files.File": {
149
"path=/tmp/src_dir/file": true,

tests/operations/files.copy/noop_dest_file_exists.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"path=/tmp/src_dir/file": null
1515
},
1616
"files.Sha1File": {
17-
"path=/tmp/src_dir/file": "94e66df8cd09d410c62d9e0dc59d3a884e458e05",
18-
"path=/tmp/dest_dir/file": "94e66df8cd09d410c62d9e0dc59d3a884e458e05"
17+
"path=/tmp/src_dir/file": "abc",
18+
"path=/tmp/dest_dir/file": "abc"
1919
}
2020
},
2121
"commands": [],

0 commit comments

Comments
 (0)