File: client.py

package info (click to toggle)
golang-github-theupdateframework-go-tuf 0.5.2-5~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 7,596 kB
  • sloc: python: 163; sh: 37; makefile: 12
file content (69 lines) | stat: -rw-r--r-- 1,824 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python

# Copyright 2012 - 2023, New York University and the TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0
#
# A simplified version of client_exmample.py from the Python implementation:
# https://github.com/theupdateframework/python-tuf/blob/v1.0.0/examples/client_example/client_example.py

import argparse

from typing import List
from pathlib import Path

import tuf.api

from tuf.ngclient import Updater


def update_client(repo: str, targets: List[str]):
    metadata_dir = Path("tufrepo/metadata/current")
    targets_dir = Path("tuftargets")
    targets_dir.mkdir()
    updater = Updater(
        metadata_dir=str(metadata_dir),
        metadata_base_url=f"{repo}/repository/",
        target_base_url=f"{repo}/repository/targets/",
        target_dir=str(targets_dir),
    )

    updater.refresh()
    for target in targets:
        info = updater.get_targetinfo(target)
        assert not updater.find_cached_target(info)
        updater.download_target(info)


def parse_arguments():
    parser = argparse.ArgumentParser(description="Retrieve file from TUF repository.")

    parser.add_argument(
        "-r",
        "--repo",
        type=str,
        required=True,
        metavar="<URI>",
        help="Specify the remote repository's URI"
        " (e.g., http://www.example.com:8001/tuf/).  The client retrieves"
        " updates from the remote repository.",
    )

    parser.add_argument(
        "targets",
        nargs="+",
        metavar="<file>",
        help="Specify"
        " the target files to retrieve from the specified TUF repository.",
    )

    parsed_arguments = parser.parse_args()
    return parsed_arguments


def main():
    arguments = parse_arguments()
    update_client(arguments.repo, arguments.targets)


if __name__ == "__main__":
    main()