Provide python version of util.
authorJelmer Vernooij <jelmer@samba.org>
Sat, 30 Oct 2010 17:01:41 +0000 (19:01 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Sat, 30 Oct 2010 17:01:41 +0000 (19:01 +0200)
buildfarm/__init__.py [new file with mode: 0644]
buildfarm/tests/test_util.py [new file with mode: 0755]
buildfarm/util.py [new file with mode: 0644]

diff --git a/buildfarm/__init__.py b/buildfarm/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/buildfarm/tests/test_util.py b/buildfarm/tests/test_util.py
new file mode 100755 (executable)
index 0000000..eb2a475
--- /dev/null
@@ -0,0 +1,55 @@
+#!/usr/bin/python
+# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010
+#
+#   This program is free software; you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation; either version 3 of the License, or
+#   (at your option) any later version.
+#   
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#   
+#   You should have received a copy of the GNU General Public License
+#   along with this program; if not, write to the Free Software
+#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+import unittest
+from buildfarm import util
+
+class CountLinesTests(unittest.TestCase):
+
+    def test_simple(self):
+        self.assertEquals(2, util.count_lines("foo\nbar"))
+        self.assertEquals(1, util.count_lines("bar"))
+        self.assertEquals(1, util.count_lines(""))
+
+
+class ChangeExtensionTests(unittest.TestCase):
+
+    def test_simple(self):
+        self.assertEquals("foo.bar", util.ChangeExtension("foo.old", "bar"))
+        self.assertEquals("foo.png", util.ChangeExtension("foo.png", "png"))
+        self.assertEquals("foobar.png", util.ChangeExtension("foobar", "png"))
+
+
+class DhmTimeTests(unittest.TestCase):
+
+    def test_simple(self):
+        self.assertEquals("0s", util.dhm_time(0))
+        self.assertEquals("1m", util.dhm_time(61))
+        self.assertEquals("-", util.dhm_time(-20))
+        self.assertEquals("1d 3h 1m", util.dhm_time(97265))
+        self.assertEquals("3h 1m", util.dhm_time(10865))
+
+
+class StripHtmlTests(unittest.TestCase):
+
+    def test_simple(self):
+        self.assertEquals("", util.strip_html("<!--foo-->"))
+        self.assertEquals("bar ", util.strip_html("<!--foo-->bar <!--bloe-->"))
+        self.assertEquals("bar <bloe>", util.strip_html("<bar>bar <bloe></bar>"))
+        self.assertEquals("", util.strip_html("<bar><bloe></bloe></bar>"))
+
+        self.assertEquals("bla", util.strip_html("<a href=\"foo\">bla</a>"))
diff --git a/buildfarm/util.py b/buildfarm/util.py
new file mode 100644 (file)
index 0000000..4b24fe5
--- /dev/null
@@ -0,0 +1,113 @@
+#!/usr/bin/python
+# utility functions to support the build farm
+# Copyright (C) tridge@samba.org, 2001
+# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010
+#
+#   This program is free software; you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation; either version 2 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program; if not, write to the Free Software
+#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+import re
+
+def load_list(fname):
+    """load a list from a file, using : to separate"""
+    ret = []
+    f = open(fname, 'r')
+    try:
+        for l in f.readlines():
+            if l[0] != "#":
+                ret.append(l)
+    finally:
+        f.close()
+    return ret
+
+
+def load_hash(fname):
+    """load a hash from a file, using : to separate"""
+    lines = load_list(fname)
+    ret = {}
+    for l in lines:
+        try:
+            (key, value) = l.split(":", 1)
+        except ValueError:
+            pass # Raise error ??
+        else:
+            key = key.rstrip()
+            value = value.lstrip().rstrip("\n")
+            ret[key] = value
+    return ret
+
+
+def FileLoad(filename):
+    """read a file into a string"""
+    f = open(filename, 'r')
+    try:
+        return f.read()
+    finally:
+        f.close()
+
+
+def FileSave(filename, contents):
+    """write a string into a file"""
+    f = open(filename, 'w')
+    try:
+        f.write(contents)
+    finally:
+        f.close()
+
+def ChangeExtension(fname, ext):
+    """return a filename with a changed extension"""
+    try:
+        (base, oldext) = fname.rsplit(".", 1)
+    except ValueError:
+        return "%s.%s" % (fname, ext)
+    else:
+        return "%s.%s" % (base, ext)
+
+
+def count_lines(s):
+    """count the number of lines in a buffer"""
+    return len(s.split("\n"))
+
+
+def dhm_time(sec):
+    """display a time as days, hours, minutes"""
+    days = int(sec / (60*60*24));
+    hour = int(sec / (60*60)) % 24;
+    min = int(sec / 60) % 60;
+
+    ret = ""
+
+    if sec < 0:
+        return "-"
+
+    if days != 0:
+        return "%dd %dh %dm" % (days, hour, min)
+    if hour != 0:
+        return "%dh %dm" % (hour, min)
+    if min != 0:
+        return "%dm" % min
+    return "%ds" % sec
+
+
+def strip_html(string):
+    """simple html markup stripper"""
+    # get rid of comments
+    string = re.sub("<!\-\-(.*?)\-\->", "", string)
+
+    # and remove tags.
+    count = True
+    while count:
+        (string, count) = re.subn("<(\w+).*?>(.*?)</\\1>", "\\2", string)
+
+    return string