|
16 | 16 | # ----------------------------------------------------------------------------- |
17 | 17 | """A handler of locations on remote hosts.""" |
18 | 18 |
|
| 19 | +from io import TextIOWrapper |
| 20 | +from textwrap import indent |
19 | 21 | from time import sleep, time |
20 | 22 |
|
21 | 23 | from metomi.rose.loc_handlers.rsync_remote_check import ( |
|
24 | 26 | from metomi.rose.popen import RosePopenError |
25 | 27 |
|
26 | 28 |
|
| 29 | +class PreRsyncCheckError(Exception): |
| 30 | + """Error to raise if we: |
| 31 | + * Can't work out which loc handler to use. |
| 32 | + * Fall back to assuming that the loc is for use with rsync. |
| 33 | + * Attempting to use it as an rsync loc fails. |
| 34 | +
|
| 35 | + """ |
| 36 | + |
| 37 | + BASE_MESSAGE = ( |
| 38 | + 'Rose tried all other file install handlers and' |
| 39 | + ' decided this must be an Rsync handler.\n\t' |
| 40 | + ) |
| 41 | + |
| 42 | + def __init__(self, dict_, cmd=None, loc=None): |
| 43 | + for key, value in dict_.items(): |
| 44 | + if isinstance(value, TextIOWrapper): |
| 45 | + setattr(self, key, value.read()) |
| 46 | + else: |
| 47 | + setattr(self, key, value) |
| 48 | + |
| 49 | + # Convert command into something the debugger can try: |
| 50 | + try: |
| 51 | + self.cmd = ' '.join(cmd) |
| 52 | + except TypeError: |
| 53 | + self.cmd = cmd |
| 54 | + |
| 55 | + # Handle ``test -e nonexistant`` where no useful error is |
| 56 | + # provided: |
| 57 | + message = '' |
| 58 | + for used_by in loc.used_by_names: |
| 59 | + message += ( |
| 60 | + f'file:{used_by}={loc.action_key}={loc.name}' |
| 61 | + ': don\'t know how to process this file location.\n' |
| 62 | + ) |
| 63 | + if ( |
| 64 | + self.returncode == 1 |
| 65 | + and self.stderr == '' |
| 66 | + and self.stdout == '' |
| 67 | + ): |
| 68 | + self.stderr = f'File "{cmd[-1]}" does not exist.' |
| 69 | + |
| 70 | + if self.returncode == 255: |
| 71 | + host = dict_['args'][dict_['args'].index('-n') + 1] |
| 72 | + self.mod_msg = ( |
| 73 | + message |
| 74 | + + self.BASE_MESSAGE |
| 75 | + + 'If it is then host' |
| 76 | + f' "{host}"' |
| 77 | + ' is uncontactable (ssh 255 error).' |
| 78 | + ) |
| 79 | + else: |
| 80 | + self.mod_msg = ( |
| 81 | + message |
| 82 | + + self.BASE_MESSAGE |
| 83 | + + f'`{self.cmd}` failed with:' |
| 84 | + + indent( |
| 85 | + f'\nreturncode: {self.returncode}' |
| 86 | + f'\nstdout: {self.stdout}' |
| 87 | + f'\nstderr: {self.stderr}', |
| 88 | + prefix=' ', |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + def __str__(self): |
| 93 | + return self.mod_msg |
| 94 | + |
| 95 | + |
27 | 96 | class RsyncLocHandler: |
28 | 97 | """Handler of locations on remote hosts.""" |
29 | 98 |
|
@@ -53,7 +122,10 @@ def can_pull(self, loc): |
53 | 122 | except RosePopenError: |
54 | 123 | return False |
55 | 124 | else: |
56 | | - return proc.wait() == 0 |
| 125 | + if proc.wait() == 0: |
| 126 | + return True |
| 127 | + else: |
| 128 | + raise PreRsyncCheckError(proc.__dict__, cmd=cmd, loc=loc) |
57 | 129 |
|
58 | 130 | def parse(self, loc, _): |
59 | 131 | """Set loc.scheme, loc.loc_type, loc.paths.""" |
|
0 commit comments