1import os
2import subprocess
3import errno
4
5def simple_exec(cmd):
6    print("   -> Executing command: %s" % " ".join(cmd))
7
8    retcode = subprocess.call(cmd)
9
10    if retcode != 0:
11        print("   !! ERROR: Command returned exit code %i!" % retcode)
12        print("   !!        Command: %s" % " ".join(cmd))
13        return False
14
15    return True
16
17def silent_exec(cmd):
18    print("   -> Executing command: %s" % " ".join(cmd))
19
20    FNULL = open(os.devnull, 'w')
21    retcode = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
22
23    if retcode != 0:
24        print("   !! ERROR: Command returned exit code %i!" % retcode)
25        print("   !!        Command: %s" % " ".join(cmd))
26        return False
27
28    return True
29
30def output_exec(cmd):
31    print("   -> Executing command: %s" % " ".join(cmd))
32
33    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
34    out, err = p.communicate()
35
36    if p.returncode != 0:
37        print("   !! ERROR: Command returned exit code %i!" % retcode)
38        print("   !!        Command: %s" % " ".join(cmd))
39        return None
40
41    return out
42
43# tzot @ StackOverflow:
44# http://stackoverflow.com/a/600612
45def mkdir_p(path):
46    try:
47        os.makedirs(path)
48    except OSError as exc:  # Python >2.5
49        if exc.errno == errno.EEXIST and os.path.isdir(path):
50            pass
51        else:
52            raise
53
54def silentremove(filename):
55    try:
56        os.remove(filename)
57    except OSError as e: # this would be "except OSError, e:" before Python 2.6
58        if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
59            raise # re-raise exception if a different error occured