Fix a refactoro.
[oota-llvm.git] / utils / lit / ShCommands.py
1 import ShUtil
2
3 class Command:
4     def __init__(self, args, redirects):
5         self.args = list(args)
6         self.redirects = list(redirects)
7
8     def __repr__(self):
9         return 'Command(%r, %r)' % (self.args, self.redirects)
10
11     def __cmp__(self, other):
12         if not isinstance(other, Command):
13             return -1
14
15         return cmp((self.args, self.redirects),
16                    (other.args, other.redirects))
17
18     def toShell(self, file):
19         for arg in self.args:
20             if "'" not in arg:
21                 quoted = "'%s'" % arg
22             elif '"' not in arg and '$' not in arg:
23                 quoted = '"%s"' % arg
24             else:
25                 raise NotImplementedError,'Unable to quote %r' % arg
26             print >>file, quoted,
27
28             # For debugging / validation.
29             dequoted = list(ShUtil.ShLexer(quoted).lex())
30             if dequoted != [arg]:
31                 raise NotImplementedError,'Unable to quote %r' % arg
32
33         for r in self.redirects:
34             if len(r[0]) == 1:
35                 print >>file, "%s '%s'" % (r[0][0], r[1]),
36             else:
37                 print >>file, "%s%s '%s'" % (r[0][1], r[0][0], r[1]),
38
39 class Pipeline:
40     def __init__(self, commands, negate=False, pipe_err=False):
41         self.commands = commands
42         self.negate = negate
43         self.pipe_err = pipe_err
44
45     def __repr__(self):
46         return 'Pipeline(%r, %r, %r)' % (self.commands, self.negate,
47                                          self.pipe_err)
48
49     def __cmp__(self, other):
50         if not isinstance(other, Pipeline):
51             return -1
52
53         return cmp((self.commands, self.negate, self.pipe_err),
54                    (other.commands, other.negate, self.pipe_err))
55
56     def toShell(self, file, pipefail=False):
57         if pipefail != self.pipe_err:
58             raise ValueError,'Inconsistent "pipefail" attribute!'
59         if self.negate:
60             print >>file, '!',
61         for cmd in self.commands:
62             cmd.toShell(file)
63             if cmd is not self.commands[-1]:
64                 print >>file, '|\n ',
65
66 class Seq:
67     def __init__(self, lhs, op, rhs):
68         assert op in (';', '&', '||', '&&')
69         self.op = op
70         self.lhs = lhs
71         self.rhs = rhs
72
73     def __repr__(self):
74         return 'Seq(%r, %r, %r)' % (self.lhs, self.op, self.rhs)
75
76     def __cmp__(self, other):
77         if not isinstance(other, Seq):
78             return -1
79
80         return cmp((self.lhs, self.op, self.rhs),
81                    (other.lhs, other.op, other.rhs))
82
83     def toShell(self, file, pipefail=False):
84         self.lhs.toShell(file, pipefail)
85         print >>file, ' %s\n' % self.op
86         self.rhs.toShell(file, pipefail)