From f8afbae2cd34dc9026fcf8af3220e070b0536611 Mon Sep 17 00:00:00 2001 From: jonaswinkler <17569239+jonaswinkler@users.noreply.github.com> Date: Wed, 19 May 2021 19:56:01 +0200 Subject: [PATCH] ignore macOS specific files --- .../management/commands/document_consumer.py | 16 ++++++++++++- .../tests/test_management_consumer.py | 24 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/documents/management/commands/document_consumer.py b/src/documents/management/commands/document_consumer.py index 969941af9..9f0ce79c3 100644 --- a/src/documents/management/commands/document_consumer.py +++ b/src/documents/management/commands/document_consumer.py @@ -36,8 +36,19 @@ def _tags_from_path(filepath): return tag_ids +def _is_ignored(filepath): + # https://github.com/jonaswinkler/paperless-ng/discussions/1037 + basename = os.path.basename(filepath) + if basename == ".DS_STORE": + return True + if basename.startswith("._"): + return True + + return False + + def _consume(filepath): - if os.path.isdir(filepath): + if os.path.isdir(filepath) or _is_ignored(filepath): return if not os.path.isfile(filepath): @@ -71,6 +82,9 @@ def _consume(filepath): def _consume_wait_unmodified(file): + if _is_ignored(file): + return + logger.debug(f"Waiting for file {file} to remain unmodified") mtime = -1 current_try = 0 diff --git a/src/documents/tests/test_management_consumer.py b/src/documents/tests/test_management_consumer.py index 2111705e0..ec5a8dc0b 100644 --- a/src/documents/tests/test_management_consumer.py +++ b/src/documents/tests/test_management_consumer.py @@ -60,10 +60,10 @@ class ConsumerMixin: super(ConsumerMixin, self).tearDown() - def wait_for_task_mock_call(self): + def wait_for_task_mock_call(self, excpeted_call_count=1): n = 0 while n < 100: - if self.task_mock.call_count > 0: + if self.task_mock.call_count >= excpeted_call_count: # give task_mock some time to finish and raise errors sleep(1) return @@ -202,6 +202,26 @@ class TestConsumer(DirectoriesMixin, ConsumerMixin, TransactionTestCase): self.assertRaises(CommandError, call_command, 'document_consumer', '--oneshot') + def test_mac_write(self): + self.task_mock.side_effect = self.bogus_task + + self.t_start() + + shutil.copy(self.sample_file, os.path.join(self.dirs.consumption_dir, ".DS_STORE")) + shutil.copy(self.sample_file, os.path.join(self.dirs.consumption_dir, "my_file.pdf")) + shutil.copy(self.sample_file, os.path.join(self.dirs.consumption_dir, "._my_file.pdf")) + shutil.copy(self.sample_file, os.path.join(self.dirs.consumption_dir, "my_second_file.pdf")) + shutil.copy(self.sample_file, os.path.join(self.dirs.consumption_dir, "._my_second_file.pdf")) + + sleep(5) + + self.wait_for_task_mock_call(excpeted_call_count=2) + + self.assertEqual(2, self.task_mock.call_count) + + fnames = [os.path.basename(args[1]) for args, _ in self.task_mock.call_args_list] + self.assertCountEqual(fnames, ["my_file.pdf", "my_second_file.pdf"]) + @override_settings(CONSUMER_POLLING=1, CONSUMER_POLLING_DELAY=1, CONSUMER_POLLING_RETRY_COUNT=20) class TestConsumerPolling(TestConsumer):