(root)/
util-linux-2.39/
libmount/
python/
test_mount_tab.py
       1  import os
       2  import sys
       3  import stat
       4  import errno
       5  import functools as ft
       6  
       7  # use "import libmount" for in a standard way installed python binding
       8  import pylibmount as mnt
       9  
      10  def usage(tss):
      11  	print("\nUsage:\n\t{:s} <test> [testoptions]\nTests:\n".format(sys.argv[0]))
      12  	for i in tss:
      13  		print("\t{15:-s}".format(i[0]))
      14  		if i[2] != "":
      15  			print(" {:s}\n".format(i[2]))
      16  
      17  	print("\n")
      18  	return 1
      19  
      20  def mnt_run_test(tss, argv):
      21  	rc = -1
      22  	if ((len(argv) < 2) or (argv[1] == "--help") or (argv[1] == "-h")):
      23  		return usage(tss)
      24  
      25  	#mnt_init_debug(0)
      26  
      27  	i=()
      28  	for i in tss:
      29  		if i[0] == argv[1]:
      30  			rc = i[1](i, argv[1:])
      31  			if rc:
      32  				print("FAILED [rc={:d}]".format(rc))
      33  			break
      34  
      35  	if ((rc < 0) and (i == ())):
      36  		return usage(tss)
      37  	return not not rc #because !!rc is too mainstream for python
      38  
      39  def parser_errcb(tb, fname, line):
      40  	print("{:s}:{:d}: parse error".format(fname, line))
      41  	return 1
      42  
      43  def create_table(f, comments):
      44  	if not f:
      45  		return None
      46  
      47  	tb = mnt.Table()
      48  	tb.enable_comments(comments)
      49  	tb.errcb = parser_errcb
      50  
      51  	try:
      52  		tb.parse_file(f)
      53  	except Exception:
      54  		print("{:s}: parsing failed".format(f))
      55  		return None
      56  	return tb
      57  
      58  def test_copy_fs(ts, argv):
      59  	rc = -1
      60  	tb = create_table(argv[1], False)
      61  	fs = tb.find_target("/", mnt.MNT_ITER_FORWARD)
      62  	if not fs:
      63  		return rc
      64  
      65  	print("ORIGINAL:")
      66  	fs.print_debug()
      67  
      68  	fs = fs.copy_fs(None)
      69  	if not fs:
      70  		return rc
      71  	print("COPY:")
      72  	fs.print_debug()
      73  	return 0
      74  
      75  def test_parse(ts, argv):
      76  	parse_comments = False
      77  
      78  	if len(argv) == 3 and argv[2] == "--comments":
      79  		parse_comments = True
      80  	tb = create_table(argv[1], parse_comments)
      81  
      82  	if tb.intro_comment:
      83  		print("Initial comment:\n\"{:s}\"".format(tb.intro_comment))
      84  	#while ((fs = tb.next_fs()) != None):
      85  	for fs in iter(ft.partial(tb.next_fs), None):
      86  		fs.print_debug()
      87  	if tb.trailing_comment:
      88  		print("Trailing comment:\n\"{:s}\"".format(tb.trailing_comment))
      89  	return 0
      90  
      91  def test_find(ts, argv, dr):
      92  	if len(argv) != 4:
      93  		print("try --help")
      94  		return -errno.EINVAL
      95  
      96  	f, find, what = argv[1:]
      97  
      98  	tb = create_table(f, False)
      99  	if find.lower() == "source":
     100  		fs = tb.find_source(what, dr)
     101  	elif find.lower() == "target":
     102  		fs = tb.find_target(what, dr)
     103  
     104  	if not fs:
     105  		print("{:s}: not found {:s} '{:s}'".format(f, find, what))
     106  	else:
     107  		fs.print_debug()
     108  	return 0
     109  
     110  def test_find_fw(ts, argv):
     111  	return test_find(ts, argv, mnt.MNT_ITER_FORWARD)
     112  
     113  def test_find_bw(ts, argv):
     114  	return test_find(ts, argv, mnt.MNT_ITER_BACKWARD)
     115  
     116  def test_find_pair(ts, argv):
     117  	rc = -1
     118  	tb = create_table(argv[1], False)
     119  	fs = tb.find_pair(argv[2], argv[3], mnt.MNT_ITER_FORWARD)
     120  	if not fs:
     121  		return rc
     122  	fs.print_debug()
     123  	return 0
     124  
     125  def test_is_mounted(ts, argv):
     126  	rc = -1
     127  	tb = mnt.Tab(path="/proc/self/mountinfo")
     128  	if not tb:
     129  		print("failed to parse mountinto")
     130  		return rc
     131  
     132  	fstab = create_table(argv[1], False)
     133  	if not fstab:
     134  		return rc
     135  	fs = ()
     136  	for fs in ft.iter(tb.next_fs(), -1):
     137  		if tb.is_fs_mounted(fs):
     138  			print("{:s} already mounted on {:s}".format(fs.source, fs.target))
     139  		else:
     140  			print("{:s} not mounted on {:s}".format(fs.source, fs.target))
     141  	return 0
     142  
     143  def test_find_mountpoint(ts, argv):
     144  	rc = -1
     145  	tb = mnt.Table("/proc/self/mountinfo")
     146  	if not tb:
     147  		return rc
     148  	fs = tb.find_mountpoint(argv[1], mnt.MNT_ITER_BACKWARD)
     149  	if not fs:
     150  		return rc
     151  	fs.print_debug()
     152  	return 0
     153  
     154  
     155  tss = (
     156  	( "--parse",    test_parse,        "<file> [--comments] parse and print(tab" ),
     157  	( "--find-forward",  test_find_fw, "<file> <source|target> <string>" ),
     158  	( "--find-backward", test_find_bw, "<file> <source|target> <string>" ),
     159  	( "--find-pair",     test_find_pair, "<file> <source> <target>" ),
     160  	( "--find-mountpoint", test_find_mountpoint, "<path>" ),
     161  	( "--copy-fs",       test_copy_fs, "<file>  copy root FS from the file" ),
     162  	( "--is-mounted",    test_is_mounted, "<fstab> check what from <file> are already mounted" ),
     163  )
     164  sys.exit(mnt_run_test(tss, sys.argv))