~openstack-charmers/charms/trusty/nova-cell/next

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/fstab.py

  • Committer: Liam Young
  • Date: 2015-01-29 13:02:55 UTC
  • Revision ID: liam.young@canonical.com-20150129130255-x5kmerk9e0jw81ql
[gnuoy] sync charm-helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
# -*- coding: utf-8 -*-
3
3
 
 
4
# Copyright 2014-2015 Canonical Limited.
 
5
#
 
6
# This file is part of charm-helpers.
 
7
#
 
8
# charm-helpers is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU Lesser General Public License version 3 as
 
10
# published by the Free Software Foundation.
 
11
#
 
12
# charm-helpers is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU Lesser General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU Lesser General Public License
 
18
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
4
20
__author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>'
5
21
 
 
22
import io
6
23
import os
7
24
 
8
25
 
9
 
class Fstab(file):
 
26
class Fstab(io.FileIO):
10
27
    """This class extends file in order to implement a file reader/writer
11
28
    for file `/etc/fstab`
12
29
    """
24
41
                options = "defaults"
25
42
 
26
43
            self.options = options
27
 
            self.d = d
28
 
            self.p = p
 
44
            self.d = int(d)
 
45
            self.p = int(p)
29
46
 
30
47
        def __eq__(self, o):
31
48
            return str(self) == str(o)
45
62
            self._path = path
46
63
        else:
47
64
            self._path = self.DEFAULT_PATH
48
 
        file.__init__(self, self._path, 'r+')
 
65
        super(Fstab, self).__init__(self._path, 'rb+')
49
66
 
50
67
    def _hydrate_entry(self, line):
51
68
        # NOTE: use split with no arguments to split on any
58
75
    def entries(self):
59
76
        self.seek(0)
60
77
        for line in self.readlines():
 
78
            line = line.decode('us-ascii')
61
79
            try:
62
 
                if not line.startswith("#"):
 
80
                if line.strip() and not line.startswith("#"):
63
81
                    yield self._hydrate_entry(line)
64
82
            except ValueError:
65
83
                pass
75
93
        if self.get_entry_by_attr('device', entry.device):
76
94
            return False
77
95
 
78
 
        self.write(str(entry) + '\n')
 
96
        self.write((str(entry) + '\n').encode('us-ascii'))
79
97
        self.truncate()
80
98
        return entry
81
99
 
82
100
    def remove_entry(self, entry):
83
101
        self.seek(0)
84
102
 
85
 
        lines = self.readlines()
 
103
        lines = [l.decode('us-ascii') for l in self.readlines()]
86
104
 
87
105
        found = False
88
106
        for index, line in enumerate(lines):
97
115
        lines.remove(line)
98
116
 
99
117
        self.seek(0)
100
 
        self.write(''.join(lines))
 
118
        self.write(''.join(lines).encode('us-ascii'))
101
119
        self.truncate()
102
120
        return True
103
121