mirror of
https://github.com/SickGear/SickGear.git
synced 2024-11-24 13:55:16 +00:00
use context manager for scandir as recommended since python 3.6
This commit is contained in:
parent
6abec1d45d
commit
aeb3f7b0dc
5 changed files with 152 additions and 130 deletions
|
@ -1120,7 +1120,8 @@ def scantree(path, # type: AnyStr
|
|||
(r'(?i)%s', include))]
|
||||
has_exclude, has_exclude_dirs, has_include = bool(exclude), bool(exclude_dirs), bool(include)
|
||||
|
||||
for entry in scandir(path):
|
||||
with scandir(path) as s_d:
|
||||
for entry in s_d:
|
||||
is_dir = entry.is_dir(follow_symlinks=follow_symlinks)
|
||||
is_file = entry.is_file(follow_symlinks=follow_symlinks)
|
||||
no_filter = any([None is filter_kind, filter_kind and is_dir, not filter_kind and is_file])
|
||||
|
|
|
@ -1721,7 +1721,8 @@ def init_stage_2():
|
|||
MEMCACHE['history_tab'] = History.menu_tab(MEMCACHE['history_tab_limit'])
|
||||
|
||||
try:
|
||||
for f in scandir(os.path.join(PROG_DIR, 'gui', GUI_NAME, 'images', 'flags')):
|
||||
with scandir(os.path.join(PROG_DIR, 'gui', GUI_NAME, 'images', 'flags')) as s_d:
|
||||
for f in s_d:
|
||||
if f.is_file():
|
||||
MEMCACHE_FLAG_IMAGES[os.path.splitext(f.name)[0].lower()] = True
|
||||
except (BaseException, Exception):
|
||||
|
|
|
@ -746,9 +746,9 @@ def cleanup_old_db_backups(filename):
|
|||
d, filename = os.path.split(filename)
|
||||
if not d:
|
||||
d = sickgear.DATA_DIR
|
||||
with scandir(d) as s_d:
|
||||
for f in filter(lambda fn: fn.is_file() and filename in fn.name and
|
||||
re.search(r'\.db(\.v\d+)?\.r\d+$', fn.name),
|
||||
scandir(d)):
|
||||
re.search(r'\.db(\.v\d+)?\.r\d+$', fn.name), s_d):
|
||||
try:
|
||||
os.unlink(f.path)
|
||||
except (BaseException, Exception):
|
||||
|
|
|
@ -1567,7 +1567,9 @@ def count_files_dirs(base_dir):
|
|||
"""
|
||||
f = d = 0
|
||||
try:
|
||||
files = scandir(base_dir)
|
||||
with scandir(base_dir) as s_d:
|
||||
try:
|
||||
files = s_d
|
||||
except OSError as e:
|
||||
logger.warning('Unable to count files %s / %s' % (repr(e), ex(e)))
|
||||
else:
|
||||
|
@ -1576,6 +1578,8 @@ def count_files_dirs(base_dir):
|
|||
f += 1
|
||||
elif e.is_dir():
|
||||
d += 1
|
||||
except OSError as e:
|
||||
logger.warning('Unable to count files %s / %s' % (repr(e), ex(e)))
|
||||
|
||||
return f, d
|
||||
|
||||
|
@ -1620,6 +1624,7 @@ def upgrade_new_naming():
|
|||
cf = 0
|
||||
p_text = 'Upgrading %s' % (d, 'banner/poster')[not d]
|
||||
_set_progress(p_text, 0, 0)
|
||||
with scandir(bd) as s_d:
|
||||
for entry in scandir(bd):
|
||||
if entry.is_file():
|
||||
cf += 1
|
||||
|
@ -1658,7 +1663,9 @@ def upgrade_new_naming():
|
|||
p_text = 'Upgrading fanart'
|
||||
_set_progress(p_text, 0, 0)
|
||||
try:
|
||||
entries = scandir(entry.path)
|
||||
with scandir(entry.path) as s_p:
|
||||
try:
|
||||
entries = s_p
|
||||
except OSError as e:
|
||||
logger.warning('Unable to stat dirs %s / %s' % (repr(e), ex(e)))
|
||||
continue
|
||||
|
@ -1675,25 +1682,34 @@ def upgrade_new_naming():
|
|||
try:
|
||||
move_file(d_entry.path, new_dir_name)
|
||||
except (BaseException, Exception) as e:
|
||||
logger.warning(f'Unable to rename {d_entry.path} to {new_dir_name}:'
|
||||
f' {repr(e)} / {ex(e)}')
|
||||
logger.warning(f'Unable to rename {d_entry.path}'
|
||||
f' to {new_dir_name}: {repr(e)} / {ex(e)}')
|
||||
if os.path.isdir(new_dir_name):
|
||||
try:
|
||||
f_n = filter(lambda fn: fn.is_file(), scandir(new_dir_name))
|
||||
with scandir(new_dir_name) as s_d_n:
|
||||
try:
|
||||
f_n = filter(lambda fn: fn.is_file(), s_d_n)
|
||||
except OSError as e:
|
||||
logger.warning('Unable to rename %s / %s' % (repr(e), ex(e)))
|
||||
logger.warning(
|
||||
f'Unable to rename {repr(e)} / {ex(d)}')
|
||||
else:
|
||||
rename_args = []
|
||||
# noinspection PyTypeChecker
|
||||
for f_entry in f_n:
|
||||
rename_args += [(f_entry.path, bp_match.sub(r'\2', f_entry.path))]
|
||||
rename_args += [
|
||||
(f_entry.path,
|
||||
bp_match.sub(r'\2', f_entry.path))]
|
||||
|
||||
for args in rename_args:
|
||||
try:
|
||||
move_file(*args)
|
||||
except (BaseException, Exception) as e:
|
||||
logger.warning(f'Unable to rename {args[0]} to {args[1]}:'
|
||||
f' {repr(e)} / {ex(e)}')
|
||||
logger.warning(
|
||||
f'Unable to rename {args[0]}'
|
||||
f' to {args[1]}: {repr(e)} / {ex(e)}')
|
||||
except OSError as e:
|
||||
logger.warning(
|
||||
'Unable to rename %s / %s' % (repr(e), ex(e)))
|
||||
else:
|
||||
try:
|
||||
shutil.rmtree(d_entry.path)
|
||||
|
@ -1703,6 +1719,9 @@ def upgrade_new_naming():
|
|||
shutil.rmtree(d_entry.path)
|
||||
except (BaseException, Exception):
|
||||
pass
|
||||
except OSError as e:
|
||||
logger.warning('Unable to stat dirs %s / %s' % (repr(e), ex(e)))
|
||||
continue
|
||||
try:
|
||||
os.rmdir(entry.path)
|
||||
except (BaseException, Exception):
|
||||
|
|
|
@ -592,6 +592,7 @@ class TVInfoTests(test.SickbeardTestDBCase):
|
|||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
super(TVInfoTests, cls).tearDownClass()
|
||||
with os.scandir(mock_data_dir) as s_d:
|
||||
files = {_f.name for _f in os.scandir(mock_data_dir) if _f.is_file()}
|
||||
unused_files = files - used_files
|
||||
if delete_unused_mock_files:
|
||||
|
|
Loading…
Reference in a new issue