Skip to content

Commit

Permalink
skipping directories that fail to be read when walking
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb committed Feb 11, 2023
1 parent ce6734f commit b536683
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
5 changes: 5 additions & 0 deletions bin/soundconverter
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ except (ImportError, ValueError) as error:
# For args compatible to gstreamer, see `gst-launch-1.0 --help-gst`
args = Gst.init(sys.argv)

if type(args) != list:
# in tests it just suddenly returns a boolean instead. when writing tests,
# beware that `--gst-...` arguments are not filtered here
args = sys.argv

from soundconverter.util.settings import settings
from soundconverter.interface.batch import batch_main, \
CLICheck, use_memory_gsettings, validate_args
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
# USA

import sys

try:
import DistUtilsExtra.auto
except ImportError:
except ImportError as e:
sys.stderr.write('You need python-distutils-extra\n')
sys.stderr.write(e)
sys.exit(1)

import os
Expand Down
32 changes: 22 additions & 10 deletions soundconverter/util/fileoperations.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,29 @@ def vfs_walk(uri):
return a list of uri.
"""
filelist = []
dirlist = Gio.file_parse_name(uri).enumerate_children(
'*', Gio.FileMonitorFlags.NONE, None
)
for file_info in dirlist:
info = dirlist.get_child(file_info).query_file_type(
Gio.FileMonitorFlags.NONE, None

try:
dirlist = Gio.file_parse_name(uri).enumerate_children(
'*', Gio.FileMonitorFlags.NONE, None
)
if info == Gio.FileType.DIRECTORY:
filelist.extend(vfs_walk(dirlist.get_child(file_info).get_uri()))
if info == Gio.FileType.REGULAR:
filelist.append(str(dirlist.get_child(file_info).get_uri()))

for file_info in dirlist:
info = dirlist.get_child(file_info).query_file_type(
Gio.FileMonitorFlags.NONE, None
)

uri = dirlist.get_child(file_info).get_uri();

if info == Gio.FileType.DIRECTORY:
filelist.extend(vfs_walk(uri))

if info == Gio.FileType.REGULAR:
filelist.append(str(uri))
except Exception as e:
# this is impossible to write unittests for, because this only happens
# when the owner of this directory is e.g. root
logger.error('Failed to walk "%s": "%s"', uri, e)

return filelist


Expand Down
4 changes: 2 additions & 2 deletions soundconverter/util/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ def format(self, record):
if settings['debug']:
self._style._fmt = ( # noqa
'\033[{}m%(levelname)s\033[0m: '
'%(filename)s, line %(lineno)d, %(msg)s'
'%(filename)s, line %(lineno)d, %(message)s'
).format(color)
else:
self._style._fmt = ( # noqa
'\033[{}m%(levelname)s\033[0m: %(msg)s'
'\033[{}m%(levelname)s\033[0m: %(message)s'
).format(color)
return super().format(record)

Expand Down
42 changes: 17 additions & 25 deletions tests/testcases/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,14 @@ def tearDown(self):
shutil.rmtree('tests/tmp')
available_elements.update(original_available_elements)

def _wait_for_conversion_to_finish(self, window):
queue = window.converter_queue
while not queue.finished:
# as Gtk.main is replaced by gtk_iteration, the unittests
# are responsible about when soundconverter continues
# to work on the conversions and updating the GUI
gtk_iteration()

def test_conversion_simple(self):
gio_settings = get_gio_settings()
gio_settings.set_int(
Expand All @@ -576,12 +584,7 @@ def test_conversion_simple(self):
window.on_convert_button_clicked()

# wait for the assertions until all files are converted
queue = window.converter_queue
while not queue.finished:
# as Gtk.main is replaced by gtk_iteration, the unittests
# are responsible about when soundconverter continues
# to work on the conversions and updating the GUI
gtk_iteration()
self._wait_for_conversion_to_finish(window)

self.assertTrue(os.path.isdir('tests/tmp/'))
self.assertTrue(os.path.isfile('tests/tmp/a.opus'))
Expand Down Expand Up @@ -633,8 +636,7 @@ def test_conversion(self):
pipeline = queue.all_tasks[0].pipeline

# wait for the assertions until all files are converted
while not queue.finished:
gtk_iteration()
self._wait_for_conversion_to_finish(window)

self.assertEqual(len(queue.all_tasks), 3)
self.assertTrue(queue.all_tasks[0].done)
Expand Down Expand Up @@ -745,8 +747,8 @@ def test_pause_resume(self):
window.on_button_pause_clicked() # resume

start = time.time()
while not queue.finished:
gtk_iteration()
self._wait_for_conversion_to_finish(window)

if time.time() - start > 0.4:
print(
'The test may not work as intended because the conversion'
Expand Down Expand Up @@ -890,9 +892,7 @@ def test_conversion_pattern(self):

window.on_convert_button_clicked()

queue = window.converter_queue
while not queue.finished:
gtk_iteration()
self._wait_for_conversion_to_finish(window)

# input files should not have been deleted
self.assertTrue(os.path.isfile(
Expand Down Expand Up @@ -932,9 +932,7 @@ def test_non_overwriting(self):
# create a few duplicates
for _ in range(3):
window.on_convert_button_clicked()
queue = window.converter_queue
while not queue.finished:
gtk_iteration()
self._wait_for_conversion_to_finish(window)

self.assertTrue(os.path.isfile('tests/tmp/a.opus'))
self.assertTrue(os.path.isfile('tests/tmp/a_(1).opus'))
Expand All @@ -960,9 +958,7 @@ def test_delete_original(self):
window.prefs.change_mime_type('audio/ogg; codecs=opus')

window.on_convert_button_clicked()
queue = window.converter_queue
while not queue.finished:
gtk_iteration()
self._wait_for_conversion_to_finish(window)

self.assertTrue(os.path.isfile('tests/tmp/a.opus'))

Expand Down Expand Up @@ -1042,9 +1038,7 @@ def test_non_audio(self):
window.prefs.change_mime_type('audio/mpeg')

window.on_convert_button_clicked()
queue = window.converter_queue
while not queue.finished:
gtk_iteration()
self._wait_for_conversion_to_finish(window)

# it uses the commonprefix of all files, not just the valid ones
self.assertTrue(os.path.isfile('tests/tmp/test_data/audio/b/c.mp3'))
Expand Down Expand Up @@ -1084,9 +1078,7 @@ def get_active(self):
available_elements.clear()
available_elements.update({encoder, 'mp4mux'})
window.on_convert_button_clicked()
queue = window.converter_queue
while not queue.finished:
gtk_iteration()
self._wait_for_conversion_to_finish(window)
win[0].close()

path_5 = 'tests/tmp/{}/5/a.m4a'.format(encoder)
Expand Down

0 comments on commit b536683

Please sign in to comment.