@@ -111,9 +111,12 @@ def __init__(self):
111
111
# target downloads are served from this dict
112
112
self .target_files : Dict [str , RepositoryTarget ] = {}
113
113
114
- # Whether to compute hashes and legth for meta in snapshot/timestamp
114
+ # Whether to compute hashes and length for meta in snapshot/timestamp
115
115
self .compute_metafile_hashes_length = False
116
116
117
+ # Enable hash-prefixed target file names
118
+ self .prefix_targets_with_hash = True
119
+
117
120
self .dump_dir = None
118
121
self .dump_version = 0
119
122
@@ -192,24 +195,32 @@ def fetch(self, url: str) -> Iterator[bytes]:
192
195
"""Fetches data from the given url and returns an Iterator (or yields
193
196
bytes).
194
197
"""
195
- if not self .root .consistent_snapshot :
196
- raise NotImplementedError ("non-consistent snapshot not supported" )
197
198
path = parse .urlparse (url ).path
198
199
if path .startswith ("/metadata/" ) and path .endswith (".json" ):
200
+ # figure out rolename and version
199
201
ver_and_name = path [len ("/metadata/" ) :][: - len (".json" )]
200
- # only consistent_snapshot supported ATM: timestamp is special case
201
- if ver_and_name == "timestamp" :
202
- version = None
203
- role = "timestamp"
204
- else :
202
+ # root is always version-prefixed while timestamp is always NOT
203
+ if ver_and_name .endswith ("root" ) or (
204
+ self .root .consistent_snapshot and ver_and_name != "timestamp"
205
+ ):
205
206
version , _ , role = ver_and_name .partition ("." )
206
207
version = int (version )
208
+ else :
209
+ # the file is not version-prefixed
210
+ role = ver_and_name
211
+ version = None
212
+
207
213
yield self ._fetch_metadata (role , version )
208
214
elif path .startswith ("/targets/" ):
209
215
# figure out target path and hash prefix
210
216
target_path = path [len ("/targets/" ) :]
211
217
dir_parts , sep , prefixed_filename = target_path .rpartition ("/" )
212
- prefix , _ , filename = prefixed_filename .partition ("." )
218
+ # extract the hash prefix, if any
219
+ if self .root .consistent_snapshot and self .prefix_targets_with_hash :
220
+ prefix , _ , filename = prefixed_filename .partition ("." )
221
+ else :
222
+ filename = prefixed_filename
223
+ prefix = None
213
224
target_path = f"{ dir_parts } { sep } { filename } "
214
225
215
226
yield self ._fetch_target (target_path , prefix )
@@ -257,7 +268,7 @@ def _fetch_metadata(
257
268
elif role == "targets" :
258
269
md = self .md_targets
259
270
else :
260
- md = self .md_delegates [ role ]
271
+ md = self .md_delegates . get ( role )
261
272
262
273
if md is None :
263
274
raise FetcherHTTPError (f"Unknown role { role } " , 404 )
0 commit comments