| 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 | |
---|
1 | 11 | """Generic recipe commands for executing external processes.""" |
---|
| 12 | |
---|
1 | 13 | import codecs |
---|
1 | 14 | import logging |
---|
1 | 15 | import os |
---|
1 | 16 | import shlex |
---|
| 17 | |
---|
1 | 18 | from bitten.build import CommandLine |
---|
1 | 19 | from bitten.util import xmlio |
---|
| 20 | |
---|
1 | 21 | log = logging.getLogger('bitten.build.shtools') |
---|
| 22 | |
---|
1 | 23 | __docformat__ = 'restructuredtext en' |
---|
| 24 | |
---|
1 | 25 | def exec_(ctxt, executable=None, file_=None, output=None, args=None, |
---|
1 | 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 | """ |
---|
2 | 41 | assert executable or file_, \ |
---|
0 | 42 | 'Either "executable" or "file" attribute required' |
---|
| 43 | |
---|
2 | 44 | returncode = execute(ctxt, executable=executable, file_=file_, |
---|
2 | 45 | output=output, args=args, dir_=dir_, |
---|
2 | 46 | timeout=timeout) |
---|
2 | 47 | if returncode != 0: |
---|
0 | 48 | ctxt.error('Executing %s failed (error code %s)' % (executable or file_, |
---|
0 | 49 | returncode)) |
---|
| 50 | |
---|
1 | 51 | def pipe(ctxt, executable=None, file_=None, input_=None, output=None, |
---|
1 | 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 | """ |
---|
0 | 67 | assert executable or file_, \ |
---|
0 | 68 | 'Either "executable" or "file" attribute required' |
---|
0 | 69 | assert input_, 'Missing required attribute "input"' |
---|
| 70 | |
---|
0 | 71 | returncode = execute(ctxt, executable=executable, file_=file_, |
---|
0 | 72 | input_=input_, output=output, args=args, dir_=dir_) |
---|
0 | 73 | if returncode != 0: |
---|
0 | 74 | ctxt.error('Piping through %s failed (error code %s)' |
---|
0 | 75 | % (executable or file_, returncode)) |
---|
| 76 | |
---|
1 | 77 | def execute(ctxt, executable=None, file_=None, input_=None, output=None, |
---|
1 | 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 | """ |
---|
2 | 99 | if args: |
---|
2 | 100 | if isinstance(args, basestring): |
---|
2 | 101 | args = shlex.split(args) |
---|
2 | 102 | else: |
---|
0 | 103 | args = [] |
---|
| 104 | |
---|
2 | 105 | if dir_: |
---|
0 | 106 | def resolve(*args): |
---|
0 | 107 | return ctxt.resolve(dir_, *args) |
---|
0 | 108 | else: |
---|
2 | 109 | resolve = ctxt.resolve |
---|
| 110 | |
---|
2 | 111 | if file_ and os.path.isfile(resolve(file_)): |
---|
0 | 112 | file_ = resolve(file_) |
---|
| 113 | |
---|
2 | 114 | shell = False |
---|
| 115 | |
---|
2 | 116 | if file_ and os.name == 'nt': |
---|
| 117 | # Need to execute script files through a shell on Windows |
---|
0 | 118 | shell = True |
---|
| 119 | |
---|
2 | 120 | if executable is None: |
---|
0 | 121 | executable = file_ |
---|
2 | 122 | elif file_: |
---|
0 | 123 | args[:0] = [file_] |
---|
| 124 | |
---|
| 125 | # Support important Windows CMD.EXE built-ins (and it does its own quoting) |
---|
2 | 126 | if os.name == 'nt' and executable.upper() in ['COPY', 'DIR', 'ECHO', |
---|
0 | 127 | 'ERASE', 'DEL', 'MKDIR', 'MD', 'MOVE', 'RMDIR', 'RD', 'TYPE']: |
---|
0 | 128 | shell = True |
---|
| 129 | |
---|
2 | 130 | if input_: |
---|
0 | 131 | input_file = codecs.open(resolve(input_), 'r', 'utf-8') |
---|
0 | 132 | else: |
---|
2 | 133 | input_file = None |
---|
| 134 | |
---|
2 | 135 | if output: |
---|
0 | 136 | output_file = codecs.open(resolve(output), 'w', 'utf-8') |
---|
0 | 137 | else: |
---|
2 | 138 | output_file = None |
---|
| 139 | |
---|
2 | 140 | if dir_ and os.path.isdir(ctxt.resolve(dir_)): |
---|
0 | 141 | dir_ = ctxt.resolve(dir_) |
---|
0 | 142 | else: |
---|
2 | 143 | dir_ = ctxt.basedir |
---|
| 144 | |
---|
2 | 145 | if not filter_: |
---|
4 | 146 | filter_=lambda s: s |
---|
| 147 | |
---|
2 | 148 | if timeout: |
---|
0 | 149 | timeout = int(timeout) |
---|
| 150 | |
---|
2 | 151 | try: |
---|
2 | 152 | cmdline = CommandLine(executable, args, input=input_file, |
---|
2 | 153 | cwd=dir_, shell=shell) |
---|
2 | 154 | log_elem = xmlio.Fragment() |
---|
4 | 155 | for out, err in cmdline.execute(timeout=timeout): |
---|
2 | 156 | if out is not None: |
---|
2 | 157 | log.info(out) |
---|
2 | 158 | info = filter_(out) |
---|
2 | 159 | if info: |
---|
2 | 160 | log_elem.append(xmlio.Element('message', level='info')[ |
---|
2 | 161 | info.replace(ctxt.basedir + os.sep, '') |
---|
2 | 162 | .replace(ctxt.basedir, '') |
---|
2 | 163 | ]) |
---|
2 | 164 | if output: |
---|
0 | 165 | output_file.write(out + os.linesep) |
---|
2 | 166 | if err is not None: |
---|
0 | 167 | log.error(err) |
---|
0 | 168 | log_elem.append(xmlio.Element('message', level='error')[ |
---|
0 | 169 | err.replace(ctxt.basedir + os.sep, '') |
---|
0 | 170 | .replace(ctxt.basedir, '') |
---|
0 | 171 | ]) |
---|
0 | 172 | if output: |
---|
0 | 173 | output_file.write(err + os.linesep) |
---|
2 | 174 | ctxt.log(log_elem) |
---|
2 | 175 | finally: |
---|
2 | 176 | if input_: |
---|
0 | 177 | input_file.close() |
---|
2 | 178 | if output: |
---|
0 | 179 | output_file.close() |
---|
| 180 | |
---|
2 | 181 | return cmdline.returncode |
---|