Runs the command (which should be an sh.Command instance), while logging the output.
(command, *args, **kwargs)
| 129 | |
| 130 | |
| 131 | def shprint(command, *args, **kwargs): |
| 132 | '''Runs the command (which should be an sh.Command instance), while |
| 133 | logging the output.''' |
| 134 | kwargs["_iter"] = True |
| 135 | kwargs["_out_bufsize"] = 1 |
| 136 | kwargs["_err_to_out"] = True |
| 137 | kwargs["_bg"] = True |
| 138 | |
| 139 | # silent mode |
| 140 | silent = kwargs.get("silent", False) |
| 141 | kwargs.pop("silent", False) |
| 142 | if silent: |
| 143 | kwargs["_out"] = None |
| 144 | |
| 145 | is_critical = kwargs.pop('_critical', False) |
| 146 | tail_n = kwargs.pop('_tail', None) |
| 147 | full_debug = False |
| 148 | if "P4A_FULL_DEBUG" in os.environ: |
| 149 | tail_n = 0 |
| 150 | full_debug = True |
| 151 | filter_in = kwargs.pop('_filter', None) |
| 152 | filter_out = kwargs.pop('_filterout', None) |
| 153 | if len(logger.handlers) > 1: |
| 154 | logger.removeHandler(logger.handlers[1]) |
| 155 | columns = get_console_width() |
| 156 | command_path = str(command).split('/') |
| 157 | command_string = command_path[-1] |
| 158 | string = ' '.join(['{}->{} running'.format(Out_Fore.LIGHTBLACK_EX, |
| 159 | Out_Style.RESET_ALL), |
| 160 | command_string] + list(args)) |
| 161 | |
| 162 | # If logging is not in DEBUG mode, trim the command if necessary |
| 163 | if logger.level > logging.DEBUG: |
| 164 | logger.info('{}{}'.format(shorten_string(string, columns - 12), |
| 165 | Err_Style.RESET_ALL)) |
| 166 | else: |
| 167 | logger.debug('{}{}'.format(string, Err_Style.RESET_ALL)) |
| 168 | |
| 169 | need_closing_newline = False |
| 170 | try: |
| 171 | msg_hdr = ' working: ' |
| 172 | msg_width = columns - len(msg_hdr) - 1 |
| 173 | output = command(*args, **kwargs) |
| 174 | for line in output: |
| 175 | if isinstance(line, bytes): |
| 176 | line = line.decode('utf-8', errors='replace') |
| 177 | if logger.level > logging.DEBUG: |
| 178 | if full_debug: |
| 179 | stdout.write(line) |
| 180 | stdout.flush() |
| 181 | continue |
| 182 | msg = line.replace( |
| 183 | '\n', ' ').replace( |
| 184 | '\t', ' ').replace( |
| 185 | '\b', ' ').rstrip() |
| 186 | if msg: |
| 187 | if "CI" not in os.environ: |
| 188 | stdout.write(u'{}\r{}{:<{width}}'.format( |
no test coverage detected