File: syscall.py

package info (click to toggle)
python-pymummer 0.11.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 320 kB
  • sloc: python: 1,074; sh: 55; makefile: 6
file content (30 lines) | stat: -rw-r--r-- 754 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
import sys
import subprocess

class Error (Exception): pass


def decode(x):
    try:
        s = x.decode()
    except:
        return x
    return s


def run(cmd, verbose=False):
    if verbose:
        print('Running command:', cmd, flush=True)
    try:
        output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as error:
        print('The following command failed with exit code', error.returncode, file=sys.stderr)
        print(cmd, file=sys.stderr)
        print('\nThe output was:\n', file=sys.stderr)
        print(error.output.decode(), file=sys.stderr)
        raise Error('Error running command:', cmd)
            
    if verbose:
        print(decode(output))