def _losetup_list_parse(output):
 devices = []
 for line in output.splitlines():
  parts = line.split(u':', 2)
  if (len(parts) != 3):
   continue
  (device_file, _, backing_file) = parts
  device_file = FilePath(device_file.strip().encode('utf-8'))
  left_bracket_offset = backing_file.find('(')
  backing_file = backing_file[(left_bracket_offset + 1):]
  right_bracket_offset = backing_file.rfind(')')
  backing_file = backing_file[:right_bracket_offset]
  expected_suffix_list = ['(deleted)']
  for suffix in expected_suffix_list:
   offset = backing_file.rfind(suffix)
   if (offset > (-1)):
    backing_file = backing_file[:offset]
  backing_file = backing_file.rstrip()
  backing_file = FilePath(backing_file.encode('utf-8'))
  devices.append((device_file, backing_file))
 return devices

def _losetup_list_parse ( output ) : devices = [ ] for line in output . splitlines ( ) : parts = line . split ( u':' , 2 ) if ( len ( parts ) != 3 ) : continue ( device_file , _ , backing_file ) = parts device_file = FilePath ( device_file . strip ( ) . encode ( 'utf-8' ) ) left_bracket_offset = backing_file . find ( '(' ) backing_file = backing_file [ ( left_bracket_offset + 1 ) : ] right_bracket_offset = backing_file . rfind ( ')' ) backing_file = backing_file [ : right_bracket_offset ] expected_suffix_list = [ '(deleted)' ] for suffix in expected_suffix_list : offset = backing_file . rfind ( suffix ) if ( offset > ( - 1 ) ) : backing_file = backing_file [ : offset ] backing_file = backing_file . rstrip ( ) backing_file = FilePath ( backing_file . encode ( 'utf-8' ) ) devices . append ( ( device_file , backing_file ) ) return devices
parse the output of losetup --all which varies depending on the privileges of the user .