summaryrefslogtreecommitdiff
path: root/utils/lit/lit/ShCommands.py
blob: 431f0074a634d6bffb9148602459aca65218d80e (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Command:
    def __init__(self, args, redirects):
        self.args = list(args)
        self.redirects = list(redirects)

    def __repr__(self):
        return 'Command(%r, %r)' % (self.args, self.redirects)

    def __cmp__(self, other):
        if not isinstance(other, Command):
            return -1

        return cmp((self.args, self.redirects),
                   (other.args, other.redirects))

    def toShell(self, file):
        for arg in self.args:
            if "'" not in arg:
                quoted = "'%s'" % arg
            elif '"' not in arg and '$' not in arg:
                quoted = '"%s"' % arg
            else:
                raise NotImplementedError('Unable to quote %r' % arg)
            file.write(quoted)

            # For debugging / validation.
            import ShUtil
            dequoted = list(ShUtil.ShLexer(quoted).lex())
            if dequoted != [arg]:
                raise NotImplementedError('Unable to quote %r' % arg)

        for r in self.redirects:
            if len(r[0]) == 1:
                file.write("%s '%s'" % (r[0][0], r[1]))
            else:
                file.write("%s%s '%s'" % (r[0][1], r[0][0], r[1]))

class Pipeline:
    def __init__(self, commands, negate=False, pipe_err=False):
        self.commands = commands
        self.negate = negate
        self.pipe_err = pipe_err

    def __repr__(self):
        return 'Pipeline(%r, %r, %r)' % (self.commands, self.negate,
                                         self.pipe_err)

    def __cmp__(self, other):
        if not isinstance(other, Pipeline):
            return -1

        return cmp((self.commands, self.negate, self.pipe_err),
                   (other.commands, other.negate, self.pipe_err))

    def toShell(self, file, pipefail=False):
        if pipefail != self.pipe_err:
            raise ValueError('Inconsistent "pipefail" attribute!')
        if self.negate:
            file.write('! ')
        for cmd in self.commands:
            cmd.toShell(file)
            if cmd is not self.commands[-1]:
                file.write('|\n  ')

class Seq:
    def __init__(self, lhs, op, rhs):
        assert op in (';', '&', '||', '&&')
        self.op = op
        self.lhs = lhs
        self.rhs = rhs

    def __repr__(self):
        return 'Seq(%r, %r, %r)' % (self.lhs, self.op, self.rhs)

    def __cmp__(self, other):
        if not isinstance(other, Seq):
            return -1

        return cmp((self.lhs, self.op, self.rhs),
                   (other.lhs, other.op, other.rhs))

    def toShell(self, file, pipefail=False):
        self.lhs.toShell(file, pipefail)
        file.write(' %s\n' % self.op)
        self.rhs.toShell(file, pipefail)