Re-imported charm-helpers from latest merged version
This commit is contained in:
parent
313616043b
commit
201273b462
@ -27,6 +27,9 @@ class Fstab(file):
|
|||||||
self.d = d
|
self.d = d
|
||||||
self.p = p
|
self.p = p
|
||||||
|
|
||||||
|
def __eq__(self, o):
|
||||||
|
return str(self) == str(o)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "{} {} {} {} {} {}".format(self.device,
|
return "{} {} {} {} {} {}".format(self.device,
|
||||||
self.mountpoint,
|
self.mountpoint,
|
||||||
@ -44,16 +47,18 @@ class Fstab(file):
|
|||||||
self._path = self.DEFAULT_PATH
|
self._path = self.DEFAULT_PATH
|
||||||
file.__init__(self, self._path, 'r+')
|
file.__init__(self, self._path, 'r+')
|
||||||
|
|
||||||
|
def _hydrate_entry(self, line):
|
||||||
|
return Fstab.Entry(*filter(
|
||||||
|
lambda x: x not in ('', None),
|
||||||
|
line.strip("\n").split(" ")))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def entries(self):
|
def entries(self):
|
||||||
self.seek(0)
|
self.seek(0)
|
||||||
for line in self.readlines():
|
for line in self.readlines():
|
||||||
if not line.startswith("#"):
|
|
||||||
try:
|
try:
|
||||||
(dev, mp, fs, options, d, p) = filter(
|
if not line.startswith("#"):
|
||||||
lambda x: x not in ('', None),
|
yield self._hydrate_entry(line)
|
||||||
line.strip("\n").split(" "))
|
|
||||||
yield Fstab.Entry(dev, mp, fs, options, d=d, p=p)
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -69,13 +74,24 @@ class Fstab(file):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
self.write(str(entry) + '\n')
|
self.write(str(entry) + '\n')
|
||||||
|
self.truncate()
|
||||||
return entry
|
return entry
|
||||||
|
|
||||||
def remove_entry(self, entry):
|
def remove_entry(self, entry):
|
||||||
self.seek(0)
|
self.seek(0)
|
||||||
|
|
||||||
lines = self.readlines()
|
lines = self.readlines()
|
||||||
|
|
||||||
|
found = False
|
||||||
for index, line in enumerate(lines):
|
for index, line in enumerate(lines):
|
||||||
if line == str(entry) + '\n':
|
if not line.startswith("#"):
|
||||||
|
if self._hydrate_entry(line) == entry:
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not found:
|
||||||
|
return False
|
||||||
|
|
||||||
lines.remove(line)
|
lines.remove(line)
|
||||||
|
|
||||||
self.seek(0)
|
self.seek(0)
|
||||||
@ -84,14 +100,15 @@ class Fstab(file):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def remove_by_mountpoint(cls, mountpoint):
|
def remove_by_mountpoint(cls, mountpoint, path=None):
|
||||||
fstab = cls()
|
fstab = cls(path=path)
|
||||||
entry = fstab.get_entry_by_attr('mountpoint', mountpoint)
|
entry = fstab.get_entry_by_attr('mountpoint', mountpoint)
|
||||||
if entry:
|
if entry:
|
||||||
return fstab.remove_entry(entry)
|
return fstab.remove_entry(entry)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def add(cls, device, mountpoint, filesystem, options=None):
|
def add(cls, device, mountpoint, filesystem, options=None, path=None):
|
||||||
return cls().add_entry(Fstab.Entry(device, mountpoint, filesystem,
|
return cls(path=path).add_entry(Fstab.Entry(device,
|
||||||
|
mountpoint, filesystem,
|
||||||
options=options))
|
options=options))
|
||||||
|
Loading…
Reference in New Issue
Block a user