Coding style fixes in loader to meet PEP 8 criteria

Change str.format() to formatted string literals.
It should be a little faster and easier to read.

Signed-off-by: Slawomir Jankowski <slawomir.jankowski@intel.com>
This commit is contained in:
Slawomir Jankowski 2020-12-07 16:37:14 +01:00 committed by Robert Baldyga
parent 068d90bbea
commit d7e0bad5a4
2 changed files with 76 additions and 80 deletions

View File

@ -5,7 +5,6 @@
# #
import subprocess import subprocess
import time
import opencas import opencas
import sys import sys
import os import os
@ -19,10 +18,9 @@ except:
try: try:
config = opencas.cas_config.from_file('/etc/opencas/opencas.conf', config = opencas.cas_config.from_file('/etc/opencas/opencas.conf',
allow_incomplete=True) allow_incomplete=True)
except Exception as e: except Exception as e:
sl.syslog(sl.LOG_ERR, sl.syslog(sl.LOG_ERR, 'Unable to load opencas config. Reason: {str(e)}')
'Unable to load opencas config. Reason: {0}'.format(str(e)))
exit(1) exit(1)
for cache in config.caches.values(): for cache in config.caches.values():
@ -32,8 +30,8 @@ for cache in config.caches.values():
opencas.start_cache(cache, True) opencas.start_cache(cache, True)
except opencas.casadm.CasadmError as e: except opencas.casadm.CasadmError as e:
sl.syslog(sl.LOG_WARNING, sl.syslog(sl.LOG_WARNING,
'Unable to load cache {0} ({1}). Reason: {2}' f'Unable to load cache {cache.cache_id} ({cache.device}). '
.format(cache.cache_id, cache.device, e.result.stderr)) f'Reason: {e.result.stderr}')
exit(e.result.exit_code) exit(e.result.exit_code)
exit(0) exit(0)
for core in cache.cores.values(): for core in cache.cores.values():
@ -43,7 +41,7 @@ for cache in config.caches.values():
opencas.add_core(core, True) opencas.add_core(core, True)
except opencas.casadm.CasadmError as e: except opencas.casadm.CasadmError as e:
sl.syslog(sl.LOG_WARNING, sl.syslog(sl.LOG_WARNING,
'Unable to attach core {0} from cache {1}. Reason: {2}' f'Unable to attach core {core.device} from cache {cache.cache_id}. '
.format(core.device, cache.cache_id, e.result.stderr)) f'Reason: {e.result.stderr}')
exit(e.result.exit_code) exit(e.result.exit_code)
exit(0) exit(0)

View File

