fix mistake in RELEASE.txt content
[scons.git] / bin / Command.py
bloba7f94f609506226e2205540a7dada82b63354e83
1 #!/usr/bin/env python
3 # XXX Python script template
5 # XXX Describe what the script does here.
7 import getopt
8 import os
9 import shlex
10 import sys
12 class Usage(Exception):
13 def __init__(self, msg):
14 self.msg = msg
16 class CommandRunner:
17 """
18 Representation of a command to be executed.
19 """
21 def __init__(self, dictionary={}):
22 self.subst_dictionary(dictionary)
24 def subst_dictionary(self, dictionary):
25 self._subst_dictionary = dictionary
27 def subst(self, string, dictionary=None):
28 """
29 Substitutes (via the format operator) the values in the specified
30 dictionary into the specified command.
32 The command can be an (action, string) tuple. In all cases, we
33 perform substitution on strings and don't worry if something isn't
34 a string. (It's probably a Python function to be executed.)
35 """
36 if dictionary is None:
37 dictionary = self._subst_dictionary
38 if dictionary:
39 try:
40 string = string % dictionary
41 except TypeError:
42 pass
43 return string
45 def do_display(self, string):
46 if isinstance(string, tuple):
47 func = string[0]
48 args = string[1:]
49 s = '%s(%s)' % (func.__name__, ', '.join(map(repr, args)))
50 else:
51 s = self.subst(string)
52 if not s.endswith('\n'):
53 s += '\n'
54 sys.stdout.write(s)
55 sys.stdout.flush()
57 def do_not_display(self, string):
58 pass
60 def do_execute(self, command):
61 if isinstance(command, str):
62 command = self.subst(command)
63 cmdargs = shlex.split(command)
64 if cmdargs[0] == 'cd':
65 command = (os.chdir,) + tuple(cmdargs[1:])
66 elif cmdargs[0] == 'mkdir':
67 command = (os.mkdir,) + tuple(cmdargs[1:])
68 if isinstance(command, tuple):
69 func = command[0]
70 args = command[1:]
71 return func(*args)
72 else:
73 return os.system(command)
75 def do_not_execute(self, command):
76 pass
78 display = do_display
79 execute = do_execute
81 def run(self, command, display=None):
82 """
83 Runs this command, displaying it first.
85 The actual display() and execute() methods we call may be
86 overridden if we're printing but not executing, or vice versa.
87 """
88 if display is None:
89 display = command
90 self.display(display)
91 return self.execute(command)
93 def main(argv=None):
94 if argv is None:
95 argv = sys.argv
97 short_options = 'hnq'
98 long_options = ['help', 'no-exec', 'quiet']
100 helpstr = """\
101 Usage: script-template.py [-hnq]
103 -h, --help Print this help and exit
104 -n, --no-exec No execute, just print command lines
105 -q, --quiet Quiet, don't print command lines
108 try:
109 try:
110 opts, args = getopt.getopt(argv[1:], short_options, long_options)
111 except getopt.error as msg:
112 raise Usage(msg)
114 for o, a in opts:
115 if o in ('-h', '--help'):
116 print(helpstr)
117 sys.exit(0)
118 elif o in ('-n', '--no-exec'):
119 Command.execute = Command.do_not_execute
120 elif o in ('-q', '--quiet'):
121 Command.display = Command.do_not_display
122 except Usage as err:
123 sys.stderr.write(err.msg)
124 sys.stderr.write('use -h to get help')
125 return 2
127 commands = [
130 for command in [ Command(c) for c in commands ]:
131 status = command.run(command)
132 if status:
133 sys.exit(status)
135 if __name__ == "__main__":
136 sys.exit(main())
138 # Local Variables:
139 # tab-width:4
140 # indent-tabs-mode:nil
141 # End:
142 # vim: set expandtab tabstop=4 shiftwidth=4: