Commit 283f26c3 authored by dylan grafmyre's avatar dylan grafmyre
Browse files

mount delete

parent d27a5208
......@@ -18,6 +18,7 @@ except ImportError as err:
ARGP = argparse.ArgumentParser()
ARGP.add_argument('--set-mount', nargs=2)
ARGP.add_argument('--del-mount')
ARGP.add_argument('--mod-dir', '-m', required=True)
ARGP.add_argument('--normpath', action='store_true')
......@@ -25,6 +26,7 @@ ARGP.add_argument('--normpath', action='store_true')
def open_mount(mod_dir, *args, **kwargs):
return open(os.path.join(mod_dir, 'cfg/mount.cfg'), *args, **kwargs)
def main_list(argp):
with open_mount(argp.mod_dir) as fh:
cfg = pyvmf.Cfg.load(fh)
......@@ -34,6 +36,7 @@ def main_list(argp):
value = os.path.normpath(value)
print('{} {}'.format(key, value))
def update_mount(buf, name, path):
ret = io.StringIO()
re_replace = re.compile((
......@@ -57,10 +60,12 @@ def update_mount(buf, name, path):
return ret.getvalue()
RE_EOB = re.compile(
r'^(\s*\}\s*)$',
re.MULTILINE
)
def add_mount(buf, name, path):
ret = io.StringIO()
match = RE_EOB.search(buf)
......@@ -84,7 +89,6 @@ def add_mount(buf, name, path):
return ret.getvalue()
def main_set_mount(argp):
name, path = argp.set_mount
if argp.normpath:
......@@ -111,11 +115,66 @@ def main_set_mount(argp):
fh.flush()
LOGGER.info('wrote %r', nbytes)
def del_mount(buf, name):
ret = io.StringIO()
re_replace = re.compile((
r'^(\s*"{}"\s*")[^"]*(")'
.format(name)
), re.MULTILINE)
match = re_replace.search(buf)
if not match:
raise NotImplementedError('failed to delete mount, no match for {!r} on \n\n\t{!r}'.format(
re_replace,
buf
))
match_span = match.span()
ret.write(buf[:match_span[0]])
has_trailing_newline = buf[match_span[1]] == '\n'
if has_trailing_newline:
ret.write(buf[match_span[1]+1:])
else:
LOGGER.warning('did not find+remove expected trailing newline after mount spec'))
ret.write(buf[match_span[1]:])
return ret.getvalue()
def main_del_mount(argp):
name = argp.del_mount
with open_mount(argp.mod_dir) as fh:
cfg = pyvmf.Cfg.load(fh)
if name not in cfg.attributes:
LOGGER.warning('cfg contains no mount {!r}'.format(name))
return 0
with open_mount(argp.mod_dir, 'r+') as fh:
buf = fh.read()
mbuf = del_mount(buf, name)
sanity = pyvmf.Cfg.loads(mbuf)
fh.seek(0)
nbytes = fh.write(mbuf)
fh.truncate()
fh.flush()
LOGGER.info('wrote %r', nbytes)
def main_mount(argp):
if argp.del_mount:
return main_del_mount(argp)
if argp.set_mount:
return main_set_mount(argp)
return main_list(argp)
def main(argp=None, argv=None):
if argp is None:
argp = ARGP.parse_args(argv)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment