Skip to content

Commit 832fecf

Browse files
committed
RF: Update and factor mount table parsing code
1 parent 9e763ba commit 832fecf

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

nipype/utils/filemanip.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -275,49 +275,48 @@ def hash_timestamp(afile):
275275
return md5hex
276276

277277

278-
def _generate_cifs_table():
279-
"""Construct a reverse-length-ordered list of mount points that
280-
fall under a CIFS mount.
278+
def _parse_mount_table(exit_code, output):
279+
"""Parses the output of ``mount`` to produce (path, fs_type) pairs
281280
282-
This precomputation allows efficient checking for whether a given path
283-
would be on a CIFS filesystem.
284-
285-
On systems without a ``mount`` command, or with no CIFS mounts, returns an
286-
empty list.
281+
Separated from _generate_cifs_table to enable testing logic with real
282+
outputs
287283
"""
288-
exit_code, output = sp.getstatusoutput("mount")
289284
# Not POSIX
290285
if exit_code != 0:
291286
return []
292287

288+
# Linux mount example: sysfs on /sys type sysfs (rw,nosuid,nodev,noexec)
289+
# <PATH>^^^^ ^^^^^<FSTYPE>
290+
# OSX mount example: /dev/disk2 on / (hfs, local, journaled)
291+
# <PATH>^ ^^^<FSTYPE>
292+
pattern = re.compile(r'.*? on (/.*?) (?:type |\()([^\s,]+)(?:, |\)| )')
293+
293294
# (path, fstype) tuples, sorted by path length (longest first)
294-
mount_info = sorted(
295-
(line.split()[2:5:2] for line in output.splitlines()),
296-
key=lambda x: len(x[0]),
297-
reverse=True)
298-
299-
# find which mount points are CIFS
300-
# init to empty list
301-
cifs_paths = []
302-
303-
try:
304-
for path_and_fstype in mount_info:
305-
# need to check for tables that have only path and no fstype
306-
if len(path_and_fstype) == 2:
307-
# if this entry is cifs, add it to list
308-
if path_and_fstype[1] == 'cifs':
309-
cifs_paths.append(path_and_fstype[0])
310-
else:
311-
fmlogger.debug('mount file system types not described by fstype')
312-
except:
313-
fmlogger.debug('mount file system type check for CIFS error')
314-
return []
295+
mount_info = sorted((pattern.match(l).groups() for l in output.splitlines()),
296+
key=lambda x: len(x[0]), reverse=True)
297+
cifs_paths = [path for path, fstype in mount_info
298+
if fstype.lower() == 'cifs']
299+
315300
return [
316301
mount for mount in mount_info
317302
if any(mount[0].startswith(path) for path in cifs_paths)
318303
]
319304

320305

306+
def _generate_cifs_table():
307+
"""Construct a reverse-length-ordered list of mount points that
308+
fall under a CIFS mount.
309+
310+
This precomputation allows efficient checking for whether a given path
311+
would be on a CIFS filesystem.
312+
313+
On systems without a ``mount`` command, or with no CIFS mounts, returns an
314+
empty list.
315+
"""
316+
exit_code, output = sp.getstatusoutput("mount")
317+
return _parse_mount_table(exit_code, output)
318+
319+
321320
_cifs_table = _generate_cifs_table()
322321

323322

0 commit comments

Comments
 (0)