diff options
Diffstat (limited to 'cloudinit/distros/helpers.py')
-rw-r--r-- | cloudinit/distros/helpers.py | 96 |
1 files changed, 82 insertions, 14 deletions
diff --git a/cloudinit/distros/helpers.py b/cloudinit/distros/helpers.py index 251d5051..916935eb 100644 --- a/cloudinit/distros/helpers.py +++ b/cloudinit/distros/helpers.py @@ -23,6 +23,87 @@ from StringIO import StringIO from cloudinit import util +def _chop_comment(text, comment_chars): + comment_locations = [text.find(c) for c in comment_chars] + comment_locations = [c for c in comment_locations if c != -1] + if not comment_locations: + return (text, '') + min_comment = min(comment_locations) + before_comment = text[0:min_comment] + comment = text[min_comment:] + return (before_comment, comment) + + +# See: man hosts +# or http://unixhelp.ed.ac.uk/CGI/man-cgi?hosts +class HostsConf(object): + def __init__(self, text): + self._text = text + self._contents = None + + def parse(self): + if self._contents is None: + self._contents = self._parse(self._text) + + def get_entry(self, ip): + self.parse() + options = [] + for (line_type, components) in self._contents: + if line_type == 'option': + (pieces, _tail) = components + if len(pieces) and pieces[0] == ip: + options.append(pieces[1:]) + return options + + def del_entries(self, ip): + self.parse() + n_entries = [] + for (line_type, components) in self._contents: + if line_type != 'option': + n_entries.append((line_type, components)) + continue + else: + (pieces, _tail) = components + if len(pieces) and pieces[0] == ip: + pass + elif len(pieces): + n_entries.append((line_type, list(components))) + self._contents = n_entries + + def add_entry(self, ip, canonical_hostname, *aliases): + self.parse() + self._contents.append(('option', + ([ip, canonical_hostname] + list(aliases), ''))) + + def _parse(self, contents): + entries = [] + for line in contents.splitlines(): + if not len(line.strip()): + entries.append(('blank', [line])) + continue + (head, tail) = _chop_comment(line.strip(), '#') + if not len(head): + entries.append(('all_comment', [line])) + continue + entries.append(('option', [head.split(None), tail])) + return entries + + def __str__(self): + self.parse() + contents = StringIO() + for (line_type, components) in self._contents: + if line_type == 'blank': + contents.write("%s\n") + elif line_type == 'all_comment': + contents.write("%s\n" % (components[0])) + elif line_type == 'option': + (pieces, tail) = components + pieces = [str(p) for p in pieces] + pieces = "\t".join(pieces) + contents.write("%s%s\n" % (pieces, tail)) + return contents.getvalue() + + # See: man resolv.conf class ResolvConf(object): def __init__(self, text): @@ -151,20 +232,7 @@ class ResolvConf(object): if not sline: entries.append(('blank', [line])) continue - comment_s_loc = sline.find(";") - comment_h_loc = sline.find("#") - comment_loc = -1 - if comment_s_loc != -1 and comment_h_loc != -1: - comment_loc = min(comment_h_loc, comment_s_loc) - elif comment_s_loc != -1: - comment_loc = comment_s_loc - elif comment_h_loc != -1: - comment_loc = comment_h_loc - head = line - tail = None - if comment_loc != -1: - head = line[:comment_loc] - tail = line[comment_loc:] + (head, tail) = _chop_comment(line, ';#') if not len(head.strip()): entries.append(('all_comment', [line])) continue |