Skip to content

Commit 183223e

Browse files
author
Kairo de Araujo
committed
Simplify README, fix some flows and output
- Simplify README and better keywords - Fix the verbosity - Better docstrings - Client flow for init and main are clear Signed-off-by: Kairo de Araujo <[email protected]>
1 parent ca13d05 commit 183223e

File tree

2 files changed

+29
-64
lines changed

2 files changed

+29
-64
lines changed

examples/client_example/README.md

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
# Python Client Example
1+
# TUF Client Example
22

3-
Introduction
4-
============
53

6-
Python Client Example, using ``python-tuf``.
4+
TUF Client Example, using ``python-tuf``.
75

8-
This Python Client Example implements the following actions:
6+
This TUF Client Example implements the following actions:
97
- Client Infrastructure Initialization
108
- Download target files from TUF Repository
119

12-
13-
Repository
14-
==========
15-
16-
This example demonstrates how to use the ``python-tuf`` to build a client
17-
application.
18-
19-
The repository will use static files.
20-
The static files are available in the ``python-tuf`` source code repository in
21-
``tests/repository_data/repository``.
10+
The example client expects to find a TUF repository running on localhost. We
11+
can use the static metadata files in ``tests/repository_data/repository``
12+
to set one up.
2213

2314
Run the repository using the Python3 built-in HTTP module, and keep this
2415
session running.
@@ -28,30 +19,8 @@ session running.
2819
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
2920
```
3021

31-
Client Example
32-
==============
33-
34-
The [Client Example source code](./client_example.py>) is available entirely
35-
in this source code repository.
36-
37-
How to use the Client Example:
38-
22+
How to use the TUF Client Example to download a target file.
3923

40-
1. Download the ``file1.txt``
41-
42-
```console
43-
$ ./client_example.py download file1.txt
44-
Download directory [./downloads] was created
45-
Metadata folder [<metadata dir>] was created
46-
Added trusted root in /Users/kdearaujo/.local/share/python-tuf-client-example
47-
Found trusted root in <metadata dir>
48-
Target downloaded and available in ./downloads/file1.txt
49-
```
50-
51-
2. Download again ``file1.txt``
52-
53-
```console
54-
$ ./client_example.py download file1.txt
55-
Found trusted root in <metadata dir>
56-
Target is available in ./downloads/file1.txt
57-
```
24+
```console
25+
$ ./client_example.py download file1.txt
26+
```

examples/client_example/client_example.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
"""Python Client Example."""
2+
"""TUF Client Example"""
33

44
# Copyright 2012 - 2017, New York University and the TUF contributors
55
# SPDX-License-Identifier: MIT OR Apache-2.0
@@ -20,20 +20,14 @@
2020
CLIENT_EXAMPLE_DIR = os.path.dirname(os.path.abspath(__file__))
2121

2222

23-
def init():
24-
"""
25-
The function that initializes the TUF client infrastructure.
26-
27-
It creates the metadata directory and adds a trusted ``root.json``.
28-
"""
23+
def init() -> None:
24+
"""Initialize local trusted metadata and create a directory for downloads"""
2925

3026
if not os.path.isdir(DOWNLOAD_DIR):
3127
os.mkdir(DOWNLOAD_DIR)
32-
print(f"Download directory [{DOWNLOAD_DIR}] was created")
3328

3429
if not os.path.isdir(METADATA_DIR):
3530
os.makedirs(METADATA_DIR)
36-
print(f"Metadata folder [{METADATA_DIR}] was created")
3731

3832
if not os.path.isfile(f"{METADATA_DIR}/root.json"):
3933
shutil.copy(
@@ -45,10 +39,9 @@ def init():
4539
print(f"Found trusted root in {METADATA_DIR}")
4640

4741

48-
def download(target):
42+
def download(target: str) -> bool:
4943
"""
50-
The function that downloads the target file using the TUF ``nglcient``
51-
Updater.
44+
Download the target file using ``ngclient`` Updater.
5245
5346
The Updater refreshes the top-level metadata, get the target information,
5447
verifies if the target is already cached, and in case it is not cached,
@@ -74,27 +67,23 @@ def download(target):
7467

7568
path = updater.find_cached_target(info)
7669
if path:
77-
print(f"Target is available in {DOWNLOAD_DIR}/{info.path}")
70+
print(f"Target is available in {path}")
7871
return True
7972

8073
path = updater.download_target(info)
81-
print(f"Target downloaded and available in {DOWNLOAD_DIR}/{info.path}")
74+
print(f"Target downloaded and available in {path}")
8275

83-
except (FileNotFoundError, RepositoryError) as e:
76+
except (OSError, RepositoryError) as e:
8477
print(str(e))
8578
return False
8679

8780
return True
8881

8982

90-
if __name__ == "__main__":
91-
92-
# initialize the Python Client Example infrastructure
93-
init()
83+
def main() -> None:
84+
"""Main TUF Client Example function"""
9485

95-
client_args = argparse.ArgumentParser(
96-
description="TUF Python Client Example"
97-
)
86+
client_args = argparse.ArgumentParser(description="TUF Client Example")
9887

9988
# Global arguments
10089
client_args.add_argument(
@@ -122,7 +111,7 @@ def download(target):
122111

123112
command_args = client_args.parse_args()
124113

125-
if command_args.verbose <= 1:
114+
if command_args.verbose == 1:
126115
loglevel = logging.ERROR
127116
elif command_args.verbose == 2:
128117
loglevel = logging.WARNING
@@ -133,8 +122,15 @@ def download(target):
133122

134123
logging.basicConfig(level=loglevel)
135124

125+
# initialize the TUF Client Example infrastructure
126+
init()
127+
136128
if command_args.sub_command == "download":
137129
download(command_args.target)
138130

139131
else:
140132
client_args.print_help()
133+
134+
135+
if __name__ == "__main__":
136+
main()

0 commit comments

Comments
 (0)