Revision 858905fb

b/autotools/build-bash-completion
40 40
from ganeti import _autoconf
41 41

  
42 42

  
43
class ShellWriter:
44
  """Helper class to write scripts with indentation.
45

  
46
  """
47
  INDENT_STR = "  "
48

  
49
  def __init__(self, fh):
50
    self._fh = fh
51
    self._indent = 0
52

  
53
  def IncIndent(self):
54
    """Increase indentation level by 1.
55

  
56
    """
57
    self._indent += 1
58

  
59
  def DecIndent(self):
60
    """Decrease indentation level by 1.
61

  
62
    """
63
    assert self._indent > 0
64
    self._indent -= 1
65

  
66
  def Write(self, txt, *args):
67
    """Write line to output file.
68

  
69
    """
70
    self._fh.write(self._indent * self.INDENT_STR)
71

  
72
    if args:
73
      self._fh.write(txt % args)
74
    else:
75
      self._fh.write(txt)
76

  
77
    self._fh.write("\n")
78

  
79

  
80 43
def WritePreamble(sw):
81 44
  """Writes the script preamble.
82 45

  
......
630 593

  
631 594
def main():
632 595
  buf = StringIO()
633
  sw = ShellWriter(buf)
596
  sw = utils.ShellWriter(buf)
634 597

  
635 598
  WritePreamble(sw)
636 599

  
b/lib/utils.py
1548 1548
  return ' '.join([ShellQuote(i) for i in args])
1549 1549

  
1550 1550

  
1551
class ShellWriter:
1552
  """Helper class to write scripts with indentation.
1553

  
1554
  """
1555
  INDENT_STR = "  "
1556

  
1557
  def __init__(self, fh):
1558
    """Initializes this class.
1559

  
1560
    """
1561
    self._fh = fh
1562
    self._indent = 0
1563

  
1564
  def IncIndent(self):
1565
    """Increase indentation level by 1.
1566

  
1567
    """
1568
    self._indent += 1
1569

  
1570
  def DecIndent(self):
1571
    """Decrease indentation level by 1.
1572

  
1573
    """
1574
    assert self._indent > 0
1575
    self._indent -= 1
1576

  
1577
  def Write(self, txt, *args):
1578
    """Write line to output file.
1579

  
1580
    """
1581
    assert self._indent >= 0
1582

  
1583
    self._fh.write(self._indent * self.INDENT_STR)
1584

  
1585
    if args:
1586
      self._fh.write(txt % args)
1587
    else:
1588
      self._fh.write(txt)
1589

  
1590
    self._fh.write("\n")
1591

  
1592

  
1551 1593
def ListVisibleFiles(path):
1552 1594
  """Returns a list of visible files in a directory.
1553 1595

  
b/test/ganeti.utils_unittest.py
38 38
import unittest
39 39
import warnings
40 40
import OpenSSL
41
from cStringIO import StringIO
41 42

  
42 43
import testutils
43 44
from ganeti import constants
......
2242 2243
    self.assertFalse(utils.IgnoreProcessNotFound(os.kill, pid, 0))
2243 2244

  
2244 2245

  
2246
class TestShellWriter(unittest.TestCase):
2247
  def test(self):
2248
    buf = StringIO()
2249
    sw = utils.ShellWriter(buf)
2250
    sw.Write("#!/bin/bash")
2251
    sw.Write("if true; then")
2252
    sw.IncIndent()
2253
    try:
2254
      sw.Write("echo true")
2255

  
2256
      sw.Write("for i in 1 2 3")
2257
      sw.Write("do")
2258
      sw.IncIndent()
2259
      try:
2260
        self.assertEqual(sw._indent, 2)
2261
        sw.Write("date")
2262
      finally:
2263
        sw.DecIndent()
2264
      sw.Write("done")
2265
    finally:
2266
      sw.DecIndent()
2267
    sw.Write("echo %s", utils.ShellQuote("Hello World"))
2268
    sw.Write("exit 0")
2269

  
2270
    self.assertEqual(sw._indent, 0)
2271

  
2272
    output = buf.getvalue()
2273

  
2274
    self.assert_(output.endswith("\n"))
2275

  
2276
    lines = output.splitlines()
2277
    self.assertEqual(len(lines), 9)
2278
    self.assertEqual(lines[0], "#!/bin/bash")
2279
    self.assert_(re.match(r"^\s+date$", lines[5]))
2280
    self.assertEqual(lines[7], "echo 'Hello World'")
2281

  
2282
  def testEmpty(self):
2283
    buf = StringIO()
2284
    sw = utils.ShellWriter(buf)
2285
    sw = None
2286
    self.assertEqual(buf.getvalue(), "")
2287

  
2288

  
2245 2289
if __name__ == '__main__':
2246 2290
  testutils.GanetiTestProgram()

Also available in: Unified diff