|
3 | 3 | # vi: set ft=python sts=4 ts=4 sw=4 et: |
4 | 4 | """Miscellaneous file manipulation functions |
5 | 5 |
|
6 | | - .. testsetup:: |
7 | | - # Change directory to provide relative paths for doctests |
8 | | - >>> import os |
9 | | - >>> filepath = os.path.dirname(os.path.realpath( __file__ )) |
10 | | - >>> datadir = os.path.realpath(os.path.join(filepath, '../testing/data')) |
11 | | - >>> os.chdir(datadir) |
12 | | -
|
13 | 6 | """ |
14 | 7 | from __future__ import (print_function, division, unicode_literals, |
15 | 8 | absolute_import) |
@@ -229,38 +222,19 @@ def check_forhash(filename): |
229 | 222 | return False, None |
230 | 223 |
|
231 | 224 |
|
232 | | -def hash_infile(afile, chunk_len=8192, crypto=hashlib.md5, |
233 | | - raise_notfound=False): |
234 | | - """ |
235 | | - Computes hash of a file using 'crypto' module |
236 | | -
|
237 | | - >>> hash_infile('smri_ants_registration_settings.json') |
238 | | - '49b956387ed8d95a4eb44576fc5103b6' |
239 | | -
|
240 | | - >>> hash_infile('surf01.vtk') |
241 | | - 'fdf1cf359b4e346034372cdeb58f9a88' |
242 | | -
|
243 | | - >>> hash_infile('spminfo') |
244 | | - '0dc55e3888c98a182dab179b976dfffc' |
245 | | -
|
246 | | - >>> hash_infile('fsl_motion_outliers_fd.txt') |
247 | | - 'defd1812c22405b1ee4431aac5bbdd73' |
248 | | -
|
249 | | -
|
250 | | - """ |
251 | | - if not op.isfile(afile): |
252 | | - if raise_notfound: |
253 | | - raise RuntimeError('File "%s" not found.' % afile) |
254 | | - return None |
255 | | - |
256 | | - crypto_obj = crypto() |
257 | | - with open(afile, 'rb') as fp: |
258 | | - while True: |
259 | | - data = fp.read(chunk_len) |
260 | | - if not data: |
261 | | - break |
262 | | - crypto_obj.update(data) |
263 | | - return crypto_obj.hexdigest() |
| 225 | +def hash_infile(afile, chunk_len=8192, crypto=hashlib.md5): |
| 226 | + """ Computes hash of a file using 'crypto' module""" |
| 227 | + hex = None |
| 228 | + if op.isfile(afile): |
| 229 | + crypto_obj = crypto() |
| 230 | + with open(afile, 'rb') as fp: |
| 231 | + while True: |
| 232 | + data = fp.read(chunk_len) |
| 233 | + if not data: |
| 234 | + break |
| 235 | + crypto_obj.update(data) |
| 236 | + hex = crypto_obj.hexdigest() |
| 237 | + return hex |
264 | 238 |
|
265 | 239 |
|
266 | 240 | def hash_timestamp(afile): |
@@ -295,8 +269,23 @@ def _generate_cifs_table(): |
295 | 269 | (line.split()[2:5:2] for line in output.splitlines()), |
296 | 270 | key=lambda x: len(x[0]), |
297 | 271 | reverse=True) |
298 | | - cifs_paths = [path for path, fstype in mount_info if fstype == 'cifs'] |
299 | 272 |
|
| 273 | + # find which mount points are CIFS |
| 274 | + # init to empty list |
| 275 | + cifs_paths = [] |
| 276 | + |
| 277 | + try: |
| 278 | + for path_and_fstype in mount_info: |
| 279 | + # need to check for tables that have only path and no fstype |
| 280 | + if len(path_and_fstype) == 2: |
| 281 | + # if this entry is cifs, add it to list |
| 282 | + if path_and_fstype[1] == 'cifs': |
| 283 | + cifs_paths.append(path_and_fstype[0]) |
| 284 | + else: |
| 285 | + fmlogger.debug('mount file system types not described by fstype') |
| 286 | + except: |
| 287 | + fmlogger.debug('mount file system type check for CIFS error') |
| 288 | + return [] |
300 | 289 | return [ |
301 | 290 | mount for mount in mount_info |
302 | 291 | if any(mount[0].startswith(path) for path in cifs_paths) |
|
0 commit comments