Skip to content

Commit

Permalink
Merge branch 'main' into add-auth-backends
Browse files Browse the repository at this point in the history
Signed-off-by: Vanessasaurus <[email protected]>
  • Loading branch information
vsoch authored Jun 1, 2024
2 parents 74e0574 + b4fd5f4 commit cffab3a
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 3 deletions.
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@
"avatar_url": [
"https://avatars.githubusercontent.com/u/4820203?v=4"
]
},
{
"login": "tumido",
"name": "Tom Coufal",
"contributions": [
"code"
],
"profile": "https://github.com/tumido",
"avatar_url": [
"https://avatars.githubusercontent.com/u/7453394?v=4"
]
}
],
"contributorsPerLine": 7
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
ls
- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action@ba1486788b0490a235422264426c45848eac35c6 # v4.4.1
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: gh-pages
folder: .
5 changes: 5 additions & 0 deletions .tributors
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,10 @@
"my5cents": {
"name": "my5cents",
"blog": "https://github.com/my5cents"
},
"tumido": {
"name": "Tom Coufal",
"bio": "Pushing buttons at @redhat-et",
"blog": "https://github.com/tumido"
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The versions coincide with releases on pip. Only major versions will be released

## [0.0.x](https://github.com/oras-project/oras-py/tree/main) (0.0.x)
- refactor of auth to be provided by backend modules (0.2.0)
- Allow generating a Subject from a pre-existing Manifest (0.1.30)
- add option to not refresh headers during the pushing flow, useful for push with basic auth (0.1.29)
- enable additionalProperties in schema validation (0.1.28)
- Introduce the option to not refresh headers when fetching manifests when pulling artifacts (0.1.27)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ORAS Python

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-19-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

![ORAS Logo](https://raw.githubusercontent.com/oras-project/oras-www/main/static/img/oras.png)
Expand Down Expand Up @@ -51,6 +51,7 @@ tool to generate a contributors graphic below.
<td align="center" valign="top" width="14.28%"><a href="https://github.com/linshokaku"><img src="https://avatars.githubusercontent.com/u/18627646?v=4?s=100" width="100px;" alt="deoxy"/><br /><sub><b>deoxy</b></sub></a><br /><a href="https://github.com/oras-project/oras-py/commits?author=linshokaku" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kavish-p"><img src="https://avatars.githubusercontent.com/u/29086148?v=4?s=100" width="100px;" alt="Kavish Punchoo"/><br /><sub><b>Kavish Punchoo</b></sub></a><br /><a href="https://github.com/oras-project/oras-py/commits?author=kavish-p" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/my5cents"><img src="https://avatars.githubusercontent.com/u/4820203?v=4?s=100" width="100px;" alt="my5cents"/><br /><sub><b>my5cents</b></sub></a><br /><a href="https://github.com/oras-project/oras-py/commits?author=my5cents" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/tumido"><img src="https://avatars.githubusercontent.com/u/7453394?v=4?s=100" width="100px;" alt="Tom Coufal"/><br /><sub><b>Tom Coufal</b></sub></a><br /><a href="https://github.com/oras-project/oras-py/commits?author=tumido" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
22 changes: 22 additions & 0 deletions docs/getting_started/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,28 @@ def push(uri, root):

</details>

<details>

<summary>Example of basic artifact attachment</summary>

We are assuming an `derived-artifact.txt` in the present working directory and that there's already a `localhost:5000/dinosaur/artifact:v1` artifact present in the registry. Here is an example of how to [attach](https://oras.land/docs/concepts/reftypes/) a derived artifact to the existing artifact.

```python
import oras.client
import oras.oci

client = oras.client.OrasClient(insecure=True)

manifest = client.remote.get_manifest("localhost:5000/dinosaur/artifact:v1")
subject = oras.oci.Subject.from_manifest(manifest)

client.push(files=["derived-artifact.txt"], target="localhost:5000/dinosaur/artifact:v1-derived", subject=subject)
Successfully pushed localhost:5000/dinosaur/artifact:v1-derived
Out[4]: <Response [201]>
```

</details>

The above examples are just a start! See our [examples](https://github.com/oras-project/oras-py/tree/main/examples)
folder alongside the repository for more code examples and clients. If you would like help
for an example, or to contribute an example, [you know what to do](https://github.com/oras-project/oras-py/issues)!
Expand Down
26 changes: 26 additions & 0 deletions oras/oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
__license__ = "Apache-2.0"

import copy
import hashlib
import json
import os
from dataclasses import dataclass
from typing import Dict, Optional, Tuple
Expand Down Expand Up @@ -159,3 +161,27 @@ def NewManifest() -> dict:
Get an empty manifest config.
"""
return copy.deepcopy(EmptyManifest)


@dataclass
class Subject:
mediaType: str
digest: str
size: int

@classmethod
def from_manifest(cls, manifest: dict) -> "Subject":
"""
Create a new Subject from a Manifest
:param manifest: manifest to convert to subject
"""
manifest_string = json.dumps(manifest).encode("utf-8")
digest = "sha256:" + hashlib.sha256(manifest_string).hexdigest()
size = len(manifest_string)

return cls(
manifest["mediaType"] or oras.defaults.default_manifest_media_type,
digest,
size,
)
2 changes: 1 addition & 1 deletion oras/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ def push(
:param target: target location to push to
:type target: str
:param subject: optional subject reference
:type subject: Subject
:type subject: oras.oci.Subject
"""
container = self.get_container(target)
files = files or []
Expand Down
20 changes: 20 additions & 0 deletions oras/tests/test_oci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest

import oras.defaults
import oras.oci


@pytest.mark.with_auth(False)
def test_create_subject_from_manifest():
"""
Basic tests for oras Subject creation from empty manifest
"""
manifest = oras.oci.NewManifest()
subject = oras.oci.Subject.from_manifest(manifest)

assert subject.mediaType == oras.defaults.default_manifest_media_type
assert (
subject.digest
== "sha256:7a6f84d8c73a71bf9417c13f721ed102f74afac9e481f89e5a72d28954e7d0c5"
)
assert subject.size == 126
1 change: 1 addition & 0 deletions oras/tests/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import oras.client
import oras.defaults
import oras.oci
import oras.provider
import oras.utils

Expand Down

0 comments on commit cffab3a

Please sign in to comment.