From b442a88d3d1a648b1dfe4ca84bcc5f18436a32e9 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 29 Jan 2021 19:35:02 +0800 Subject: [PATCH] pybind/mgr/snap-schedule: silence flake8 warnings Signed-off-by: Kefu Chai --- src/pybind/mgr/snap_schedule/fs/schedule.py | 83 ++++++++++++------- .../mgr/snap_schedule/fs/schedule_client.py | 44 ++++++---- 2 files changed, 81 insertions(+), 46 deletions(-) diff --git a/src/pybind/mgr/snap_schedule/fs/schedule.py b/src/pybind/mgr/snap_schedule/fs/schedule.py index ec9e94ab408..bc07ca74ec7 100644 --- a/src/pybind/mgr/snap_schedule/fs/schedule.py +++ b/src/pybind/mgr/snap_schedule/fs/schedule.py @@ -6,7 +6,6 @@ LGPL2.1. See file COPYING. from datetime import datetime, timezone import json import logging -from os import environ import re import sqlite3 from typing import cast, Any, Dict, List, Tuple, Optional, Union @@ -68,6 +67,8 @@ def parse_retention(retention: str) -> Dict[str, int]: RETENTION_MULTIPLIERS = ['n', 'M', 'h', 'd', 'w', 'm', 'y'] +TableRowT = Dict[str, Union[int, str]] + def dump_retention(retention: Dict[str, str]) -> str: ret = '' @@ -132,7 +133,7 @@ class Schedule(object): self.active = bool(active) @classmethod - def _from_db_row(cls, table_row: Dict[str, Union[int, str]], fs: str) -> 'Schedule': + def _from_db_row(cls, table_row: TableRowT, fs: str) -> 'Schedule': return cls(cast(str, table_row['path']), cast(str, table_row['schedule']), fs, @@ -147,10 +148,10 @@ class Schedule(object): cast(int, table_row['created_count']), cast(int, table_row['pruned_count']), cast(bool, table_row['active']), - ) + ) def __str__(self) -> str: - return f'''{self.path} {self.schedule} {dump_retention(self.retention)}''' + return f'{self.path} {self.schedule} {dump_retention(self.retention)}' def json_list(self) -> str: return json.dumps({'path': self.path, 'schedule': self.schedule, @@ -261,7 +262,8 @@ class Schedule(object): sched_id = c.lastrowid except sqlite3.IntegrityError: # might be adding another schedule, retrieve sched id - log.debug(f'found schedule entry for {self.path}, trying to add meta') + log.debug((f'found schedule entry for {self.path}, ' + 'trying to add meta')) c = db.execute('SELECT id FROM schedules where path = ?', (self.path,)) sched_id = c.fetchone()[0] @@ -293,7 +295,8 @@ class Schedule(object): id_ = tuple(row) if repeat or start: - meta_delete = 'DELETE FROM schedules_meta WHERE schedule_id = ?' + meta_delete = ('DELETE FROM schedules_meta ' + 'WHERE schedule_id = ?') delete_param = id_ if repeat: meta_delete += ' AND schedule = ?' @@ -314,7 +317,7 @@ class Schedule(object): id_) if meta_count.fetchone() == (0,): log.debug( - f'no more schedules left, cleaning up schedules table') + 'no more schedules left, cleaning up schedules table') db.execute('DELETE FROM schedules WHERE id = ?;', id_) else: # just delete the schedule CASCADE DELETE takes care of the @@ -328,7 +331,10 @@ class Schedule(object): WHERE path = ?''' @classmethod - def add_retention(cls, db: sqlite3.Connection, path: str, retention_spec: str) -> None: + def add_retention(cls, + db: sqlite3.Connection, + path: str, + retention_spec: str) -> None: with db: row = db.execute(cls.GET_RETENTION, (path,)).fetchone() if not row: @@ -341,13 +347,18 @@ class Schedule(object): current_retention = json.loads(current) for r, v in retention.items(): if r in current_retention: - raise ValueError((f'Retention for {r} is already present ' - 'with value {current_retention[r]}. Please remove first')) + msg = (f'Retention for {r} is already present with value' + f'{current_retention[r]}. Please remove first') + raise ValueError(msg) current_retention.update(retention) - db.execute(cls.UPDATE_RETENTION, (json.dumps(current_retention), path)) + db.execute(cls.UPDATE_RETENTION, + (json.dumps(current_retention), path)) @classmethod - def rm_retention(cls, db: sqlite3.Connection, path: str, retention_spec: str) -> None: + def rm_retention(cls, + db: sqlite3.Connection, + path: str, + retention_spec: str) -> None: with db: row = db.execute(cls.GET_RETENTION, (path,)).fetchone() if not row: @@ -357,10 +368,12 @@ class Schedule(object): current_retention = json.loads(current) for r, v in retention.items(): if r not in current_retention or current_retention[r] != v: - raise ValueError((f'Retention for {r}: {v} was not set for {path} ' - 'can\'t remove')) + msg = (f'Retention for {r}: {v} was not set for {path} ' + 'can\'t remove') + raise ValueError(msg) current_retention.pop(r) - db.execute(cls.UPDATE_RETENTION, (json.dumps(current_retention), path)) + db.execute(cls.UPDATE_RETENTION, + (json.dumps(current_retention), path)) def report(self) -> str: return self.report_json() @@ -402,11 +415,12 @@ class Schedule(object): def update_last(self, time: datetime, db: sqlite3.Connection) -> None: with db: - db.execute(self.UPDATE_LAST, (time.strftime(SNAP_DB_TS_FORMAT), - time.strftime(SNAP_DB_TS_FORMAT), - self.path, - self.start.strftime(SNAP_DB_TS_FORMAT), - self.repeat)) + db.execute(self.UPDATE_LAST, + (time.strftime(SNAP_DB_TS_FORMAT), + time.strftime(SNAP_DB_TS_FORMAT), + self.path, + self.start.strftime(SNAP_DB_TS_FORMAT), + self.repeat)) self.created_count += 1 self.last = time if not self.first: @@ -425,10 +439,12 @@ class Schedule(object): def set_inactive(self, db: sqlite3.Connection) -> None: with db: - log.debug(f'Deactivating schedule ({self.repeat}, {self.start}) on path {self.path}') - db.execute(self.UPDATE_INACTIVE, (self.path, - self.start.strftime(SNAP_DB_TS_FORMAT), - self.repeat)) + log.debug((f'Deactivating schedule ({self.repeat}, ' + f'{self.start}) on path {self.path}')) + db.execute(self.UPDATE_INACTIVE, + (self.path, + self.start.strftime(SNAP_DB_TS_FORMAT), + self.repeat)) self.active = False UPDATE_ACTIVE = '''UPDATE schedules_meta @@ -444,10 +460,12 @@ class Schedule(object): def set_active(self, db: sqlite3.Connection) -> None: with db: - log.debug(f'Activating schedule ({self.repeat}, {self.start}) on path {self.path}') - db.execute(self.UPDATE_ACTIVE, (self.path, - self.start.strftime(SNAP_DB_TS_FORMAT), - self.repeat)) + log.debug(f'Activating schedule ({self.repeat}, {self.start}) ' + f'on path {self.path}') + db.execute(self.UPDATE_ACTIVE, + (self.path, + self.start.strftime(SNAP_DB_TS_FORMAT), + self.repeat)) self.active = True UPDATE_PRUNED = '''UPDATE schedules_meta @@ -467,9 +485,10 @@ class Schedule(object): db: sqlite3.Connection, pruned: int) -> None: with db: - db.execute(self.UPDATE_PRUNED, (time.strftime(SNAP_DB_TS_FORMAT), pruned, - self.path, - self.start.strftime(SNAP_DB_TS_FORMAT), - self.repeat)) + db.execute(self.UPDATE_PRUNED, + (time.strftime(SNAP_DB_TS_FORMAT), pruned, + self.path, + self.start.strftime(SNAP_DB_TS_FORMAT), + self.repeat)) self.pruned_count += pruned self.last_pruned = time diff --git a/src/pybind/mgr/snap_schedule/fs/schedule_client.py b/src/pybind/mgr/snap_schedule/fs/schedule_client.py index 1f8da07e8ee..84950d39924 100644 --- a/src/pybind/mgr/snap_schedule/fs/schedule_client.py +++ b/src/pybind/mgr/snap_schedule/fs/schedule_client.py @@ -16,7 +16,7 @@ from threading import Timer from typing import cast, Any, Callable, Dict, Iterator, List, Set, Optional, \ Tuple, TypeVar, Union import sqlite3 -from .schedule import Schedule, parse_retention +from .schedule import Schedule import traceback @@ -34,8 +34,10 @@ log = logging.getLogger(__name__) CephfsClientT = TypeVar('CephfsClientT', bound=CephfsClient) + @contextmanager -def open_ioctx(self: CephfsClientT, pool: Union[int, str]) -> Iterator[rados.Ioctx]: +def open_ioctx(self: CephfsClientT, + pool: Union[int, str]) -> Iterator[rados.Ioctx]: try: if type(pool) is int: with self.mgr.rados.open_ioctx2(pool) as ioctx: @@ -52,6 +54,7 @@ def open_ioctx(self: CephfsClientT, pool: Union[int, str]) -> Iterator[rados.Ioc FuncT = TypeVar('FuncT', bound=Callable[..., None]) + def updates_schedule_db(func: FuncT) -> FuncT: def f(self: 'SnapSchedClient', fs: str, schedule_or_path: str, *args: Any) -> None: ret = func(self, fs, schedule_or_path, *args) @@ -93,13 +96,16 @@ def get_prune_set(candidates: Set[Tuple[cephfs.DirEntry, datetime]], if snap_ts != last: last = snap_ts if snap not in keep: - log.debug(f'keeping {snap[0].d_name} due to {period_count}{period}') + log.debug((f'keeping {snap[0].d_name} due to ' + f'{period_count}{period}')) keep.append(snap) if len(keep) == period_count: - log.debug(f'found enough snapshots for {period_count}{period}') + log.debug(('found enough snapshots for ' + f'{period_count}{period}')) break if len(keep) > MAX_SNAPS_PER_PATH: - log.info(f'Would keep more then {MAX_SNAPS_PER_PATH}, pruning keep set') + log.info((f'Would keep more then {MAX_SNAPS_PER_PATH}, ' + 'pruning keep set')) keep = keep[:MAX_SNAPS_PER_PATH] return candidates - set(keep) @@ -133,7 +139,8 @@ class SnapSchedClient(CephfsClient): size).decode('utf-8') con.executescript(db) except rados.ObjectNotFound: - log.debug(f'No schedule DB found in {fs}, creating one.') + log.debug((f'No schedule DB found in {fs}, ' + 'creating one.')) con.executescript(Schedule.CREATE_TABLES) return self.sqlite_connections[fs] @@ -156,24 +163,27 @@ class SnapSchedClient(CephfsClient): def _is_allowed_repeat(self, exec_row: Dict[str, str], path: str) -> bool: if Schedule.parse_schedule(exec_row['schedule'])[1] == 'M': if self.allow_minute_snaps: - log.debug(f'Minute repeats allowed, scheduling snapshot on path {path}') + log.debug(('Minute repeats allowed, ' + f'scheduling snapshot on path {path}')) return True else: - log.info(f'Minute repeats disabled, skipping snapshot on path {path}') + log.info(('Minute repeats disabled, ' + f'skipping snapshot on path {path}')) return False else: return True - def refresh_snap_timers(self, fs: str, path: str) -> None: try: - log.debug(f'SnapDB on {fs} changed for {path}, updating next Timer') + log.debug((f'SnapDB on {fs} changed for {path}, ' + 'updating next Timer')) db = self.get_schedule_db(fs) rows = [] with db: cur = db.execute(Schedule.EXEC_QUERY, (path,)) all_rows = cur.fetchall() - rows = [r for r in all_rows if self._is_allowed_repeat(r, path)][0:1] + rows = [r for r in all_rows + if self._is_allowed_repeat(r, path)][0:1] timers = self.active_timers.get((fs, path), []) for timer in timers: timer.cancel() @@ -195,8 +205,11 @@ class SnapSchedClient(CephfsClient): log.error(traceback.format_exc()) def create_scheduled_snapshot(self, - fs_name: str, path: str, - retention: str, start: str, repeat: str) -> None: + fs_name: str, + path: str, + retention: str, + start: str, + repeat: str) -> None: log.debug(f'Scheduled snapshot of {path} triggered') try: db = self.get_schedule_db(fs_name) @@ -259,7 +272,10 @@ class SnapSchedClient(CephfsClient): db = self.get_schedule_db(fs) return Schedule.get_db_schedules(path, db, fs) - def list_snap_schedules(self, fs: str, path: str, recursive: bool) -> List[Schedule]: + def list_snap_schedules(self, + fs: str, + path: str, + recursive: bool) -> List[Schedule]: db = self.get_schedule_db(fs) return Schedule.list_schedules(path, db, fs, recursive) -- 2.39.5