From e511566f98d0374c429412e69d9c032d752341e5 Mon Sep 17 00:00:00 2001 From: daniel glen Date: Tue, 6 Feb 2018 19:20:27 -0500 Subject: [PATCH 1/4] fstype parsing for cifs can fail on Mac OS --- nipype/utils/filemanip.py | 69 ++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/nipype/utils/filemanip.py b/nipype/utils/filemanip.py index 97a7538780..774c1eeadc 100644 --- a/nipype/utils/filemanip.py +++ b/nipype/utils/filemanip.py @@ -3,13 +3,6 @@ # vi: set ft=python sts=4 ts=4 sw=4 et: """Miscellaneous file manipulation functions - .. testsetup:: - # Change directory to provide relative paths for doctests - >>> import os - >>> filepath = os.path.dirname(os.path.realpath( __file__ )) - >>> datadir = os.path.realpath(os.path.join(filepath, '../testing/data')) - >>> os.chdir(datadir) - """ from __future__ import (print_function, division, unicode_literals, absolute_import) @@ -229,38 +222,19 @@ def check_forhash(filename): return False, None -def hash_infile(afile, chunk_len=8192, crypto=hashlib.md5, - raise_notfound=False): - """ - Computes hash of a file using 'crypto' module - - >>> hash_infile('smri_ants_registration_settings.json') - '49b956387ed8d95a4eb44576fc5103b6' - - >>> hash_infile('surf01.vtk') - 'fdf1cf359b4e346034372cdeb58f9a88' - - >>> hash_infile('spminfo') - '0dc55e3888c98a182dab179b976dfffc' - - >>> hash_infile('fsl_motion_outliers_fd.txt') - 'defd1812c22405b1ee4431aac5bbdd73' - - - """ - if not op.isfile(afile): - if raise_notfound: - raise RuntimeError('File "%s" not found.' % afile) - return None - - crypto_obj = crypto() - with open(afile, 'rb') as fp: - while True: - data = fp.read(chunk_len) - if not data: - break - crypto_obj.update(data) - return crypto_obj.hexdigest() +def hash_infile(afile, chunk_len=8192, crypto=hashlib.md5): + """ Computes hash of a file using 'crypto' module""" + hex = None + if op.isfile(afile): + crypto_obj = crypto() + with open(afile, 'rb') as fp: + while True: + data = fp.read(chunk_len) + if not data: + break + crypto_obj.update(data) + hex = crypto_obj.hexdigest() + return hex def hash_timestamp(afile): @@ -295,8 +269,23 @@ def _generate_cifs_table(): (line.split()[2:5:2] for line in output.splitlines()), key=lambda x: len(x[0]), reverse=True) - cifs_paths = [path for path, fstype in mount_info if fstype == 'cifs'] + # find which mount points are CIFS + # init to empty list + cifs_paths = [] + + try: + for path_and_fstype in mount_info: + # need to check for tables that have only path and no fstype + if len(path_and_fstype) == 2: + # if this entry is cifs, add it to list + if path_and_fstype[1] == 'cifs': + cifs_paths.append(path_and_fstype[0]) + else: + fmlogger.debug('mount file system types not described by fstype') + except: + fmlogger.debug('mount file system type check for CIFS error') + return [] return [ mount for mount in mount_info if any(mount[0].startswith(path) for path in cifs_paths) From 547e2de2f614750e0cf069bab72d46e1d4c81e8d Mon Sep 17 00:00:00 2001 From: daniel glen Date: Tue, 6 Feb 2018 19:24:43 -0500 Subject: [PATCH 2/4] fstype parsing for cifs can fail on Mac OS --- nipype/utils/filemanip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nipype/utils/filemanip.py b/nipype/utils/filemanip.py index 774c1eeadc..1a3270b068 100644 --- a/nipype/utils/filemanip.py +++ b/nipype/utils/filemanip.py @@ -281,7 +281,7 @@ def _generate_cifs_table(): # if this entry is cifs, add it to list if path_and_fstype[1] == 'cifs': cifs_paths.append(path_and_fstype[0]) - else: + else: fmlogger.debug('mount file system types not described by fstype') except: fmlogger.debug('mount file system type check for CIFS error') From a0d48fd1623424d66e55114002b7d618305b6e55 Mon Sep 17 00:00:00 2001 From: daniel glen Date: Wed, 7 Feb 2018 12:44:13 -0500 Subject: [PATCH 3/4] more proper integration --- nipype/utils/filemanip.py | 52 +++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/nipype/utils/filemanip.py b/nipype/utils/filemanip.py index 1a3270b068..83f2a9ead2 100644 --- a/nipype/utils/filemanip.py +++ b/nipype/utils/filemanip.py @@ -3,6 +3,13 @@ # vi: set ft=python sts=4 ts=4 sw=4 et: """Miscellaneous file manipulation functions + .. testsetup:: + # Change directory to provide relative paths for doctests + >>> import os + >>> filepath = os.path.dirname(os.path.realpath( __file__ )) + >>> datadir = os.path.realpath(os.path.join(filepath, '../testing/data')) + >>> os.chdir(datadir) + """ from __future__ import (print_function, division, unicode_literals, absolute_import) @@ -222,19 +229,38 @@ def check_forhash(filename): return False, None -def hash_infile(afile, chunk_len=8192, crypto=hashlib.md5): - """ Computes hash of a file using 'crypto' module""" - hex = None - if op.isfile(afile): - crypto_obj = crypto() - with open(afile, 'rb') as fp: - while True: - data = fp.read(chunk_len) - if not data: - break - crypto_obj.update(data) - hex = crypto_obj.hexdigest() - return hex +def hash_infile(afile, chunk_len=8192, crypto=hashlib.md5, + raise_notfound=False): + """ + Computes hash of a file using 'crypto' module + + >>> hash_infile('smri_ants_registration_settings.json') + '49b956387ed8d95a4eb44576fc5103b6' + + >>> hash_infile('surf01.vtk') + 'fdf1cf359b4e346034372cdeb58f9a88' + + >>> hash_infile('spminfo') + '0dc55e3888c98a182dab179b976dfffc' + + >>> hash_infile('fsl_motion_outliers_fd.txt') + 'defd1812c22405b1ee4431aac5bbdd73' + + + """ + if not op.isfile(afile): + if raise_notfound: + raise RuntimeError('File "%s" not found.' % afile) + return None + + crypto_obj = crypto() + with open(afile, 'rb') as fp: + while True: + data = fp.read(chunk_len) + if not data: + break + crypto_obj.update(data) + return crypto_obj.hexdigest() def hash_timestamp(afile): From 9e763badde423611621c670dbd2954b0f9a7ecbf Mon Sep 17 00:00:00 2001 From: daniel glen Date: Wed, 7 Feb 2018 13:44:32 -0500 Subject: [PATCH 4/4] tab/space fix --- nipype/utils/filemanip.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/nipype/utils/filemanip.py b/nipype/utils/filemanip.py index 83f2a9ead2..6773d19d8b 100644 --- a/nipype/utils/filemanip.py +++ b/nipype/utils/filemanip.py @@ -301,17 +301,17 @@ def _generate_cifs_table(): cifs_paths = [] try: - for path_and_fstype in mount_info: - # need to check for tables that have only path and no fstype - if len(path_and_fstype) == 2: - # if this entry is cifs, add it to list - if path_and_fstype[1] == 'cifs': - cifs_paths.append(path_and_fstype[0]) - else: - fmlogger.debug('mount file system types not described by fstype') + for path_and_fstype in mount_info: + # need to check for tables that have only path and no fstype + if len(path_and_fstype) == 2: + # if this entry is cifs, add it to list + if path_and_fstype[1] == 'cifs': + cifs_paths.append(path_and_fstype[0]) + else: + fmlogger.debug('mount file system types not described by fstype') except: - fmlogger.debug('mount file system type check for CIFS error') - return [] + fmlogger.debug('mount file system type check for CIFS error') + return [] return [ mount for mount in mount_info if any(mount[0].startswith(path) for path in cifs_paths)