11import logging
2- import traceback
2+ import os
33
44from django .utils import timezone
55from rest_framework import serializers
@@ -28,14 +28,52 @@ class ChecksumValidator(BaseValidator):
2828
2929 label = 'Checksum Validator'
3030
31+ @classmethod
32+ def get_form (cls ):
33+ return [
34+ {
35+ 'key' : 'path' ,
36+ 'type' : 'input' ,
37+ 'templateOptions' : {
38+ 'label' : 'Path to validate' ,
39+ 'required' : True ,
40+ }
41+ },
42+ {
43+ 'key' : 'options.algorithm' ,
44+ 'type' : 'select' ,
45+ 'defaultValue' : 'SHA-256' ,
46+ 'templateOptions' : {
47+ 'label' : 'Checksum algorithm' ,
48+ 'required' : True ,
49+ 'labelProp' : 'name' ,
50+ 'valueProp' : 'value' ,
51+ 'options' : [
52+ {'name' : 'MD5' , 'value' : 'MD5' },
53+ {'name' : 'SHA-1' , 'value' : 'SHA-1' },
54+ {'name' : 'SHA-224' , 'value' : 'SHA-224' },
55+ {'name' : 'SHA-256' , 'value' : 'SHA-256' },
56+ {'name' : 'SHA-384' , 'value' : 'SHA-384' },
57+ {'name' : 'SHA-512' , 'value' : 'SHA-512' },
58+ ]
59+ }
60+ },
61+ {
62+ 'key' : 'options.expected' ,
63+ 'type' : 'input' ,
64+ 'templateOptions' : {
65+ 'label' : 'Checksum' ,
66+ 'required' : True ,
67+ }
68+ },
69+ ]
70+
3171 class Serializer (BaseValidator .Serializer ):
32- context = serializers .ChoiceField ( choices = [ 'checksum_str' , 'checksum_file' , 'xml_file' ] )
72+ context = serializers .CharField ( default = 'checksum_str' )
3373 block_size = serializers .IntegerField (default = 65536 )
3474
3575 class OptionsSerializer (BaseValidator .OptionsSerializer ):
3676 expected = serializers .CharField ()
37- rootdir = serializers .CharField (default = '' , allow_blank = True )
38- recursive = serializers .BooleanField (default = True )
3977 algorithm = serializers .ChoiceField (
4078 choices = ['MD5' , 'SHA-1' , 'SHA-224' , 'SHA-256' , 'SHA-384' , 'SHA-512' ],
4179 default = 'SHA-256' ,
@@ -52,8 +90,14 @@ def __init__(self, *args, **kwargs):
5290
5391 def validate (self , filepath , expected = None ):
5492 logger .debug ('Validating checksum of %s' % filepath )
93+
94+ if self .ip is not None :
95+ relpath = os .path .relpath (filepath , self .ip .object_path )
96+ else :
97+ relpath = filepath
98+
5599 val_obj = Validation .objects .create (
56- filename = filepath ,
100+ filename = relpath ,
57101 time_started = timezone .now (),
58102 validator = self .__class__ .__name__ ,
59103 required = self .required ,
@@ -82,14 +126,14 @@ def validate(self, filepath, expected=None):
82126 actual_checksum = calculate_checksum (filepath , algorithm = self .algorithm , block_size = self .block_size )
83127 if actual_checksum != checksum :
84128 raise ValidationError ("checksum for %s is not valid (%s != %s)" % (
85- filepath , checksum , actual_checksum
129+ relpath , checksum , actual_checksum
86130 ))
87131 passed = True
88- except Exception :
89- val_obj .message = traceback . format_exc ( )
132+ except Exception as e :
133+ val_obj .message = str ( e )
90134 raise
91135 else :
92- message = 'Successfully validated checksum of %s' % filepath
136+ message = 'Successfully validated checksum of %s' % relpath
93137 val_obj .message = message
94138 logger .info (message )
95139 finally :
0 commit comments