forked from theupdateframework/python-tuf
/
example_client.py
executable file
·94 lines (70 loc) · 2.92 KB
/
example_client.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
<Program Name>
example_client.py
<Author>
Vladimir Diaz <vladimir.v.diaz@gmail.com>
<Started>
September 2012
<Copyright>
See LICENSE for licensing information.
<Purpose>
Example script demonstrating custom python code a software updater
utilizing The Update Framework may write to securely update files.
The 'basic_client.py' script can be used on the command-line to perform
an update that will download and update all available targets; writing
custom code is not required in this case.
The custom examples below demonstrate:
(1) updating all targets
(2) updating all the targets of a specified role
(3) updating a specific target explicitely named.
"""
import logging
import tuf.client.updater
# Uncomment the line below to enable printing of debugging information.
#tuf.log.set_log_level(logging.DEBUG)
# Set the local repository directory containing the metadata files.
tuf.conf.repository_directory = '.'
# Set the repository mirrors. This dictionary is needed by the Updater
# class of updater.py. The client will download metadata and target
# files from any one of these mirrors.
repository_mirrors = {'mirror1': {'url_prefix': 'http://localhost:8001',
'metadata_path': 'metadata',
'targets_path': 'targets',
'confined_target_dirs': ['']}}
# Create the Upater object using the updater name 'tuf-example'
# and the repository mirrors defined above.
updater = tuf.client.updater.Updater('tuf-example', repository_mirrors)
# Set the local destination directory to save the target files.
destination_directory = './targets'
# Refresh the repository's top-level roles, store the target information for
# all the targets tracked, and determine which of these targets have been
# updated.
updater.refresh()
all_targets = updater.all_targets()
updated_targets = updater.updated_targets(all_targets, destination_directory)
# Download each of these updated targets and save them locally.
for target in updated_targets:
try:
updater.download_target(target, destination_directory)
except tuf.DownloadError, e:
pass
# Remove any files from the destination directory that are no longer being
# tracked.
updater.remove_obsolete_targets(destination_directory)
"""
# Example demonstrating an update that only downloads the targets of
# a specific role (i.e., 'targets/role1')
updater.refresh()
targets_of_role1 = updater.targets_of_role('targets/role1')
updated_targets = updater.updated_targets(targets_of_role1, destination_directory)
for target in updated_targets:
updater.download_target(target, destination_directory)
"""
"""
# Example demonstrating an update that downloads a specific target.
updater.refresh()
target = updater.target('LICENSE.txt')
updated_target = updater.updated_targets([target], destination_directory)
for target in updated_target:
updater.download_target(target, destination_directory)
"""