Split with some tests.
[cdist.git] / cdist / util.py
diff --git a/cdist/util.py b/cdist/util.py
new file mode 100644 (file)
index 0000000..082459c
--- /dev/null
@@ -0,0 +1,148 @@
+#
+#    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+#
+#    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 os
+import subprocess
+import shutil
+
+class Error(Exception):
+    def __init__(self, value):
+        self.value = value
+    def __str__(self):
+        return self.value
+    def __repr__(self):
+        return str(self)
+
+def scp_escape(n):
+    s = n.split(':')
+    assert(len(s) == 1 or len(s) == 2)
+    if len(s) == 2:
+        return '%s:"\'%s\'"' % (s[0], s[1])
+    else:
+        return '\"%s\"' % s[0]
+
+def copytree(a, b):
+    log('copy %s -> %s' % (scp_escape(b), scp_escape(b)))
+    command('scp -r %s %s' % (scp_escape(a), scp_escape(b)))
+
+def copyfile(a, b):
+    log('copy %s -> %s' % (scp_escape(a), scp_escape(b)))
+    command('scp %s %s' % (scp_escape(a), scp_escape(b)))
+
+def makedirs(d):
+    if d.find(':') == -1:
+        os.makedirs(d)
+    else:
+        s = d.split(':')
+        command('ssh %s -- mkdir -p %s' % (s[0], s[1]))
+
+def rmdir(a):
+    log('remove %s' % a)
+    os.rmdir(a)
+
+def rmtree(a):
+    log('remove %s' % a)
+    shutil.rmtree(a, ignore_errors=True)
+
+def chdir(a):
+    log('chdir %s' % a)
+    os.chdir(a)
+
+def command(c):
+    log(c)
+    r = os.system(c)
+    if (r >> 8):
+        raise Error('command %s failed' % c)
+
+def command_and_read(c):
+    log(c)
+    p = subprocess.Popen(c.split(), stdout=subprocess.PIPE)
+    f = os.fdopen(os.dup(p.stdout.fileno()))
+    return f
+
+def read_wscript_variable(directory, variable):
+    f = open('%s/wscript' % directory, 'r')
+    while True:
+        l = f.readline()
+        if l == '':
+            break
+
+        s = l.split()
+        if len(s) == 3 and s[0] == variable:
+            f.close()
+            return s[2][1:-1]
+
+    f.close()
+    return None
+
+def set_version_in_wscript(version):
+    f = open('wscript', 'rw')
+    o = open('wscript.tmp', 'w')
+    while True:
+        l = f.readline()
+        if l == '':
+            break
+
+        s = l.split()
+        if len(s) == 3 and s[0] == "VERSION":
+            print "Writing %s" % version
+            print >>o,"VERSION = '%s'" % version
+        else:
+            print >>o,l,
+    f.close()
+    o.close()
+
+    os.rename('wscript.tmp', 'wscript')
+
+def append_version_to_changelog(version):
+    try:
+        f = open('ChangeLog', 'r')
+    except:
+        log('Could not open ChangeLog')
+        return
+
+    c = f.read()
+    f.close()
+
+    f = open('ChangeLog', 'w')
+    now = datetime.datetime.now()
+    f.write('%d-%02d-%02d  Carl Hetherington  <cth@carlh.net>\n\n\t* Version %s released.\n\n' % (now.year, now.month, now.day, version))
+    f.write(c)
+
+def append_version_to_debian_changelog(version):
+    if not os.path.exists('debian'):
+        log('Could not find debian directory')
+        return
+
+    command('dch -b -v %s-1 "New upstream release."' % version)
+
+def devel_to_git(git_commit, filename):
+    if git_commit is not None:
+        filename = filename.replace('devel', '-%s' % git_commit)
+    return filename
+
+def maybe_single_to_list(s):
+    if hasattr(s, 'strip') or (not hasattr(s, '__getitem__') and not hasattr(s, '__iter__')):
+        return [s]
+    return s
+
+import globals
+
+def log(m):
+    if not globals.quiet:
+        print '\x1b[33m* %s\x1b[0m' % m
+