Thomas Waldmann committed 65bcd55 Draft

use exec_shell, make exec_cmd.* work exception-based

Comments (0)

Files changed (5)

 def exec_cmd(cmd, input=None, **kw):
-    """execute cmd, give it <input> as stdin, return its stdout output,
-    stderr output and return code.
+    """execute cmd, give it <input> as stdin,
+       return its stdout and stderr output.
     p = subprocess.Popen(cmd, **kw)
     data, errors = p.communicate(input)
-    return data, errors, p.returncode
+    if p.returncode != 0:
+        raise subprocess.CalledProcessError(p.returncode, cmd, data)
+    return data, errors
 def exec_shell(cmd, input=None,
 import os
 import errno
-from subprocess import check_call as cmd
+from exec_cmd import exec_shell as cmd
 def make_dir(path):
 def mount(src, mountpoint):
     """mount src on mountpoint"""
-    cmd("mount %(src)s %(mountpoint)s" % dict(src=src, mountpoint=mountpoint),
-        shell=True)
+    cmd("mount %(src)s %(mountpoint)s" % dict(src=src, mountpoint=mountpoint))
     # TODO: check if mount succeeded with os.path.ismount(mountpoint)
 def umount(mountpoint):
     """umount mountpoint"""
-    cmd("umount %(mountpoint)s" % dict(mountpoint=mountpoint),
-        shell=True)
+    cmd("umount %(mountpoint)s" % dict(mountpoint=mountpoint))
 def _process_pathes(command, pathes, dst_path, root_path="/", **kw):
     for path in pathes:
         dst = os.path.join(dst_path, path[prefix_len:])
-        cmd(command % dict(src=path, dst=dst), shell=True)
+        cmd(command % dict(src=path, dst=dst))
 def mount_pathes(pathes, dst_path, root_path="/", **kw):
 import errno
 import subprocess
 import shutil
+from exec_cmd import exec_shell as cmd
 RSYNC = "rsync -avH --numeric-ids --link-dest=%(link_dst)s %(src)s/ %(dst)s/"
-cmd = subprocess.check_call
 def join(path, n, token):
     if n == 0:
         # there is only ONE "0" directory, no token appended
     cmd(RSYNC % dict(src=abspath(src),
-        shell=True, **kw)
+        **kw)
 def dirshift(path, n, token):

 import time
 import subprocess
-from rsync_backup import backup, cmd
+from rsync_backup import backup
+from exec_cmd import exec_shell as cmd
 from fs import mount_pathes, umount_pathes, mount, umount
 from smb import send_msg
 def pre_backup():
     """do stuff before the backup begins"""
-    send_msg("backup starts in 60s...", MSG_HOST)
+    #send_msg("backup starts in 60s...", MSG_HOST)
     mount_pathes(PATHES, ROOTFS)
     """do stuff after the backup finished"""
     umount_pathes(PATHES, ROOTFS)
-    send_msg("backup ended", MSG_HOST)
+    #send_msg("backup ended", MSG_HOST)
 def do_backup():
             backup(SRC, DST, GENERATIONS, stdout=logfile, stderr=subprocess.STDOUT)
         except Exception as err:
-            send_msg("backup crashed!", MSG_HOST)
-            #logfile.write("Backup crashed, Exception:\n")
-            #logfile.write(str(err))
+            #send_msg("backup crashed!", MSG_HOST)
+            logfile.write("Backup crashed, Exception:\n")
+            logfile.write(str(err))
 license: BSD
-from subprocess import check_call as cmd
+from exec_cmd import exec_shell as cmd
 SENDMSG = """echo "%(msg)s" | smbclient -U%(user)s%%%(password)s -M %(hostname)s"""
             user = "server" # it will say "message from <user> ..." in the popup
         if password is None:
             password = user
-    cmd(SENDMSG % dict(msg=msg, hostname=hostname, user=user, password=password),
-        shell=True)
+    cmd(SENDMSG % dict(msg=msg, hostname=hostname, user=user, password=password))