1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
4 | # Copyright (C) 2007-2010 Edgewall Software |
---|
5 | # All rights reserved. |
---|
6 | # |
---|
7 | # This software is licensed as described in the file COPYING, which |
---|
8 | # you should have received as part of this distribution. The terms |
---|
9 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
10 | |
---|
11 | """Generic recipe commands for executing external processes.""" |
---|
12 | |
---|
13 | import codecs |
---|
14 | import logging |
---|
15 | import os |
---|
16 | import shlex |
---|
17 | |
---|
18 | from bitten.build import CommandLine |
---|
19 | from bitten.util import xmlio |
---|
20 | |
---|
21 | log = logging.getLogger('bitten.build.shtools') |
---|
22 | |
---|
23 | __docformat__ = 'restructuredtext en' |
---|
24 | |
---|
25 | def exec_(ctxt, executable=None, file_=None, output=None, args=None, |
---|
26 | dir_=None, timeout=None): |
---|
27 | """Execute a program or shell script. |
---|
28 | |
---|
29 | :param ctxt: the build context |
---|
30 | :type ctxt: `Context` |
---|
31 | :param executable: name of the executable to run |
---|
32 | :param file\_: name of the script file, relative to the project directory, |
---|
33 | that should be run |
---|
34 | :param output: name of the file to which the output of the script should be |
---|
35 | written |
---|
36 | :param args: command-line arguments to pass to the script |
---|
37 | :param dir\_: directory to change to before executing the command |
---|
38 | :param timeout: the number of seconds before the external process should |
---|
39 | be aborted (has same constraints as CommandLine) |
---|
40 | """ |
---|
41 | assert executable or file_, \ |
---|
42 | 'Either "executable" or "file" attribute required' |
---|
43 | |
---|
44 | returncode = execute(ctxt, executable=executable, file_=file_, |
---|
45 | output=output, args=args, dir_=dir_, |
---|
46 | timeout=timeout) |
---|
47 | if returncode != 0: |
---|
48 | ctxt.error('Executing %s failed (error code %s)' % (executable or file_, |
---|
49 | returncode)) |
---|
50 | |
---|
51 | def pipe(ctxt, executable=None, file_=None, input_=None, output=None, |
---|
52 | args=None, dir_=None): |
---|
53 | """Pipe the contents of a file through a program or shell script. |
---|
54 | |
---|
55 | :param ctxt: the build context |
---|
56 | :type ctxt: `Context` |
---|
57 | :param executable: name of the executable to run |
---|
58 | :param file\_: name of the script file, relative to the project directory, |
---|
59 | that should be run |
---|
60 | :param input\_: name of the file containing the data that should be passed |
---|
61 | to the shell script on its standard input stream |
---|
62 | :param output: name of the file to which the output of the script should be |
---|
63 | written |
---|
64 | :param args: command-line arguments to pass to the script |
---|
65 | :param dir\_: directory to change to before executing the command |
---|
66 | """ |
---|
67 | assert executable or file_, \ |
---|
68 | 'Either "executable" or "file" attribute required' |
---|
69 | assert input_, 'Missing required attribute "input"' |
---|
70 | |
---|
71 | returncode = execute(ctxt, executable=executable, file_=file_, |
---|
72 | input_=input_, output=output, args=args, dir_=dir_) |
---|
73 | if returncode != 0: |
---|
74 | ctxt.error('Piping through %s failed (error code %s)' |
---|
75 | % (executable or file_, returncode)) |
---|
76 | |
---|
77 | def execute(ctxt, executable=None, file_=None, input_=None, output=None, |
---|
78 | args=None, dir_=None, filter_=None, timeout=None): |
---|
79 | """Generic external program execution. |
---|
80 | |
---|
81 | This function is not itself bound to a recipe command, but rather used from |
---|
82 | other commands. |
---|
83 | |
---|
84 | :param ctxt: the build context |
---|
85 | :type ctxt: `Context` |
---|
86 | :param executable: name of the executable to run |
---|
87 | :param file\_: name of the script file, relative to the project directory, |
---|
88 | that should be run |
---|
89 | :param input\_: name of the file containing the data that should be passed |
---|
90 | to the shell script on its standard input stream |
---|
91 | :param output: name of the file to which the output of the script should be |
---|
92 | written |
---|
93 | :param args: command-line arguments to pass to the script |
---|
94 | :param dir\_: directory to change to before executing the command |
---|
95 | :param filter\_: function to filter out messages from the executable stdout |
---|
96 | :param timeout: the number of seconds before the external process should |
---|
97 | be aborted (has same constraints as CommandLine) |
---|
98 | """ |
---|
99 | if args: |
---|
100 | if isinstance(args, basestring): |
---|
101 | args = shlex.split(args) |
---|
102 | else: |
---|
103 | args = [] |
---|
104 | |
---|
105 | if dir_: |
---|
106 | def resolve(*args): |
---|
107 | return ctxt.resolve(dir_, *args) |
---|
108 | else: |
---|
109 | resolve = ctxt.resolve |
---|
110 | |
---|
111 | if file_ and os.path.isfile(resolve(file_)): |
---|
112 | file_ = resolve(file_) |
---|
113 | |
---|
114 | shell = False |
---|
115 | |
---|
116 | if file_ and os.name == 'nt': |
---|
117 | # Need to execute script files through a shell on Windows |
---|
118 | shell = True |
---|
119 | |
---|
120 | if executable is None: |
---|
121 | executable = file_ |
---|
122 | elif file_: |
---|
123 | args[:0] = [file_] |
---|
124 | |
---|
125 | # Support important Windows CMD.EXE built-ins (and it does its own quoting) |
---|
126 | if os.name == 'nt' and executable.upper() in ['COPY', 'DIR', 'ECHO', |
---|
127 | 'ERASE', 'DEL', 'MKDIR', 'MD', 'MOVE', 'RMDIR', 'RD', 'TYPE']: |
---|
128 | shell = True |
---|
129 | |
---|
130 | if input_: |
---|
131 | input_file = codecs.open(resolve(input_), 'r', 'utf-8') |
---|
132 | else: |
---|
133 | input_file = None |
---|
134 | |
---|
135 | if output: |
---|
136 | output_file = codecs.open(resolve(output), 'w', 'utf-8') |
---|
137 | else: |
---|
138 | output_file = None |
---|
139 | |
---|
140 | if dir_ and os.path.isdir(ctxt.resolve(dir_)): |
---|
141 | dir_ = ctxt.resolve(dir_) |
---|
142 | else: |
---|
143 | dir_ = ctxt.basedir |
---|
144 | |
---|
145 | if not filter_: |
---|
146 | filter_=lambda s: s |
---|
147 | |
---|
148 | if timeout: |
---|
149 | timeout = int(timeout) |
---|
150 | |
---|
151 | try: |
---|
152 | cmdline = CommandLine(executable, args, input=input_file, |
---|
153 | cwd=dir_, shell=shell) |
---|
154 | log_elem = xmlio.Fragment() |
---|
155 | for out, err in cmdline.execute(timeout=timeout): |
---|
156 | if out is not None: |
---|
157 | log.info(out) |
---|
158 | info = filter_(out) |
---|
159 | if info: |
---|
160 | log_elem.append(xmlio.Element('message', level='info')[ |
---|
161 | info.replace(ctxt.basedir + os.sep, '') |
---|
162 | .replace(ctxt.basedir, '') |
---|
163 | ]) |
---|
164 | if output: |
---|
165 | output_file.write(out + os.linesep) |
---|
166 | if err is not None: |
---|
167 | log.error(err) |
---|
168 | log_elem.append(xmlio.Element('message', level='error')[ |
---|
169 | err.replace(ctxt.basedir + os.sep, '') |
---|
170 | .replace(ctxt.basedir, '') |
---|
171 | ]) |
---|
172 | if output: |
---|
173 | output_file.write(err + os.linesep) |
---|
174 | ctxt.log(log_elem) |
---|
175 | finally: |
---|
176 | if input_: |
---|
177 | input_file.close() |
---|
178 | if output: |
---|
179 | output_file.close() |
---|
180 | |
---|
181 | return cmdline.returncode |
---|