Format a command given as a list of strings so that it can be viewed nicely and executed by bash directly and print it to stdout.
(cmd, cwd=None, extra_env=None, extra_paths=None)
| 68 | |
| 69 | @staticmethod |
| 70 | def cmd_to_string(cmd, cwd=None, extra_env=None, extra_paths=None): |
| 71 | ''' |
| 72 | Format a command given as a list of strings so that it can |
| 73 | be viewed nicely and executed by bash directly and print it to stdout. |
| 74 | ''' |
| 75 | last_newline = ' \\\n' |
| 76 | newline_separator = last_newline + ' ' |
| 77 | out = [] |
| 78 | if extra_env is None: |
| 79 | extra_env = {} |
| 80 | if cwd is not None: |
| 81 | out.append('cd {} &&'.format(shlex.quote(cwd))) |
| 82 | if extra_paths is not None: |
| 83 | out.append('PATH="{}:${{PATH}}"'.format(':'.join(extra_paths))) |
| 84 | for key in extra_env: |
| 85 | out.append('{}={}'.format(shlex.quote(key), shlex.quote(extra_env[key]))) |
| 86 | cmd_quote = [] |
| 87 | newline_count = 0 |
| 88 | for arg in cmd: |
| 89 | if arg == LF: |
| 90 | cmd_quote.append(arg) |
| 91 | newline_count += 1 |
| 92 | else: |
| 93 | cmd_quote.append(shlex.quote(arg)) |
| 94 | if newline_count > 0: |
| 95 | cmd_quote = [' '.join(list(y)) for x, y in itertools.groupby(cmd_quote, lambda z: z == LF) if not x] |
| 96 | out.extend(cmd_quote) |
| 97 | if newline_count == 1 and cmd[-1] == LF: |
| 98 | ending = '' |
| 99 | else: |
| 100 | ending = last_newline + ';' |
| 101 | return newline_separator.join(out) + ending |
| 102 | |
| 103 | def copy_dir_if_update_non_recursive(self, srcdir, destdir, filter_ext=None): |
| 104 | # TODO print rsync equivalent. |