Thomas Waldmann avatar 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:])
         make_dir(dst)
-        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),
                      dst=abspath(dst),
                      link_dst=abspath(link_dst)),
-        shell=True, **kw)
+        **kw)
 
 
 def dirshift(path, n, token):

rsync_backup_daemon.py

 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)
     time.sleep(60)
     mount(BACKUP_DEV, BACKUP_MOUNTPOINT)
     mount_pathes(PATHES, ROOTFS)
     """do stuff after the backup finished"""
     umount_pathes(PATHES, ROOTFS)
     umount(BACKUP_MOUNTPOINT)
-    send_msg("backup ended", MSG_HOST)
+    #send_msg("backup ended", MSG_HOST)
 
 
 def do_backup():
             backup(SRC, DST, GENERATIONS, stdout=logfile, stderr=subprocess.STDOUT)
             post_backup()
         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))
             raise
 
 
 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))
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.