-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PKG-7597] Publish packages with attestation bundle (#7)
- Loading branch information
Showing
10 changed files
with
264 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,56 @@ | ||
import os | ||
from glob import glob | ||
from pathlib import Path | ||
|
||
from package_publisher.cli_arguments import CliArguments | ||
from package_publisher.core import PackagePublisher | ||
from package_publisher.helpers import attestations_to_bundle | ||
|
||
arguments = CliArguments() | ||
|
||
publisher = PackagePublisher(registry=arguments.get_registry()) | ||
|
||
artifacts_dir = arguments.get_artifacts_dir() | ||
if artifacts_dir == "": | ||
print( | ||
"Error: Missing --artifacts-dir argument. Example: --artifacts-dir ./artifacts" | ||
) | ||
exit(1) | ||
|
||
artifacts_glob = glob("{}/**/*".format(artifacts_dir), recursive=True) | ||
files = [path for path in artifacts_glob if os.path.isfile(path)] | ||
file_paths = [path for path in artifacts_glob if os.path.isfile(path)] | ||
|
||
attestations_dir = arguments.get_attestations_dir() | ||
|
||
if attestations_dir != "": | ||
attestations_glob = glob("{}/**/*".format(attestations_dir), recursive=True) | ||
attestation_files = [path for path in attestations_glob if os.path.isfile(path)] | ||
attestation_bundle_path = attestations_to_bundle(attestation_files) | ||
else: | ||
attestation_bundle_path = None | ||
|
||
for file in files: | ||
|
||
publisher = PackagePublisher( | ||
registry=arguments.get_registry(), | ||
attestation_bundle_path=attestation_bundle_path, | ||
) | ||
|
||
for file_path in file_paths: | ||
print( | ||
"Publishing {} → {}".format( | ||
file.replace("{}/".format(artifacts_dir), ""), arguments.get_registry() | ||
file_path.replace("{}/".format(artifacts_dir), ""), arguments.get_registry() | ||
) | ||
) | ||
|
||
response = publisher.upload_package( | ||
file_path=file, | ||
provenance_bundle_path=arguments.get_provenance_bundle(), | ||
file_path=file_path, | ||
) | ||
|
||
print(" ✅ \033]1339;url={}\a".format(response["web_url"])) | ||
print("") | ||
|
||
|
||
if attestation_bundle_path is not None: | ||
print("~~~ 🚚 Preview Attestation Bundle") | ||
with open(attestation_bundle_path, "r", encoding="utf-8") as f: | ||
print(f.read()) | ||
|
||
Path(attestation_bundle_path).unlink() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import json | ||
|
||
from tempfile import NamedTemporaryFile | ||
|
||
|
||
def attestations_to_bundle(file_paths: list[str]) -> str | None: | ||
if len(file_paths) <= 0: | ||
return None | ||
|
||
bundle_file = NamedTemporaryFile(delete=False) | ||
|
||
for file_path in file_paths: | ||
with open(file_path, "r", encoding="utf-8") as file: | ||
try: | ||
content = json.loads(file.read()) | ||
except json.decoder.JSONDecodeError as error: | ||
print("Error parsing JSON in attestation: {}".format(file_path)) | ||
print(" {}".format(error)) | ||
exit(1) | ||
bundle_file.write(bytearray(json.dumps(content), encoding="utf-8")) | ||
bundle_file.write(bytearray("\n", encoding="utf-8")) | ||
|
||
bundle_file.close() | ||
return bundle_file.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.