@@ -628,13 +628,13 @@ def load_json(filename):
628
628
629
629
630
630
def loadcrash (infile , * args ):
631
- if '. pkl' in infile :
632
- return loadpkl (infile )
631
+ if infile . endswith ( ' pkl') or infile . endswith ( 'pklz' ) :
632
+ return loadpkl (infile , versioning = True )
633
633
else :
634
634
raise ValueError ('Only pickled crashfiles are supported' )
635
635
636
636
637
- def loadpkl (infile ):
637
+ def loadpkl (infile , versioning = False ):
638
638
"""Load a zipped or plain cPickled file
639
639
"""
640
640
fmlogger .debug ('Loading pkl: %s' , infile )
@@ -643,11 +643,44 @@ def loadpkl(infile):
643
643
else :
644
644
pkl_file = open (infile , 'rb' )
645
645
646
+ if versioning :
647
+ pkl_metadata = {}
648
+
649
+ # Look if pkl file contains version file
650
+ try :
651
+ pkl_metadata_line = pkl_file .readline ()
652
+ pkl_metadata = json .loads (pkl_metadata_line )
653
+ except :
654
+ # Could not get version info
655
+ pkl_file .seek (0 )
656
+
646
657
try :
647
- unpkl = pickle .load (pkl_file )
648
- except UnicodeDecodeError :
649
- unpkl = pickle .load (pkl_file , fix_imports = True , encoding = 'utf-8' )
650
- return unpkl
658
+ try :
659
+ unpkl = pickle .load (pkl_file )
660
+ except UnicodeDecodeError :
661
+ unpkl = pickle .load (pkl_file , fix_imports = True , encoding = 'utf-8' )
662
+
663
+ return unpkl
664
+
665
+ # Unpickling problems
666
+ except Exception as e :
667
+ if not versioning :
668
+ raise e
669
+
670
+ from nipype import __version__ as version
671
+
672
+ if 'version' in pkl_metadata :
673
+ if pkl_metadata ['version' ] != version :
674
+ fmlogger .error ('Your Nipype version is: %s' ,
675
+ version )
676
+ fmlogger .error ('Nipype version of the pkl is: %s' ,
677
+ pkl_metadata ['version' ])
678
+ else :
679
+ fmlogger .error ('No metadata was found in the pkl file.' )
680
+ fmlogger .error ('Make sure that you are using the same Nipype'
681
+ 'version from the generated pkl.' )
682
+
683
+ raise e
651
684
652
685
653
686
def crash2txt (filename , record ):
@@ -682,11 +715,19 @@ def read_stream(stream, logger=None, encoding=None):
682
715
return out .splitlines ()
683
716
684
717
685
- def savepkl (filename , record ):
718
+ def savepkl (filename , record , versioning = False ):
686
719
if filename .endswith ('pklz' ):
687
720
pkl_file = gzip .open (filename , 'wb' )
688
721
else :
689
722
pkl_file = open (filename , 'wb' )
723
+
724
+ if versioning :
725
+ from nipype import __version__ as version
726
+ metadata = json .dumps ({'version' : version })
727
+
728
+ pkl_file .write (metadata .encode ('utf-8' ))
729
+ pkl_file .write ('\n ' .encode ('utf-8' ))
730
+
690
731
pickle .dump (record , pkl_file )
691
732
pkl_file .close ()
692
733
0 commit comments