Skip to content

Commit 3779445

Browse files
authored
Add support for SHA384 hash in files.download() (#1247)
Co-authored-by: simonhammes <simonhammes@users.noreply.github.com>
1 parent 7147572 commit 3779445

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

pyinfra/facts/files.py

+6
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ class Sha256File(HashFileFactBase, digits=64, cmds=["sha256sum", "shasum -a 256"
276276
"""
277277

278278

279+
class Sha384File(HashFileFactBase, digits=96, cmds=["sha384sum", "shasum -a 384", "sha384"]):
280+
"""
281+
Returns a SHA384 hash of a file, or ``None`` if the file does not exist.
282+
"""
283+
284+
279285
class Md5File(HashFileFactBase, digits=32, cmds=["md5sum", "md5"]):
280286
"""
281287
Returns an MD5 hash of a file, or ``None`` if the file does not exist.

pyinfra/operations/files.py

+18
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
Md5File,
5252
Sha1File,
5353
Sha256File,
54+
Sha384File,
5455
)
5556
from pyinfra.facts.server import Date, Which
5657

@@ -67,6 +68,7 @@ def download(
6768
mode: str | None = None,
6869
cache_time: int | None = None,
6970
force=False,
71+
sha384sum: str | None = None,
7072
sha256sum: str | None = None,
7173
sha1sum: str | None = None,
7274
md5sum: str | None = None,
@@ -84,6 +86,7 @@ def download(
8486
+ mode: permissions of the files
8587
+ cache_time: if the file exists already, re-download after this time (in seconds)
8688
+ force: always download the file, even if it already exists
89+
+ sha384sum: sha384 hash to checksum the downloaded file against
8790
+ sha256sum: sha256 hash to checksum the downloaded file against
8891
+ sha1sum: sha1 hash to checksum the downloaded file against
8992
+ md5sum: md5 hash to checksum the downloaded file against
@@ -135,6 +138,10 @@ def download(
135138
if sha256sum != host.get_fact(Sha256File, path=dest):
136139
download = True
137140

141+
if sha384sum:
142+
if sha384sum != host.get_fact(Sha384File, path=dest):
143+
download = True
144+
138145
if md5sum:
139146
if md5sum != host.get_fact(Md5File, path=dest):
140147
download = True
@@ -211,6 +218,17 @@ def download(
211218
QuoteString("SHA256 did not match!"),
212219
)
213220

221+
if sha384sum:
222+
yield make_formatted_string_command(
223+
(
224+
"(( sha384sum {0} 2> /dev/null || shasum -a 384 {0} ) "
225+
"| grep {1}) || ( echo {2} && exit 1 )"
226+
),
227+
QuoteString(dest),
228+
sha384sum,
229+
QuoteString("SHA384 did not match!"),
230+
)
231+
214232
if md5sum:
215233
yield make_formatted_string_command(
216234
(
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"arg": "myfile",
3+
"command": "test -e myfile && ( sha384sum myfile 2> /dev/null || shasum -a 384 myfile 2> /dev/null || sha384 myfile 2> /dev/null ) || true",
4+
"output": [
5+
"1dd2e448df7c4dc19848a37fdf25c86660b19d06a4cd6987ca2c4071f80e9dd3061e4b01ddb982dc56ed7a2ed2294699 myfile"
6+
],
7+
"fact": "1dd2e448df7c4dc19848a37fdf25c86660b19d06a4cd6987ca2c4071f80e9dd3061e4b01ddb982dc56ed7a2ed2294699"
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"arg": "myfile",
3+
"command": "test -e myfile && ( sha384sum myfile 2> /dev/null || shasum -a 384 myfile 2> /dev/null || sha384 myfile 2> /dev/null ) || true",
4+
"output": [
5+
"SHA384 (myfile) = 1dd2e448df7c4dc19848a37fdf25c86660b19d06a4cd6987ca2c4071f80e9dd3061e4b01ddb982dc56ed7a2ed2294699"
6+
],
7+
"fact": "1dd2e448df7c4dc19848a37fdf25c86660b19d06a4cd6987ca2c4071f80e9dd3061e4b01ddb982dc56ed7a2ed2294699"
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"arg": "my () file && special_chars.txt",
3+
"command": "test -e 'my () file && special_chars.txt' && ( sha384sum 'my () file && special_chars.txt' 2> /dev/null || shasum -a 384 'my () file && special_chars.txt' 2> /dev/null || sha384 'my () file && special_chars.txt' 2> /dev/null ) || true",
4+
"output": [
5+
"1dd2e448df7c4dc19848a37fdf25c86660b19d06a4cd6987ca2c4071f80e9dd3061e4b01ddb982dc56ed7a2ed2294699 my () file && special_chars.txt"
6+
],
7+
"fact": "1dd2e448df7c4dc19848a37fdf25c86660b19d06a4cd6987ca2c4071f80e9dd3061e4b01ddb982dc56ed7a2ed2294699"
8+
}

0 commit comments

Comments
 (0)