Fix mypy errors in simple_backup() and Backup
Accept None for the missing inputs, output, exclude and keep values and replace them with their defaults in __init__, and annotate the SSH client, which mypy typed as None and so skipped checking the remote code.
This commit is contained in:
@@ -136,14 +136,16 @@ class Backup:
|
|||||||
Perform the backup
|
Perform the backup
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, inputs: List[str], output: str, exclude: List[str], keep: int, options: str,
|
def __init__(self, inputs: Optional[List[str]], output: Optional[str], exclude: Optional[List[str]],
|
||||||
|
keep: Optional[int], options: str,
|
||||||
ssh_host: Optional[str] = None, ssh_user: Optional[str] = None, ssh_keyfile: Optional[str] = None,
|
ssh_host: Optional[str] = None, ssh_user: Optional[str] = None, ssh_keyfile: Optional[str] = None,
|
||||||
remote_sudo: bool = False, remove_before: bool = False, verbose: bool = False) -> None:
|
remote_sudo: bool = False, remove_before: bool = False, verbose: bool = False) -> None:
|
||||||
self.inputs = inputs
|
# Missing values are reported by check_params()
|
||||||
self.output = output
|
self.inputs = inputs if inputs is not None else []
|
||||||
self.exclude = exclude
|
self.output = output if output is not None else ''
|
||||||
|
self.exclude = exclude if exclude is not None else []
|
||||||
self.options = options
|
self.options = options
|
||||||
self.keep = keep
|
self.keep = keep if keep is not None else -1
|
||||||
self.ssh_host = ssh_host
|
self.ssh_host = ssh_host
|
||||||
self.ssh_user = ssh_user
|
self.ssh_user = ssh_user
|
||||||
self.ssh_keyfile = ssh_keyfile
|
self.ssh_keyfile = ssh_keyfile
|
||||||
@@ -156,20 +158,20 @@ class Backup:
|
|||||||
self._inputs_path = ''
|
self._inputs_path = ''
|
||||||
self._exclude_path = ''
|
self._exclude_path = ''
|
||||||
self._remote = False
|
self._remote = False
|
||||||
self._ssh = None
|
self._ssh: Optional[paramiko.client.SSHClient] = None
|
||||||
self._password_auth = False
|
self._password_auth = False
|
||||||
self._removed_count = 0
|
self._removed_count = 0
|
||||||
|
|
||||||
def check_params(self, homedir: str = '') -> int:
|
def check_params(self, homedir: str = '') -> int:
|
||||||
"""Check if parameters for the backup are valid"""
|
"""Check if parameters for the backup are valid"""
|
||||||
|
|
||||||
if self.inputs is None or len(self.inputs) == 0:
|
if len(self.inputs) == 0:
|
||||||
logger.info(
|
logger.info(
|
||||||
'No existing files or directories specified for backup. Nothing to do')
|
'No existing files or directories specified for backup. Nothing to do')
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if self.output is None:
|
if self.output == '':
|
||||||
logger.critical(
|
logger.critical(
|
||||||
'No output path specified. Use -o argument or specify output path in configuration file')
|
'No output path specified. Use -o argument or specify output path in configuration file')
|
||||||
|
|
||||||
@@ -207,9 +209,6 @@ class Backup:
|
|||||||
|
|
||||||
self.output = os.path.abspath(self.output)
|
self.output = os.path.abspath(self.output)
|
||||||
|
|
||||||
if self.keep is None:
|
|
||||||
self.keep = -1
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def _resolve_remote_path(self, path: str) -> Optional[str]:
|
def _resolve_remote_path(self, path: str) -> Optional[str]:
|
||||||
@@ -605,10 +604,9 @@ class Backup:
|
|||||||
fd, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True)
|
fd, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True)
|
||||||
|
|
||||||
with os.fdopen(fd, 'w', encoding='utf-8') as fp:
|
with os.fdopen(fd, 'w', encoding='utf-8') as fp:
|
||||||
if self.exclude is not None:
|
for e in self.exclude:
|
||||||
for e in self.exclude:
|
fp.write(e)
|
||||||
fp.write(e)
|
fp.write('\n')
|
||||||
fp.write('\n')
|
|
||||||
|
|
||||||
if self.keep != -1 and self._remove_before:
|
if self.keep != -1 and self._remove_before:
|
||||||
self.remove_old_backups()
|
self.remove_old_backups()
|
||||||
@@ -1015,9 +1013,7 @@ def simple_backup() -> int:
|
|||||||
if args.numeric_ids or config_args['numeric_ids']:
|
if args.numeric_ids or config_args['numeric_ids']:
|
||||||
rsync_options.append('--numeric-ids')
|
rsync_options.append('--numeric-ids')
|
||||||
|
|
||||||
rsync_options = ' '.join(rsync_options)
|
backup = Backup(inputs, output, exclude, keep, ' '.join(rsync_options), ssh_host, ssh_user, ssh_keyfile,
|
||||||
|
|
||||||
backup = Backup(inputs, output, exclude, keep, rsync_options, ssh_host, ssh_user, ssh_keyfile,
|
|
||||||
remote_sudo, remove_before=args.remove_before_backup, verbose=args.verbose)
|
remote_sudo, remove_before=args.remove_before_backup, verbose=args.verbose)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
+2
-2
@@ -79,14 +79,14 @@ class TestCheckParams:
|
|||||||
assert sb.Backup([], str(out), [], -1, OPTIONS).check_params() == 1
|
assert sb.Backup([], str(out), [], -1, OPTIONS).check_params() == 1
|
||||||
|
|
||||||
def test_no_output(self) -> None:
|
def test_no_output(self) -> None:
|
||||||
assert sb.Backup(['/x'], None, [], -1, OPTIONS).check_params() == 2 # type: ignore[arg-type]
|
assert sb.Backup(['/x'], None, [], -1, OPTIONS).check_params() == 2
|
||||||
|
|
||||||
def test_missing_output(self, tmp_path: Path) -> None:
|
def test_missing_output(self, tmp_path: Path) -> None:
|
||||||
assert sb.Backup(['/x'], str(tmp_path / 'missing'), [], -1, OPTIONS).check_params() == 2
|
assert sb.Backup(['/x'], str(tmp_path / 'missing'), [], -1, OPTIONS).check_params() == 2
|
||||||
|
|
||||||
def test_relative_output(self, out: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
def test_relative_output(self, out: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||||
monkeypatch.chdir(out.parent)
|
monkeypatch.chdir(out.parent)
|
||||||
backup = sb.Backup(['/x'], out.name, [], None, OPTIONS) # type: ignore[arg-type]
|
backup = sb.Backup(['/x'], out.name, [], None, OPTIONS)
|
||||||
|
|
||||||
assert backup.check_params() == 0
|
assert backup.check_params() == 0
|
||||||
assert backup.output == str(out)
|
assert backup.output == str(out)
|
||||||
|
|||||||
Reference in New Issue
Block a user