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))]
|
(r'(?i)%s', include))]
|
||||||
has_exclude, has_exclude_dirs, has_include = bool(exclude), bool(exclude_dirs), bool(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_dir = entry.is_dir(follow_symlinks=follow_symlinks)
|
||||||
is_file = entry.is_file(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])
|
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'])
|
MEMCACHE['history_tab'] = History.menu_tab(MEMCACHE['history_tab_limit'])
|
||||||
|
|
||||||
try:
|
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():
|
if f.is_file():
|
||||||
MEMCACHE_FLAG_IMAGES[os.path.splitext(f.name)[0].lower()] = True
|
MEMCACHE_FLAG_IMAGES[os.path.splitext(f.name)[0].lower()] = True
|
||||||
except (BaseException, Exception):
|
except (BaseException, Exception):
|
||||||
|
|
|
@ -746,9 +746,9 @@ def cleanup_old_db_backups(filename):
|
||||||
d, filename = os.path.split(filename)
|
d, filename = os.path.split(filename)
|
||||||
if not d:
|
if not d:
|
||||||
d = sickgear.DATA_DIR
|
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
|
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),
|
re.search(r'\.db(\.v\d+)?\.r\d+$', fn.name), s_d):
|
||||||
scandir(d)):
|
|
||||||
try:
|
try:
|
||||||
os.unlink(f.path)
|
os.unlink(f.path)
|
||||||
except (BaseException, Exception):
|
except (BaseException, Exception):
|
||||||
|
|
|
@ -1567,7 +1567,9 @@ def count_files_dirs(base_dir):
|
||||||
"""
|
"""
|
||||||
f = d = 0
|
f = d = 0
|
||||||
try:
|
try:
|
||||||
files = scandir(base_dir)
|
with scandir(base_dir) as s_d:
|
||||||
|
try:
|
||||||
|
files = s_d
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
logger.warning('Unable to count files %s / %s' % (repr(e), ex(e)))
|
logger.warning('Unable to count files %s / %s' % (repr(e), ex(e)))
|
||||||
else:
|
else:
|
||||||
|
@ -1576,6 +1578,8 @@ def count_files_dirs(base_dir):
|
||||||
f += 1
|
f += 1
|
||||||
elif e.is_dir():
|
elif e.is_dir():
|
||||||
d += 1
|
d += 1
|
||||||
|
except OSError as e:
|
||||||
|
logger.warning('Unable to count files %s / %s' % (repr(e), ex(e)))
|
||||||
|
|
||||||
return f, d
|
return f, d
|
||||||
|
|
||||||
|
@ -1620,6 +1624,7 @@ def upgrade_new_naming():
|
||||||
cf = 0
|
cf = 0
|
||||||
p_text = 'Upgrading %s' % (d, 'banner/poster')[not d]
|
p_text = 'Upgrading %s' % (d, 'banner/poster')[not d]
|
||||||
_set_progress(p_text, 0, 0)
|
_set_progress(p_text, 0, 0)
|
||||||
|
with scandir(bd) as s_d:
|
||||||
for entry in scandir(bd):
|
for entry in scandir(bd):
|
||||||
if entry.is_file():
|
if entry.is_file():
|
||||||
cf += 1
|
cf += 1
|
||||||
|
@ -1658,7 +1663,9 @@ def upgrade_new_naming():
|
||||||
p_text = 'Upgrading fanart'
|
p_text = 'Upgrading fanart'
|
||||||
_set_progress(p_text, 0, 0)
|
_set_progress(p_text, 0, 0)
|
||||||
try:
|
try:
|
||||||
entries = scandir(entry.path)
|
with scandir(entry.path) as s_p:
|
||||||
|
try:
|
||||||
|
entries = s_p
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
logger.warning('Unable to stat dirs %s / %s' % (repr(e), ex(e)))
|
logger.warning('Unable to stat dirs %s / %s' % (repr(e), ex(e)))
|
||||||
continue
|
continue
|
||||||
|
@ -1675,25 +1682,34 @@ def upgrade_new_naming():
|
||||||
try:
|
try:
|
||||||
move_file(d_entry.path, new_dir_name)
|
move_file(d_entry.path, new_dir_name)
|
||||||
except (BaseException, Exception) as e:
|
except (BaseException, Exception) as e:
|
||||||
logger.warning(f'Unable to rename {d_entry.path} to {new_dir_name}:'
|
logger.warning(f'Unable to rename {d_entry.path}'
|
||||||
f' {repr(e)} / {ex(e)}')
|
f' to {new_dir_name}: {repr(e)} / {ex(e)}')
|
||||||
if os.path.isdir(new_dir_name):
|
if os.path.isdir(new_dir_name):
|
||||||
try:
|
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:
|
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:
|
else:
|
||||||
rename_args = []
|
rename_args = []
|
||||||
# noinspection PyTypeChecker
|
# noinspection PyTypeChecker
|
||||||
for f_entry in f_n:
|
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:
|
for args in rename_args:
|
||||||
try:
|
try:
|
||||||
move_file(*args)
|
move_file(*args)
|
||||||
except (BaseException, Exception) as e:
|
except (BaseException, Exception) as e:
|
||||||
logger.warning(f'Unable to rename {args[0]} to {args[1]}:'
|
logger.warning(
|
||||||
f' {repr(e)} / {ex(e)}')
|
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:
|
else:
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(d_entry.path)
|
shutil.rmtree(d_entry.path)
|
||||||
|
@ -1703,6 +1719,9 @@ def upgrade_new_naming():
|
||||||
shutil.rmtree(d_entry.path)
|
shutil.rmtree(d_entry.path)
|
||||||
except (BaseException, Exception):
|
except (BaseException, Exception):
|
||||||
pass
|
pass
|
||||||
|
except OSError as e:
|
||||||
|
logger.warning('Unable to stat dirs %s / %s' % (repr(e), ex(e)))
|
||||||
|
continue
|
||||||
try:
|
try:
|
||||||
os.rmdir(entry.path)
|
os.rmdir(entry.path)
|
||||||
except (BaseException, Exception):
|
except (BaseException, Exception):
|
||||||
|
|
|
@ -592,6 +592,7 @@ class TVInfoTests(test.SickbeardTestDBCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def tearDownClass(cls):
|
def tearDownClass(cls):
|
||||||
super(TVInfoTests, cls).tearDownClass()
|
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()}
|
files = {_f.name for _f in os.scandir(mock_data_dir) if _f.is_file()}
|
||||||
unused_files = files - used_files
|
unused_files = files - used_files
|
||||||
if delete_unused_mock_files:
|
if delete_unused_mock_files:
|
||||||
|
|
Loading…
Reference in a new issue