1
1
#!/usr/bin/env python
2
- """Python Client Example. """
2
+ """TUF Client Example"""
3
3
4
4
# Copyright 2012 - 2017, New York University and the TUF contributors
5
5
# SPDX-License-Identifier: MIT OR Apache-2.0
20
20
CLIENT_EXAMPLE_DIR = os .path .dirname (os .path .abspath (__file__ ))
21
21
22
22
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"""
29
25
30
26
if not os .path .isdir (DOWNLOAD_DIR ):
31
27
os .mkdir (DOWNLOAD_DIR )
32
- print (f"Download directory [{ DOWNLOAD_DIR } ] was created" )
33
28
34
29
if not os .path .isdir (METADATA_DIR ):
35
30
os .makedirs (METADATA_DIR )
36
- print (f"Metadata folder [{ METADATA_DIR } ] was created" )
37
31
38
32
if not os .path .isfile (f"{ METADATA_DIR } /root.json" ):
39
33
shutil .copy (
@@ -45,10 +39,9 @@ def init():
45
39
print (f"Found trusted root in { METADATA_DIR } " )
46
40
47
41
48
- def download (target ) :
42
+ def download (target : str ) -> bool :
49
43
"""
50
- The function that downloads the target file using the TUF ``nglcient``
51
- Updater.
44
+ Download the target file using ``ngclient`` Updater.
52
45
53
46
The Updater refreshes the top-level metadata, get the target information,
54
47
verifies if the target is already cached, and in case it is not cached,
@@ -74,27 +67,23 @@ def download(target):
74
67
75
68
path = updater .find_cached_target (info )
76
69
if path :
77
- print (f"Target is available in { DOWNLOAD_DIR } / { info . path } " )
70
+ print (f"Target is available in { path } " )
78
71
return True
79
72
80
73
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 } " )
82
75
83
- except (FileNotFoundError , RepositoryError ) as e :
76
+ except (OSError , RepositoryError ) as e :
84
77
print (str (e ))
85
78
return False
86
79
87
80
return True
88
81
89
82
90
- if __name__ == "__main__" :
91
-
92
- # initialize the Python Client Example infrastructure
93
- init ()
83
+ def main () -> None :
84
+ """Main TUF Client Example function"""
94
85
95
- client_args = argparse .ArgumentParser (
96
- description = "TUF Python Client Example"
97
- )
86
+ client_args = argparse .ArgumentParser (description = "TUF Client Example" )
98
87
99
88
# Global arguments
100
89
client_args .add_argument (
@@ -122,7 +111,7 @@ def download(target):
122
111
123
112
command_args = client_args .parse_args ()
124
113
125
- if command_args .verbose < = 1 :
114
+ if command_args .verbose = = 1 :
126
115
loglevel = logging .ERROR
127
116
elif command_args .verbose == 2 :
128
117
loglevel = logging .WARNING
@@ -133,8 +122,15 @@ def download(target):
133
122
134
123
logging .basicConfig (level = loglevel )
135
124
125
+ # initialize the TUF Client Example infrastructure
126
+ init ()
127
+
136
128
if command_args .sub_command == "download" :
137
129
download (command_args .target )
138
130
139
131
else :
140
132
client_args .print_help ()
133
+
134
+
135
+ if __name__ == "__main__" :
136
+ main ()
0 commit comments