@ -39,31 +39,31 @@ class casadm:
@classmethod @classmethod
def get_version(cls): def get_version(cls):
cmd = [cls.casadm_path, cmd = [cls.casadm_path,
'--version', '--version',
'--output-format', 'csv'] '--output-format', 'csv']
return cls.run_cmd(cmd) return cls.run_cmd(cmd)
@classmethod @classmethod
def list_caches(cls): def list_caches(cls):
cmd = [cls.casadm_path, cmd = [cls.casadm_path,
'--list-caches', '--list-caches',
'--output-format', 'csv'] '--output-format', 'csv']
return cls.run_cmd(cmd) return cls.run_cmd(cmd)
@classmethod @classmethod
def check_cache_device(cls, device): def check_cache_device(cls, device):
cmd = [cls.casadm_path, cmd = [cls.casadm_path,
'--script', '--script',
'--check-cache-device', '--check-cache-device',
'--cache-device', device] '--cache-device', device]
return cls.run_cmd(cmd) return cls.run_cmd(cmd)
@classmethod @classmethod
def start_cache(cls, device, cache_id=None, cache_mode=None, def start_cache(cls, device, cache_id=None, cache_mode=None,
cache_line_size=None, load=False, force=False): cache_line_size=None, load=False, force=False):
cmd = [cls.casadm_path, cmd = [cls.casadm_path,
'--start-cache', '--start-cache',
'--cache-device', device] '--cache-device', device]
if cache_id: if cache_id:
cmd += ['--cache-id', str(cache_id)] cmd += ['--cache-id', str(cache_id)]
if cache_mode: if cache_mode:
@ -79,10 +79,10 @@ class casadm:
@classmethod @classmethod
def add_core(cls, device, cache_id, core_id=None, try_add=False): def add_core(cls, device, cache_id, core_id=None, try_add=False):
cmd = [cls.casadm_path, cmd = [cls.casadm_path,
'--script', '--script',
'--add-core', '--add-core',
'--core-device', device, '--core-device', device,
'--cache-id', str(cache_id)] '--cache-id', str(cache_id)]
if core_id is not None: if core_id is not None:
cmd += ['--core-id', str(core_id)] cmd += ['--core-id', str(core_id)]
if try_add: if try_add:
@ -92,8 +92,8 @@ class casadm:
@classmethod @classmethod
def stop_cache(cls, cache_id, no_flush=False): def stop_cache(cls, cache_id, no_flush=False):
cmd = [cls.casadm_path, cmd = [cls.casadm_path,
'--stop-cache', '--stop-cache',
'--cache-id', str(cache_id)] '--cache-id', str(cache_id)]
if no_flush: if no_flush:
cmd += ['--no-data-flush'] cmd += ['--no-data-flush']
return cls.run_cmd(cmd) return cls.run_cmd(cmd)
@ -101,10 +101,10 @@ class casadm:
@classmethod @classmethod
def remove_core(cls, cache_id, core_id, detach=False, force=False): def remove_core(cls, cache_id, core_id, detach=False, force=False):
cmd = [cls.casadm_path, cmd = [cls.casadm_path,
'--script', '--script',
'--remove-core', '--remove-core',
'--cache-id', str(cache_id), '--cache-id', str(cache_id),
'--core-id', str(core_id)] '--core-id', str(core_id)]
if detach: if detach:
cmd += ['--detach'] cmd += ['--detach']
if force: if force:
@ -114,8 +114,8 @@ class casadm:
@classmethod @classmethod
def set_param(cls, namespace, cache_id, **kwargs): def set_param(cls, namespace, cache_id, **kwargs):
cmd = [cls.casadm_path, cmd = [cls.casadm_path,
'--set-param', '--name', namespace, '--set-param', '--name', namespace,
'--cache-id', str(cache_id)] '--cache-id', str(cache_id)]
for param, value in kwargs.items(): for param, value in kwargs.items():
cmd += ['--'+param.replace('_', '-'), str(value)] cmd += ['--'+param.replace('_', '-'), str(value)]
@ -125,8 +125,8 @@ class casadm:
@classmethod @classmethod
def get_params(cls, namespace, cache_id, **kwargs): def get_params(cls, namespace, cache_id, **kwargs):
cmd = [cls.casadm_path, cmd = [cls.casadm_path,
'--get-param', '--name', namespace, '--get-param', '--name', namespace,
'--cache-id', str(cache_id)] '--cache-id', str(cache_id)]
for param, value in kwargs.items(): for param, value in kwargs.items():
cmd += ['--'+param.replace('_', '-'), str(value)] cmd += ['--'+param.replace('_', '-'), str(value)]
@ -138,18 +138,18 @@ class casadm:
@classmethod @classmethod
def flush_parameters(cls, cache_id, policy_type): def flush_parameters(cls, cache_id, policy_type):
cmd = [cls.casadm_path, cmd = [cls.casadm_path,
'--flush-parameters', '--flush-parameters',
'--cache-id', str(cache_id), '--cache-id', str(cache_id),
'--cleaning-policy-type', policy_type] '--cleaning-policy-type', policy_type]
return cls.run_cmd(cmd) return cls.run_cmd(cmd)
@classmethod @classmethod
def io_class_load_config(cls, cache_id, ioclass_file): def io_class_load_config(cls, cache_id, ioclass_file):
cmd = [cls.casadm_path, cmd = [cls.casadm_path,
'--io-class', '--io-class',
'--load-config', '--load-config',
'--cache-id', str(cache_id), '--cache-id', str(cache_id),
'--file', ioclass_file] '--file', ioclass_file]
return cls.run_cmd(cmd) return cls.run_cmd(cmd)
@classmethod @classmethod
@ -187,15 +187,15 @@ class cas_config(object):
@staticmethod @staticmethod
def check_block_device(path): def check_block_device(path):
if not os.path.exists(path) and path.startswith('/dev/cas'): if not os.path.exists(path) and path.startswith('/dev/cas'):
return return
try: try:
mode = os.stat(path).st_mode mode = os.stat(path).st_mode
except: except:
raise ValueError('{0} not found'.format(path)) raise ValueError(f'{path} not found')
if not stat.S_ISBLK(mode): if not stat.S_ISBLK(mode):
raise ValueError('{0} is not block device'.format(path)) raise ValueError(f'{path} is not block device')
class cache_config(object): class cache_config(object):
def __init__(self, cache_id, device, cache_mode, **params): def __init__(self, cache_id, device, cache_mode, **params):
@ -255,12 +255,12 @@ class cas_config(object):
elif param_name == "lazy_startup": elif param_name == "lazy_startup":
self.check_lazy_startup_valid(param_value) self.check_lazy_startup_valid(param_value)
else: else:
raise ValueError('{0} is invalid parameter name'.format(param_name)) raise ValueError(f'{param_name} is invalid parameter name')
@staticmethod @staticmethod
def check_cache_id_valid(cache_id): def check_cache_id_valid(cache_id):
if not 1 <= int(cache_id) <= 16384: if not 1 <= int(cache_id) <= 16384:
raise ValueError('{0} is invalid cache id'.format(cache_id)) raise ValueError(f'{cache_id} is invalid cache id')
def check_cache_device_empty(self): def check_cache_device_empty(self):
try: try:
@ -273,17 +273,16 @@ class cas_config(object):
if len(list(filter(lambda a: a != '', result.stdout.split('\n')))) > 1: if len(list(filter(lambda a: a != '', result.stdout.split('\n')))) > 1:
raise ValueError( raise ValueError(
'Partitions found on device {0}. Use force option to ignore'. 'Partitions found on device {self.device}. Use force option to ignore'
format(self.device)) )
def check_cache_mode_valid(self, cache_mode): def check_cache_mode_valid(self, cache_mode):
if cache_mode.lower() not in ['wt', 'pt', 'wa', 'wb', 'wo']: if cache_mode.lower() not in ['wt', 'pt', 'wa', 'wb', 'wo']:
raise ValueError('Invalid cache mode {0}'.format(cache_mode)) raise ValueError(f'Invalid cache mode {cache_mode}')
def check_cleaning_policy_valid(self, cleaning_policy): def check_cleaning_policy_valid(self, cleaning_policy):
if cleaning_policy.lower() not in ['acp', 'alru', 'nop']: if cleaning_policy.lower() not in ['acp', 'alru', 'nop']:
raise ValueError('{0} is invalid cleaning policy name'.format( raise ValueError(f'{cleaning_policy} is invalid cleaning policy name')
cleaning_policy))
def check_lazy_startup_valid(self, lazy_startup): def check_lazy_startup_valid(self, lazy_startup):
if lazy_startup.lower() not in ["true", "false"]: if lazy_startup.lower() not in ["true", "false"]:
@ -291,13 +290,11 @@ class cas_config(object):
def check_promotion_policy_valid(self, promotion_policy): def check_promotion_policy_valid(self, promotion_policy):
if promotion_policy.lower() not in ['always', 'nhit']: if promotion_policy.lower() not in ['always', 'nhit']:
raise ValueError('{0} is invalid promotion policy name'.format( raise ValueError(f'{promotion_policy} is invalid promotion policy name')
promotion_policy))
def check_cache_line_size_valid(self, cache_line_size): def check_cache_line_size_valid(self, cache_line_size):
if cache_line_size not in ['4', '8', '16', '32', '64']: if cache_line_size not in ['4', '8', '16', '32', '64']:
raise ValueError('{0} is invalid cache line size'.format( raise ValueError(f'{cache_line_size} is invalid cache line size')
cache_line_size))
def check_recursive(self): def check_recursive(self):
if not self.device.startswith('/dev/cas'): if not self.device.startswith('/dev/cas'):
@ -310,7 +307,7 @@ class cas_config(object):
raise ValueError('Recursive configuration detected') raise ValueError('Recursive configuration detected')
def to_line(self): def to_line(self):
ret = '{0}\t{1}\t{2}'.format(self.cache_id, self.device, self.cache_mode) ret = f'{self.cache_id}\t{self.device}\t{self.cache_mode}'
if len(self.params) > 0: if len(self.params) > 0:
i = 0 i = 0
for param, value in self.params.items(): for param, value in self.params.items():
@ -319,7 +316,7 @@ class cas_config(object):
else: else:
ret += '\t' ret += '\t'
ret += '{0}={1}'.format(param, value) ret += f'{param}={value}'
i += 1 i += 1
ret += '\n' ret += '\n'
@ -378,16 +375,14 @@ class cas_config(object):
if param_name == "lazy_startup": if param_name == "lazy_startup":
if param_value.lower() not in ["true", "false"]: if param_value.lower() not in ["true", "false"]:
raise ValueError( raise ValueError(
"{} is invalid value for '{}' core param".format( f"{param_value} is invalid value for '{param_name}' core param"
param_value, param_name
)
) )
else: else:
raise ValueError("'{}' is invalid core param name".format(param_name)) raise ValueError(f"'{param_name}' is invalid core param name")
def check_core_id_valid(self): def check_core_id_valid(self):
if not 0 <= int(self.core_id) <= 4095: if not 0 <= int(self.core_id) <= 4095:
raise ValueError('{0} is invalid core id'.format(self.core_id)) raise ValueError(f'{self.core_id} is invalid core id')
def check_recursive(self): def check_recursive(self):
if not self.device.startswith('/dev/cas'): if not self.device.startswith('/dev/cas'):
@ -400,11 +395,11 @@ class cas_config(object):
raise ValueError('Recursive configuration detected') raise ValueError('Recursive configuration detected')
def to_line(self): def to_line(self):
ret = "{0}\t{1}\t{2}".format(self.cache_id, self.core_id, self.device) ret = f"{self.cache_id}\t{self.core_id}\t{self.device}"
for i, (param, value) in enumerate(self.params.items()): for i, (param, value) in enumerate(self.params.items()):
ret += "," if i > 0 else "\t" ret += "," if i > 0 else "\t"
ret += "{0}={1}".format(param, value) ret += f"{param}={value}"
ret += "\n" ret += "\n"
return ret return ret
@ -493,7 +488,7 @@ class cas_config(object):
def insert_core(self, new_core_config): def insert_core(self, new_core_config):
if new_core_config.cache_id not in self.caches: if new_core_config.cache_id not in self.caches:
raise KeyError('Cache id {0} doesn\'t exist'.format(new_core_config.cache_id)) raise KeyError(f'Cache id {new_core_config.cache_id} doesn\'t exist')
try: try:
for cache_id, cache in self.caches.items(): for cache_id, cache in self.caches.items():
@ -537,7 +532,7 @@ class cas_config(object):
def write(self, config_file): def write(self, config_file):
try: try:
with open(config_file, 'w') as conf: with open(config_file, 'w') as conf:
conf.write('{0}\n'.format(self.version_tag)) conf.write(f'{self.version_tag}\n')
conf.write('# This config was automatically generated\n') conf.write('# This config was automatically generated\n')
conf.write('[caches]\n') conf.write('[caches]\n')
@ -547,7 +542,6 @@ class cas_config(object):
conf.write('\n[cores]\n') conf.write('\n[cores]\n')
for core in self.cores: for core in self.cores:
conf.write(core.to_line()) conf.write(core.to_line())
except: except:
raise Exception('Couldn\'t write config file') raise Exception('Couldn\'t write config file')
@ -563,6 +557,7 @@ def start_cache(cache, load, force=False):
load=load, load=load,
force=force) force=force)
def configure_cache(cache): def configure_cache(cache):
if "cleaning_policy" in cache.params: if "cleaning_policy" in cache.params:
casadm.set_param( casadm.set_param(
@ -587,6 +582,7 @@ def add_core(core, attach):
# Another helper functions # Another helper functions
def is_cache_started(cache_config): def is_cache_started(cache_config):
dev_list = get_caches_list() dev_list = get_caches_list()
for dev in dev_list: for dev in dev_list:
@ -595,6 +591,7 @@ def is_cache_started(cache_config):
return False return False
def is_core_added(core_config): def is_core_added(core_config):
dev_list = get_caches_list() dev_list = get_caches_list()
cache_id = 0 cache_id = 0
@ -609,14 +606,17 @@ def is_core_added(core_config):
return False return False
def get_caches_list(): def get_caches_list():
result = casadm.list_caches() result = casadm.list_caches()
return list(csv.DictReader(result.stdout.split('\n'))) return list(csv.DictReader(result.stdout.split('\n')))
def check_cache_device(device): def check_cache_device(device):
result = casadm.check_cache_device(device) result = casadm.check_cache_device(device)
return list(csv.DictReader(result.stdout.split('\n')))[0] return list(csv.DictReader(result.stdout.split('\n')))[0]
def get_cas_version(): def get_cas_version():
version = casadm.get_version() version = casadm.get_version()
@ -640,7 +640,7 @@ class CompoundException(Exception):
s = "Multiple exceptions occured:\n" if len(self.exception_list) > 1 else "" s = "Multiple exceptions occured:\n" if len(self.exception_list) > 1 else ""
for e in self.exception_list: for e in self.exception_list:
s += '{0}\n'.format(str(e)) s += f'{str(e)}\n'
return s return s
@ -659,6 +659,7 @@ class CompoundException(Exception):
else: else:
raise self raise self
def detach_core_recursive(cache_id, core_id, flush): def detach_core_recursive(cache_id, core_id, flush):
# Catching exceptions is left to uppermost caller of detach_core_recursive # Catching exceptions is left to uppermost caller of detach_core_recursive
# as the immediate caller that made a recursive call depends on the callee # as the immediate caller that made a recursive call depends on the callee
@ -668,12 +669,13 @@ def detach_core_recursive(cache_id, core_id, flush):
if dev['type'] == 'cache': if dev['type'] == 'cache':
l_cache_id = dev['id'] l_cache_id = dev['id']
elif dev['type'] == 'core' and dev['status'] == 'Active': elif dev['type'] == 'core' and dev['status'] == 'Active':
if '/dev/cas{0}-{1}'.format(cache_id, core_id) in dev['disk']: if f'/dev/cas{cache_id}-{core_id}' in dev['disk']:
detach_core_recursive(l_cache_id, dev['id'], flush) detach_core_recursive(l_cache_id, dev['id'], flush)
elif l_cache_id == cache_id and dev['id'] == core_id and dev['status'] != 'Active': elif l_cache_id == cache_id and dev['id'] == core_id and dev['status'] != 'Active':
return return
casadm.remove_core(cache_id, core_id, detach = True, force = not flush) casadm.remove_core(cache_id, core_id, detach=True, force=not flush)
def detach_all_cores(flush): def detach_all_cores(flush):
error = CompoundException() error = CompoundException()
@ -681,8 +683,7 @@ def detach_all_cores(flush):
try: try:
dev_list = get_caches_list() dev_list = get_caches_list()
except casadm.CasadmError as e: except casadm.CasadmError as e:
raise Exception('Unable to list caches. Reason:\n{0}'.format( raise Exception(f'Unable to list caches. Reason:\n{e.result.stderr}')
e.result.stderr))
except: except:
raise Exception('Unable to list caches.') raise Exception('Unable to list caches.')
@ -696,22 +697,20 @@ def detach_all_cores(flush):
detach_core_recursive(cache_id, dev['id'], flush) detach_core_recursive(cache_id, dev['id'], flush)
except casadm.CasadmError as e: except casadm.CasadmError as e:
error.add_exception(Exception( error.add_exception(Exception(
'Unable to detach core {0}. Reason:\n{1}'.format( f"Unable to detach core {dev['disk']}. Reason:\n{e.result.stderr}"))
dev['disk'], e.result.stderr)))
except: except:
error.add_exception(Exception( error.add_exception(Exception(f"Unable to detach core {dev['disk']}."))
'Unable to detach core {0}.'.format(dev['disk'])))
error.raise_nonempty() error.raise_nonempty()
def stop_all_caches(flush): def stop_all_caches(flush):
error = CompoundException() error = CompoundException()
try: try:
dev_list = get_caches_list() dev_list = get_caches_list()
except casadm.CasadmError as e: except casadm.CasadmError as e:
raise Exception('Unable to list caches. Reason:\n{0}'.format( raise Exception(f'Unable to list caches. Reason:\n{e.result.stderr}')
e.result.stderr))
except: except:
raise Exception('Unable to list caches.') raise Exception('Unable to list caches.')
@ -723,14 +722,13 @@ def stop_all_caches(flush):
casadm.stop_cache(dev['id'], not flush) casadm.stop_cache(dev['id'], not flush)
except casadm.CasadmError as e: except casadm.CasadmError as e:
error.add_exception(Exception( error.add_exception(Exception(
'Unable to stop cache {0}. Reason:\n{1}'.format( f"Unable to stop cache {dev['disk']}. Reason:\n{e.result.stderr}"))
dev['disk'], e.result.stderr)))
except: except:
error.add_exception(Exception( error.add_exception(Exception(f"Unable to stop cache {dev['disk']}."))
'Unable to stop cache {0}.'.format(dev['disk'])))
error.raise_nonempty() error.raise_nonempty()
def stop(flush): def stop(flush):
error = CompoundException() error = CompoundException()
@ -837,7 +835,7 @@ def wait_for_startup(timeout=300, interval=5):
cas_config.default_location, allow_incomplete=True cas_config.default_location, allow_incomplete=True
) )
except Exception as e: except Exception as e:
raise Exception("Unable to load opencas config. Reason: {0}".format(str(e))) raise Exception(f"Unable to load opencas config. Reason: {str(e)}")
not_initialized = _get_uninitialized_devices(config) not_initialized = _get_uninitialized_devices(config)
if not not_initialized: if not not_initialized: