Skip to content

Commit

Permalink
feat(output): save list of deleted artifacts as json (#145)
Browse files Browse the repository at this point in the history
* feat(output): save list of deleted artifacts as json

* fixup! feat(output): save list of deleted artifacts as json

* fixup! feat(output): save list of deleted artifacts as json

* removed_artifacts can not be None

* removed_artifacts

---------

Co-authored-by: roytev <[email protected]>
Co-authored-by: allburov <[email protected]>
  • Loading branch information
3 people authored Aug 20, 2024
1 parent c398fcd commit d6ed5f0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ docker run -v "$(pwd)":/app devopshq/artifactory-cleanup artifactory-cleanup --l
# Save the table summary in a file
artifactory-cleanup --output=myfile.txt
# Save the summary in a Json file
# Save the summary in a json file
artifactory-cleanup --output=myfile.txt --output-format=json
# Save the summary in a json file and append the list of all removed artifacts
artifactory-cleanup --output=myfile.json --output-format json --output-artifacts
```

# Rules
Expand Down
12 changes: 7 additions & 5 deletions artifactory_cleanup/artifactorycleanup.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from concurrent.futures import ThreadPoolExecutor
from datetime import date
from typing import List, Iterator
from typing import List, Iterator, Optional

from attr import dataclass
from requests import Session

from artifactory_cleanup.errors import ArtifactoryCleanupException
from artifactory_cleanup.rules.base import CleanupPolicy, ArtifactDict
from artifactory_cleanup.rules.base import ArtifactsList, CleanupPolicy, ArtifactDict


@dataclass
class CleanupSummary:
policy_name: str
artifacts_removed: int
artifacts_size: int
removed_artifacts: ArtifactsList


class ArtifactoryCleanup:
Expand All @@ -38,7 +39,7 @@ def _init_policies(self, today):
for policy in self.policies:
policy.init(self.session, today)

def cleanup(self, block_ctx_mgr, test_ctx_mgr) -> Iterator[CleanupSummary]:
def cleanup(self, block_ctx_mgr, test_ctx_mgr) -> Iterator[Optional[CleanupSummary]]:
for policy in self.policies:
with block_ctx_mgr(policy.name):
# Prepare
Expand Down Expand Up @@ -72,12 +73,13 @@ def _delete(artifact):
try:
artifacts_size = sum([x["size"] for x in artifacts_to_remove])
print("Summary size: {}".format(artifacts_size))
yield CleanupSummary(
summary = CleanupSummary(
policy_name=policy.name,
artifacts_size=artifacts_size,
artifacts_removed=len(artifacts_to_remove),
removed_artifacts=artifacts_to_remove
)

yield summary
except KeyError:
print("Summary size not defined")
yield None
Expand Down
33 changes: 20 additions & 13 deletions artifactory_cleanup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
ArtifactoryCleanup,
)
from artifactory_cleanup.base_url_session import BaseUrlSession
from artifactory_cleanup.context_managers import get_context_managers
from artifactory_cleanup.errors import InvalidConfigError
from artifactory_cleanup.loaders import (
PythonLoader,
YamlConfigLoader,
)
from artifactory_cleanup.context_managers import get_context_managers

requests.packages.urllib3.disable_warnings()

Expand Down Expand Up @@ -95,6 +95,13 @@ class ArtifactoryCleanupCLI(cli.Application):
mandatory=False,
)

_output_artifacts = cli.Flag(
"--output-artifacts",
help="When --output-format is json, append the list of all removed artifacts to output",
mandatory=False,
default=False,
)

@property
def VERSION(self):
# To prevent circular imports
Expand Down Expand Up @@ -137,13 +144,13 @@ def _print_table(self, result: dict):
print(self._format_table(result))

def _create_output_file(self, result, filename, format):
text = None
text = ""
if format == "table":
text = self._format_table(result).get_string()
else:
text = json.dumps(result)
elif format == "json":
text = json.dumps(result, indent=4)

with open(filename, "w") as file:
with open(filename, "w", encoding="utf-8") as file:
file.write(text)

def main(self):
Expand Down Expand Up @@ -189,14 +196,14 @@ def main(self):
continue
total_size += summary.artifacts_size

result["policies"].append(
{
"name": summary.policy_name,
"file_count": summary.artifacts_removed,
"size": summary.artifacts_size,
}
)

policy = {
"name": summary.policy_name,
"file_count": summary.artifacts_removed,
"size": summary.artifacts_size
}
if self._output_artifacts:
policy["removed_artifacts"] = summary.removed_artifacts
result["policies"].append(policy)
result["total_size"] = total_size

self._print_table(result)
Expand Down

0 comments on commit d6ed5f0

Please sign in to comment.