Skip to content

Commit b088cf0

Browse files
committed
Make changes according to the review comment
- Refactor the existing deploy method to push artifact's checksums
1 parent cdd908f commit b088cf0

5 files changed

Lines changed: 25 additions & 90 deletions

File tree

README.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ This library enables you to manage Artifactory resources such as users, groups,
3333
+ [Get the information about a file or folder](#get-the-information-about-a-file-or-folder)
3434
+ [Deploy an artifact](#deploy-an-artifact)
3535
+ [Deploy an artifact with properties](#deploy-an-artifact-with-properties)
36-
+ [Deploy an artifact with checksums](#deploy-an-artifact-with-checksums)
3736
+ [Deploy an artifact by checksums](#deploy-an-artifact-by-checksums)
3837
+ [Download an artifact](#download-an-artifact)
3938
+ [Retrieve artifact list](#retrieve-artifact-list)
@@ -425,16 +424,6 @@ artifact = art.artifacts.deploy("<LOCAL_FILE_LOCATION>", "<ARTIFACT_PATH_IN_ARTI
425424
# artifact = art.artifacts.deploy("Desktop/myNewFile.txt", "my-repository/my/new/artifact/directory/file.txt", {"retention": ["30"]})
426425
```
427426

428-
#### Deploy an artifact with checksums
429-
430-
```python
431-
artifact = art.artifacts.deploy("<LOCAL_FILE_LOCATION>", "<ARTIFACT_PATH_IN_ARTIFACTORY>", "<CHECKSUM_ALGORITHMS>")
432-
# artifact = art.artifacts.deploy("Desktop/myNewFile.txt", "my-repository/my/new/artifact/directory/file.txt", checksum_algorithms=["md5", "sha1", "sha256"] )
433-
```
434-
435-
Provides locally calculated checksums for files during deployment so that Artifactory can verify the authenticity of artifacts.
436-
437-
438427
#### Deploy an artifact by checksums
439428

440429
```python

pyartifactory/exception.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,3 @@ class InvalidTokenDataError(ArtifactoryError):
5858

5959
class BuildNotFoundError(ArtifactoryError):
6060
"""Requested build were not found"""
61-
62-
63-
class InvalidAlgorithmError(ArtifactoryError):
64-
"""The Algoritnm is not supported by Artifactory."""

pyartifactory/models/artifact.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,28 @@
1010

1111
from pydantic import BaseModel
1212

13-
from pyartifactory.exception import InvalidAlgorithmError
14-
1513

1614
class Checksums(BaseModel):
1715
"""Models a checksum."""
1816

19-
sha1: Optional[str] = None
20-
md5: Optional[str] = None
21-
sha256: Optional[str] = None
17+
sha1: str
18+
md5: str
19+
sha256: str
2220

2321
@classmethod
24-
def generate(cls, file_: Path, algorithms: List[str]) -> Checksums:
22+
def generate(cls, file_: Path) -> Checksums:
2523
block_size: int = 65536
2624
mapping: dict[str, Callable[[], Any]] = {"md5": hashlib.md5, "sha1": hashlib.sha1, "sha256": hashlib.sha256}
2725
results = {}
2826

29-
for algorithm in algorithms:
30-
if algorithm in mapping:
31-
hasher = mapping[algorithm]()
32-
with file_.absolute().open("rb") as fd:
27+
for algorithm, hashing_function in mapping.items():
28+
hasher = hashing_function()
29+
with file_.absolute().open("rb") as fd:
30+
buf = fd.read(block_size)
31+
while len(buf) > 0:
32+
hasher.update(buf)
3333
buf = fd.read(block_size)
34-
while len(buf) > 0:
35-
hasher.update(buf)
36-
buf = fd.read(block_size)
37-
results[algorithm] = hasher.hexdigest()
38-
else:
39-
raise InvalidAlgorithmError(f"'{algorithm}' is not a supported checksum algorithm")
34+
results[algorithm] = hasher.hexdigest()
4035

4136
return cls(**results)
4237

pyartifactory/objects/artifact.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def deploy(
8282
artifact_path: Union[Path, str],
8383
properties: Optional[Dict[str, List[str]]] = None,
8484
checksum_enabled: bool = False,
85-
checksum_algorithms: Optional[List[str]] = None,
8685
) -> ArtifactInfoResponse:
8786
"""
8887
Deploy a file or directory.
@@ -99,30 +98,22 @@ def deploy(
9998
for file in files:
10099
self.deploy(Path(f"{root}/{file}"), Path(f"{new_root}/{file}"), properties, checksum_enabled)
101100
else:
102-
checksum_headers: Dict[str, str] = {}
103-
104-
if checksum_enabled:
105-
checksum_headers["X-Checksum-Deploy"] = "true"
106-
checksum_algorithms = ["sha1", "sha256", "md5"]
107-
108-
if checksum_algorithms is not None:
109-
artifact_check_sums = Checksums.generate(local_file, checksum_algorithms)
110-
if artifact_check_sums.md5:
111-
checksum_headers["X-Checksum"] = artifact_check_sums.md5
112-
if artifact_check_sums.sha1:
113-
checksum_headers["X-Checksum-Sha1"] = artifact_check_sums.sha1
114-
if artifact_check_sums.sha256:
115-
checksum_headers["X-Checksum-Sha256"] = artifact_check_sums.sha256
116-
101+
properties_param_str = ""
102+
if properties is not None:
103+
properties_param_str = self._format_properties(properties)
104+
route = ";".join(s for s in [artifact_folder.as_posix(), properties_param_str] if s)
105+
artifact_check_sums = Checksums.generate(local_file)
106+
headers = {
107+
"X-Checksum-Sha1": artifact_check_sums.sha1,
108+
"X-Checksum-Sha256": artifact_check_sums.sha256,
109+
"X-Checksum": artifact_check_sums.md5,
110+
}
117111
if checksum_enabled:
112+
headers["X-Checksum-Deploy"] = "true"
118113
try:
119-
properties_param_str = ""
120-
if properties is not None:
121-
properties_param_str = self._format_properties(properties)
122-
route = ";".join(s for s in [artifact_folder.as_posix(), properties_param_str] if s)
123114
self._put(
124115
route=route,
125-
headers=checksum_headers,
116+
headers=headers,
126117
)
127118
except requests.exceptions.HTTPError as error:
128119
if error.response.status_code == 404:
@@ -135,11 +126,7 @@ def deploy(
135126
raise ArtifactoryError from error
136127
else:
137128
with local_file.open("rb") as stream:
138-
properties_param_str = ""
139-
if properties is not None:
140-
properties_param_str = self._format_properties(properties)
141-
route = ";".join(s for s in [artifact_folder.as_posix(), properties_param_str] if s)
142-
self._put(route=route, headers=checksum_headers, data=stream)
129+
self._put(route=route, headers=headers, data=stream)
143130

144131
logger.debug("Artifact %s successfully deployed", local_file)
145132
return self.info(artifact_folder)

tests/test_artifacts.py

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def test_update_property_fail_bad_value():
601601
],
602602
)
603603
def test_checksum_defined_file(file_path: Path, expected_sha1: str, expected_md5: str, expected_sha256: str):
604-
result = Checksums.generate(file_path, ["sha1", "sha256", "md5"])
604+
result = Checksums.generate(file_path)
605605
expected = Checksums(
606606
sha1=expected_sha1,
607607
md5=expected_md5,
@@ -681,42 +681,10 @@ def test_deploy_artifact_with_checksum_algorithms_success(file_info: dict, expec
681681
artifact = artifactory.deploy(
682682
Path(LOCAL_FILE_LOCATION),
683683
Path(ARTIFACT_PATH),
684-
checksum_algorithms=file_info["originalChecksums"].keys(),
685684
)
686685
assert expected == artifact.originalChecksums
687686

688687

689-
@pytest.mark.parametrize(
690-
"file_info",
691-
[
692-
pytest.param(
693-
{
694-
**FILE_INFO_RESPONSE.copy(),
695-
"originalChecksums": {"sha224": "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"},
696-
},
697-
id="sha224",
698-
),
699-
],
700-
)
701-
@responses.activate
702-
def test_deploy_artifact_with_checksum_algorithms_error(file_info: dict):
703-
responses.add(responses.PUT, f"{URL}/{ARTIFACT_PATH}", status=409)
704-
705-
responses.add(
706-
responses.GET,
707-
f"{URL}/api/storage/{ARTIFACT_PATH}",
708-
json=file_info,
709-
status=200,
710-
)
711-
with pytest.raises(Exception):
712-
artifactory = ArtifactoryArtifact(AuthModel(url=URL, auth=AUTH))
713-
artifactory.deploy(
714-
Path(LOCAL_FILE_LOCATION),
715-
Path(ARTIFACT_PATH),
716-
checksum_algorithms=file_info["originalChecksums"].keys(),
717-
)
718-
719-
720688
@responses.activate
721689
def test_deploy_artifact_with_checksum_success(mocker):
722690
responses.add(responses.PUT, f"{URL}/{ARTIFACT_PATH}", status=200)

0 commit comments

Comments
 (0)