@@ -1219,6 +1219,78 @@ def to_dict(self) -> Dict[str, Any]:
12191219 ** self .unrecognized_fields ,
12201220 }
12211221
1222+ @classmethod
1223+ def from_file (
1224+ cls ,
1225+ target_file_path : str ,
1226+ local_path : str ,
1227+ hash_algorithms : Optional [List [str ]] = None ,
1228+ ) -> "TargetFile" :
1229+ """Creates TargetFile object from a file.
1230+ Arguments:
1231+ target_file_path: The TargetFile path.
1232+ local_path: The local path to the file to create TargetFile from.
1233+ hash_algorithms: An optional list of hash algorithms to create
1234+ the hashes with. If not specified the securesystemslib default
1235+ hash algorithm is used.
1236+ Raises:
1237+ FileNotFoundError: The file doesn't exist.
1238+ UnsupportedAlgorithmError: The hash algorithms list
1239+ contains an unsupported algorithm.
1240+ """
1241+ with open (local_path , "rb" ) as file :
1242+ return cls .from_data (target_file_path , file , hash_algorithms )
1243+
1244+ @classmethod
1245+ def from_data (
1246+ cls ,
1247+ target_file_path : str ,
1248+ data : Union [bytes , IO [bytes ]],
1249+ hash_algorithms : Optional [List [str ]] = None ,
1250+ ) -> "TargetFile" :
1251+ """Creates TargetFile object from bytes.
1252+ Arguments:
1253+ target_file_path: The TargetFile path.
1254+ data: The data to create TargetFile from.
1255+ hash_algorithms: An optional list of hash algorithms to create
1256+ the hashes with. If not specified the securesystemslib default
1257+ hash algorithm is used.
1258+ Raises:
1259+ UnsupportedAlgorithmError: The hash algorithms list
1260+ contains an unsupported algorithm.
1261+ """
1262+ if isinstance (data , bytes ):
1263+ length = len (data )
1264+ else :
1265+ data .seek (0 , io .SEEK_END )
1266+ length = data .tell ()
1267+
1268+ hashes = {}
1269+
1270+ if hash_algorithms is None :
1271+ hash_algorithms = [sslib_hash .DEFAULT_HASH_ALGORITHM ]
1272+
1273+ for algorithm in hash_algorithms :
1274+ try :
1275+ if isinstance (data , bytes ):
1276+ digest_object = sslib_hash .digest (algorithm )
1277+ digest_object .update (data )
1278+ else :
1279+ digest_object = sslib_hash .digest_fileobject (
1280+ data , algorithm
1281+ )
1282+ except (
1283+ sslib_exceptions .UnsupportedAlgorithmError ,
1284+ sslib_exceptions .FormatError ,
1285+ ) as e :
1286+ raise exceptions .UnsupportedAlgorithmError (
1287+ f"Unsupported algorithm '{ algorithm } '"
1288+ ) from e
1289+
1290+ hashes [algorithm ] = digest_object .hexdigest ()
1291+
1292+ return cls (length , hashes , target_file_path )
1293+
12221294 def verify_length_and_hashes (self , data : Union [bytes , IO [bytes ]]) -> None :
12231295 """Verifies that length and hashes of "data" match expected values.
12241296
0 commit comments