Commit 858905fb authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Move ShellWriter class to utils



Also add unittest.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 9dc71d5a
......@@ -40,43 +40,6 @@ from ganeti import build
from ganeti import _autoconf
class ShellWriter:
"""Helper class to write scripts with indentation.
"""
INDENT_STR = " "
def __init__(self, fh):
self._fh = fh
self._indent = 0
def IncIndent(self):
"""Increase indentation level by 1.
"""
self._indent += 1
def DecIndent(self):
"""Decrease indentation level by 1.
"""
assert self._indent > 0
self._indent -= 1
def Write(self, txt, *args):
"""Write line to output file.
"""
self._fh.write(self._indent * self.INDENT_STR)
if args:
self._fh.write(txt % args)
else:
self._fh.write(txt)
self._fh.write("\n")
def WritePreamble(sw):
"""Writes the script preamble.
......@@ -630,7 +593,7 @@ def GetCommands(filename, module):
def main():
buf = StringIO()
sw = ShellWriter(buf)
sw = utils.ShellWriter(buf)
WritePreamble(sw)
......
......@@ -1548,6 +1548,48 @@ def ShellQuoteArgs(args):
return ' '.join([ShellQuote(i) for i in args])
class ShellWriter:
"""Helper class to write scripts with indentation.
"""
INDENT_STR = " "
def __init__(self, fh):
"""Initializes this class.
"""
self._fh = fh
self._indent = 0
def IncIndent(self):
"""Increase indentation level by 1.
"""
self._indent += 1
def DecIndent(self):
"""Decrease indentation level by 1.
"""
assert self._indent > 0
self._indent -= 1
def Write(self, txt, *args):
"""Write line to output file.
"""
assert self._indent >= 0
self._fh.write(self._indent * self.INDENT_STR)
if args:
self._fh.write(txt % args)
else:
self._fh.write(txt)
self._fh.write("\n")
def ListVisibleFiles(path):
"""Returns a list of visible files in a directory.
......
......@@ -38,6 +38,7 @@ import time
import unittest
import warnings
import OpenSSL
from cStringIO import StringIO
import testutils
from ganeti import constants
......@@ -2242,5 +2243,48 @@ class TestIgnoreProcessNotFound(unittest.TestCase):
self.assertFalse(utils.IgnoreProcessNotFound(os.kill, pid, 0))
class TestShellWriter(unittest.TestCase):
def test(self):
buf = StringIO()
sw = utils.ShellWriter(buf)
sw.Write("#!/bin/bash")
sw.Write("if true; then")
sw.IncIndent()
try:
sw.Write("echo true")
sw.Write("for i in 1 2 3")
sw.Write("do")
sw.IncIndent()
try:
self.assertEqual(sw._indent, 2)
sw.Write("date")
finally:
sw.DecIndent()
sw.Write("done")
finally:
sw.DecIndent()
sw.Write("echo %s", utils.ShellQuote("Hello World"))
sw.Write("exit 0")
self.assertEqual(sw._indent, 0)
output = buf.getvalue()
self.assert_(output.endswith("\n"))
lines = output.splitlines()
self.assertEqual(len(lines), 9)
self.assertEqual(lines[0], "#!/bin/bash")
self.assert_(re.match(r"^\s+date$", lines[5]))
self.assertEqual(lines[7], "echo 'Hello World'")
def testEmpty(self):
buf = StringIO()
sw = utils.ShellWriter(buf)
sw = None
self.assertEqual(buf.getvalue(), "")
if __name__ == '__main__':
testutils.GanetiTestProgram()
Markdown is supported
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