6
6
7
7
class ModelData (object ):
8
8
def __init__ (self , client , model_manifest_path ):
9
- self .manifest_freeze_path = model_manifest_path
10
- self .manifest_data = get_manifest (self .manifest_freeze_path )
9
+ self .manifest_path = model_manifest_path
10
+ self .manifest_freeze_path = "{}.freeze" .format (self .manifest_path )
11
+ self .manifest_data = get_manifest (self .manifest_freeze_path , self .manifest_path )
11
12
self .client = client
12
13
self .models = {}
13
14
self .usr_key = "__user__"
@@ -27,7 +28,6 @@ def data(self):
27
28
output [without_usr_key ] = __dict [key ]
28
29
return output
29
30
30
-
31
31
def available (self ):
32
32
if self .manifest_data :
33
33
return True
@@ -39,14 +39,16 @@ def initialize(self):
39
39
raise Exception ("Client was not defined, please define a Client when using Model Manifests." )
40
40
for required_file in self .manifest_data ['required_files' ]:
41
41
name = required_file ['name' ]
42
+ source_uri = required_file ['source_uri' ]
43
+ fail_on_tamper = required_file .get ('fail_on_tamper' , False )
44
+ expected_hash = required_file .get ('md5_checksum' , None )
42
45
if name in self .models :
43
46
raise Exception ("Duplicate 'name' detected. \n "
44
47
+ name + " was found to be used by more than one data file, please rename." )
45
- expected_hash = required_file ['md5_checksum' ]
46
- with self .client .file (required_file ['source_uri' ]).getFile () as f :
48
+ with self .client .file (source_uri ).getFile () as f :
47
49
local_data_path = f .name
48
50
real_hash = md5_for_file (local_data_path )
49
- if real_hash != expected_hash and required_file [ ' fail_on_tamper' ] :
51
+ if real_hash != expected_hash and fail_on_tamper :
50
52
raise Exception ("Model File Mismatch for " + name +
51
53
"\n expected hash: " + expected_hash + "\n real hash: " + real_hash )
52
54
else :
@@ -70,32 +72,46 @@ def find_optional_model(self, file_name):
70
72
raise Exception ("file with name '" + file_name + "' not found in model manifest." )
71
73
model_info = found_models [0 ]
72
74
self .models [file_name ] = {}
73
- expected_hash = model_info ['md5_checksum' ]
74
- with self .client .file (model_info ['source_uri' ]).getFile () as f :
75
+ source_uri = model_info ['source_uri' ]
76
+ fail_on_tamper = model_info .get ("fail_on_tamper" , False )
77
+ expected_hash = model_info .get ('md5_checksum' , None )
78
+ with self .client .file (source_uri ).getFile () as f :
75
79
local_data_path = f .name
76
80
real_hash = md5_for_file (local_data_path )
77
- if real_hash != expected_hash and model_info [ ' fail_on_tamper' ] :
81
+ if real_hash != expected_hash and fail_on_tamper :
78
82
raise Exception ("Model File Mismatch for " + file_name +
79
83
"\n expected hash: " + expected_hash + "\n real hash: " + real_hash )
80
84
else :
81
85
self .models [file_name ] = FileData (real_hash , local_data_path )
82
86
83
87
84
- def get_manifest (manifest_path ):
85
- if os .path .exists (manifest_path ):
86
- with open (manifest_path ) as f :
88
+ def get_manifest (manifest_frozen_path , manifest_reg_path ):
89
+ if os .path .exists (manifest_frozen_path ):
90
+ with open (manifest_frozen_path ) as f :
87
91
manifest_data = json .load (f )
88
- expected_lock_checksum = manifest_data .get ('lock_checksum' )
89
- del manifest_data ['lock_checksum' ]
90
- detected_lock_checksum = md5_for_str (str (manifest_data ))
91
- if expected_lock_checksum != detected_lock_checksum :
92
+ if check_lock (manifest_data ):
93
+ return manifest_data
94
+ else :
92
95
raise Exception ("Manifest FreezeFile Tamper Detected; please use the CLI and 'algo freeze' to rebuild your "
93
96
"algorithm's freeze file." )
97
+ elif os .path .exists (manifest_reg_path ):
98
+ with open (manifest_reg_path ) as f :
99
+ manifest_data = json .load (f )
94
100
return manifest_data
95
101
else :
96
102
return None
97
103
98
104
105
+ def check_lock (manifest_data ):
106
+ expected_lock_checksum = manifest_data .get ('lock_checksum' )
107
+ del manifest_data ['lock_checksum' ]
108
+ detected_lock_checksum = md5_for_str (str (manifest_data ))
109
+ if expected_lock_checksum != detected_lock_checksum :
110
+ return False
111
+ else :
112
+ return True
113
+
114
+
99
115
def md5_for_file (fname ):
100
116
hash_md5 = hashlib .md5 ()
101
117
with open (fname , "rb" ) as f :
0 commit comments