|
8 | 8 | ocilabeltype = "org.containers.type" |
9 | 9 |
|
10 | 10 |
|
11 | | -def engine_supports_manifest_attributes(engine): |
| 11 | +def convert_from_human_readable_size(input) -> float: |
| 12 | + sizes = [("KB", 1024), ("MB", 1024**2), ("GB", 1024**3), ("TB", 1024**4), ("B", 1)] |
| 13 | + for unit, size in sizes: |
| 14 | + if input.endswith(unit) or input.endswith(unit.lower()): |
| 15 | + return float(input[: -len(unit)]) * size |
| 16 | + |
| 17 | + return float(input) |
| 18 | + |
| 19 | + |
| 20 | +def list_artifacts(args: EngineArgType): |
| 21 | + if args.engine == "docker": |
| 22 | + return [] |
| 23 | + |
| 24 | + conman_args = [ |
| 25 | + args.engine, |
| 26 | + "artifact", |
| 27 | + "ls", |
| 28 | + "--format", |
| 29 | + ( |
| 30 | + '{"name":"oci://{{ .Repository }}:{{ .Tag }}",\ |
| 31 | + "created":"{{ .CreatedAt }}", \ |
| 32 | + "size":"{{ .Size }}", \ |
| 33 | + "ID":"{{ .Digest }}"},' |
| 34 | + ), |
| 35 | + ] |
| 36 | + output = run_cmd(conman_args).stdout.decode("utf-8").strip() |
| 37 | + if output == "": |
| 38 | + return [] |
| 39 | + |
| 40 | + artifacts = json.loads(f"[{output[:-1]}]") |
| 41 | + models = [] |
| 42 | + for artifact in artifacts: |
| 43 | + conman_args = [ |
| 44 | + args.engine, |
| 45 | + "artifact", |
| 46 | + "inspect", |
| 47 | + artifact["ID"], |
| 48 | + ] |
| 49 | + output = run_cmd(conman_args).stdout.decode("utf-8").strip() |
| 50 | + |
| 51 | + if output == "": |
| 52 | + continue |
| 53 | + inspect = json.loads(output) |
| 54 | + if "Manifest" not in inspect: |
| 55 | + continue |
| 56 | + if "artifactType" not in inspect["Manifest"]: |
| 57 | + continue |
| 58 | + if inspect["Manifest"]['artifactType'] != annotations.ArtifactTypeModelManifest: |
| 59 | + continue |
| 60 | + models += [ |
| 61 | + { |
| 62 | + "name": artifact["name"], |
| 63 | + "modified": artifact["created"], |
| 64 | + "size": convert_from_human_readable_size(artifact["size"]), |
| 65 | + } |
| 66 | + ] |
| 67 | + return models |
| 68 | + |
| 69 | + |
| 70 | +def engine_supports_manifest_attributes(engine) -> bool: |
12 | 71 | if not engine or engine == "" or engine == "docker": |
13 | 72 | return False |
14 | 73 | if engine == "podman" and engine_version(engine) < "5": |
@@ -91,26 +150,26 @@ def list_models(args: EngineArgType): |
91 | 150 | "--format", |
92 | 151 | formatLine, |
93 | 152 | ] |
| 153 | + models = [] |
94 | 154 | output = run_cmd(conman_args, env={"TZ": "UTC"}).stdout.decode("utf-8").strip() |
95 | | - if output == "": |
96 | | - return [] |
97 | | - |
98 | | - models = json.loads(f"[{output[:-1]}]") |
99 | | - # exclude dangling images having no tag (i.e. <none>:<none>) |
100 | | - models = [model for model in models if model["name"] != "oci://<none>:<none>"] |
101 | | - |
102 | | - # Grab the size from the inspect command |
103 | | - if conman == "docker": |
104 | | - # grab the size from the inspect command |
105 | | - for model in models: |
106 | | - conman_args = [conman, "image", "inspect", model["id"], "--format", "{{.Size}}"] |
107 | | - output = run_cmd(conman_args).stdout.decode("utf-8").strip() |
108 | | - # convert the number value from the string output |
109 | | - model["size"] = int(output) |
110 | | - # drop the id from the model |
111 | | - del model["id"] |
| 155 | + if output != "": |
| 156 | + models += json.loads(f"[{output[:-1]}]") |
| 157 | + # exclude dangling images having no tag (i.e. <none>:<none>) |
| 158 | + models = [model for model in models if model["name"] != "oci://<none>:<none>"] |
| 159 | + |
| 160 | + # Grab the size from the inspect command |
| 161 | + if conman == "docker": |
| 162 | + # grab the size from the inspect command |
| 163 | + for model in models: |
| 164 | + conman_args = [conman, "image", "inspect", model["id"], "--format", "{{.Size}}"] |
| 165 | + output = run_cmd(conman_args).stdout.decode("utf-8").strip() |
| 166 | + # convert the number value from the string output |
| 167 | + model["size"] = int(output) |
| 168 | + # drop the id from the model |
| 169 | + del model["id"] |
112 | 170 |
|
113 | 171 | models += list_manifests(args) |
| 172 | + models += list_artifacts(args) |
114 | 173 | for model in models: |
115 | 174 | # Convert to ISO 8601 format |
116 | 175 | parsed_date = datetime.fromisoformat( |
|
0 commit comments