-
Notifications
You must be signed in to change notification settings - Fork 533
ENH: Add versioning metadata to crash files #2626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -628,13 +628,13 @@ def load_json(filename): | |
|
||
|
||
def loadcrash(infile, *args): | ||
if '.pkl' in infile: | ||
return loadpkl(infile) | ||
if infile.endswith('pkl') or infile.endswith('pklz'): | ||
return loadpkl(infile, versioning=True) | ||
else: | ||
raise ValueError('Only pickled crashfiles are supported') | ||
|
||
|
||
def loadpkl(infile): | ||
def loadpkl(infile, versioning=False): | ||
"""Load a zipped or plain cPickled file | ||
""" | ||
fmlogger.debug('Loading pkl: %s', infile) | ||
|
@@ -643,11 +643,44 @@ def loadpkl(infile): | |
else: | ||
pkl_file = open(infile, 'rb') | ||
|
||
if versioning: | ||
pkl_metadata = {} | ||
|
||
# Look if pkl file contains version file | ||
try: | ||
pkl_metadata_line = pkl_file.readline() | ||
pkl_metadata = json.loads(pkl_metadata_line) | ||
except: | ||
# Could not get version info | ||
pkl_file.seek(0) | ||
|
||
try: | ||
unpkl = pickle.load(pkl_file) | ||
except UnicodeDecodeError: | ||
unpkl = pickle.load(pkl_file, fix_imports=True, encoding='utf-8') | ||
return unpkl | ||
try: | ||
unpkl = pickle.load(pkl_file) | ||
except UnicodeDecodeError: | ||
unpkl = pickle.load(pkl_file, fix_imports=True, encoding='utf-8') | ||
|
||
return unpkl | ||
|
||
# Unpickling problems | ||
except Exception as e: | ||
if not versioning: | ||
raise e | ||
|
||
from nipype import __version__ as version | ||
|
||
if 'version' in pkl_metadata: | ||
if pkl_metadata['version'] != version: | ||
fmlogger.error('Your Nipype version is: %s', | ||
version) | ||
fmlogger.error('Nipype version of the pkl is: %s', | ||
pkl_metadata['version']) | ||
else: | ||
fmlogger.error('No metadata was found in the pkl file.') | ||
fmlogger.error('Make sure that you are using the same Nipype' | ||
'version from the generated pkl.') | ||
|
||
raise e | ||
|
||
|
||
def crash2txt(filename, record): | ||
|
@@ -682,11 +715,21 @@ def read_stream(stream, logger=None, encoding=None): | |
return out.splitlines() | ||
|
||
|
||
def savepkl(filename, record): | ||
def savepkl(filename, record, versioning=False): | ||
if filename.endswith('pklz'): | ||
pkl_file = gzip.open(filename, 'wb') | ||
else: | ||
pkl_file = open(filename, 'wb') | ||
|
||
if versioning: | ||
from nipype import __version__ as version | ||
metadata = json.dumps({'version': version}, | ||
ensure_ascii=True, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use UTF-8 like we do throughout the package? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My initial preference around ASCII over UTF-8, in this case, is about Python 2 retro-compatibility. I've done some tests now, and using UTF-8 will not be a problem at all. |
||
encoding='ascii') | ||
|
||
pkl_file.write(metadata.encode('ascii')) | ||
pkl_file.write('\n'.encode('ascii')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. utf-8? |
||
|
||
pickle.dump(record, pkl_file) | ||
pkl_file.close() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
many blank lines (personal preference, feel free to ignore)