From de22c79665d0b805838fd7e41cc9734fc7cc5b3d Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 30 Jan 2019 11:51:17 -0800 Subject: [PATCH] backups: Minor styling fixes - Run yapf - Fix flake8 errors/warnings. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- actions/backups | 57 +++++++++++++++------------- actions/sshfs | 7 ++-- plinth/modules/backups/repository.py | 11 ++++-- 3 files changed, 42 insertions(+), 33 deletions(-) diff --git a/actions/backups b/actions/backups index b2db83510..172437f3f 100755 --- a/actions/backups +++ b/actions/backups @@ -65,29 +65,29 @@ def parse_arguments(): export_tar = subparsers.add_parser('export-tar', help=export_help) get_archive_apps = subparsers.add_parser( - 'get-archive-apps', - help='Get list of apps included in archive') + 'get-archive-apps', help='Get list of apps included in archive') restore_archive = subparsers.add_parser( 'restore-archive', help='Restore files from an archive') restore_archive.add_argument('--destination', help='Destination', required=True) - for cmd in [info, init, list_repo, create_archive, delete_archive, - export_tar, get_archive_apps, restore_archive, setup]: + for cmd in [ + info, init, list_repo, create_archive, delete_archive, export_tar, + get_archive_apps, restore_archive, setup + ]: cmd.add_argument('--path', help='Repository or Archive path', required=False) cmd.add_argument('--ssh-keyfile', help='Path of private ssh key', default=None) cmd.add_argument('--encryption-passphrase', - help='Encryption passphrase', - default=None) + help='Encryption passphrase', default=None) get_exported_archive_apps = subparsers.add_parser( 'get-exported-archive-apps', help='Get list of apps included in exported archive file') - get_exported_archive_apps.add_argument( - '--path', help='Tarball file path', required=True) + get_exported_archive_apps.add_argument('--path', help='Tarball file path', + required=True) restore_exported_archive = subparsers.add_parser( 'restore-exported-archive', @@ -108,21 +108,23 @@ def subcommand_setup(arguments): if not os.path.exists(path): os.makedirs(path) - init(arguments, encryption='none') + init_repository(arguments, encryption='none') -def init(arguments, encryption): +def init_repository(arguments, encryption): """Initialize a local or remote borg repository""" if encryption != 'none': if not hasattr(arguments, 'encryption_passphrase') or not \ arguments.encryption_passphrase: raise ValueError('No encryption passphrase provided') + cmd = ['borg', 'init', '--encryption', encryption, arguments.path] run(cmd, arguments=arguments) def subcommand_init(arguments): - init(arguments, encryption=arguments.encryption) + """Initialize the borg repository.""" + init_repository(arguments, encryption=arguments.encryption) def subcommand_info(arguments): @@ -162,8 +164,7 @@ def _extract(archive_path, destination, locations=None, env=None): os.chdir(os.path.expanduser(destination)) # TODO: with python 3.7 use subprocess.run with the 'capture_output' # argument - process = subprocess.run(borg_call, env=env, - stdout=subprocess.PIPE, + process = subprocess.run(borg_call, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if process.returncode != 0: error = process.stderr.decode() @@ -190,8 +191,10 @@ def subcommand_get_archive_apps(arguments): """Get list of apps included in archive.""" env = get_env(arguments) manifest_folder = os.path.relpath(MANIFESTS_FOLDER, '/') - borg_call = ['borg', 'list', arguments.path, manifest_folder, - '--format', '{path}{NEWLINE}'] + borg_call = [ + 'borg', 'list', arguments.path, manifest_folder, '--format', + '{path}{NEWLINE}' + ] timeout = None if 'BORG_RSH' in env and 'SSHPASS' not in env: timeout = TIMEOUT @@ -217,24 +220,25 @@ def _get_apps_of_manifest(manifest): Get apps of a manifest. Supports both dict format as well as list format of plinth <=0.42 """ - if type(manifest) is list: + if isinstance(manifest, list): apps = manifest - elif type(manifest) is dict and 'apps' in manifest: + elif isinstance(manifest, dict) and 'apps' in manifest: apps = manifest['apps'] else: raise RuntimeError('Unknown manifest format') + return apps def subcommand_get_exported_archive_apps(arguments): """Get list of apps included in an exported archive file.""" manifest = None - with tarfile.open(arguments.path) as t: - filenames = t.getnames() + with tarfile.open(arguments.path) as tar_handle: + filenames = tar_handle.getnames() for name in filenames: if 'var/lib/plinth/backups-manifests/' in name \ and name.endswith('.json'): - manifest_data = t.extractfile(name).read() + manifest_data = tar_handle.extractfile(name).read() manifest = json.loads(manifest_data) break @@ -275,8 +279,8 @@ def read_password(): """Read the password from stdin.""" if sys.stdin.isatty(): return '' - else: - return ''.join(sys.stdin) + + return ''.join(sys.stdin) def get_env(arguments, use_credentials=False): @@ -284,12 +288,11 @@ def get_env(arguments, use_credentials=False): env = dict(os.environ, BORG_RELOCATED_REPO_ACCESS_IS_OK='yes') # always provide BORG_PASSPHRASE (also if empty) so borg does not get stuck # while asking for a passphrase. - passphrase = arguments.encryption_passphrase if \ - arguments.encryption_passphrase else '' + passphrase = arguments.encryption_passphrase or '' env['BORG_PASSPHRASE'] = passphrase if use_credentials: if arguments.ssh_keyfile: - env['BORG_RSH'] = "ssh -i %s" % arguments.ssh_keyfile + env['BORG_RSH'] = 'ssh -i %s' % arguments.ssh_keyfile else: password = read_password() if password: @@ -297,6 +300,7 @@ def get_env(arguments, use_credentials=False): env['BORG_RSH'] = 'sshpass -e ssh -o StrictHostKeyChecking=no' else: raise ValueError('could not find credentials') + return env @@ -305,9 +309,10 @@ def run(cmd, arguments, check=True): # Set a timeout to not get stuck if the remote server asks for a password. timeout = None use_credentials = False - if "@" in arguments.path: + if '@' in arguments.path: timeout = TIMEOUT use_credentials = True + env = get_env(arguments, use_credentials=use_credentials) subprocess.run(cmd, check=check, env=env, timeout=timeout) diff --git a/actions/sshfs b/actions/sshfs index f308cbcc6..5cfa9be5b 100755 --- a/actions/sshfs +++ b/actions/sshfs @@ -30,7 +30,7 @@ TIMEOUT = 5 class AlreadyMountedError(Exception): - pass + """Exception raised when mount point is already mounted.""" def parse_arguments(): @@ -112,6 +112,7 @@ def _is_mounted(mountpoint): def subcommand_is_mounted(arguments): + """Print whether a path is already mounted.""" print(json.dumps(_is_mounted(arguments.mountpoint))) @@ -119,8 +120,8 @@ def read_password(): """Read the password from stdin.""" if sys.stdin.isatty(): return '' - else: - return ''.join(sys.stdin) + + return ''.join(sys.stdin) def main(): diff --git a/plinth/modules/backups/repository.py b/plinth/modules/backups/repository.py index 92206339a..65d7b3ee8 100644 --- a/plinth/modules/backups/repository.py +++ b/plinth/modules/backups/repository.py @@ -183,15 +183,16 @@ class BorgRepository(): def run(self, arguments): return self._run('backups', arguments) - def reraise_known_error(self, err): + @staticmethod + def reraise_known_error(err): """Look whether the caught error is known and reraise it accordingly""" caught_error = str(err) for known_error in KNOWN_ERRORS: for error in known_error["errors"]: if error in caught_error: raise known_error["raise_as"](known_error["message"]) - else: - raise err + + raise err class SshBorgRepository(BorgRepository): @@ -319,7 +320,8 @@ class SshBorgRepository(BorgRepository): except Exception as err: logger.error(err) - def _append_sshfs_arguments(self, arguments, credentials): + @staticmethod + def _append_sshfs_arguments(arguments, credentials): """Add credentials to a run command and kwargs""" kwargs = {} @@ -348,6 +350,7 @@ def get_ssh_repositories(): for storage in network_storage.get_storages().values(): repository = SshBorgRepository(automount=False, **storage) repositories[storage['uuid']] = repository.get_view_content() + return repositories