diff --git a/api/funkwhale_api/providers/audiofile/management/commands/import_files.py b/api/funkwhale_api/providers/audiofile/management/commands/import_files.py index de2560d3c..8de779230 100644 --- a/api/funkwhale_api/providers/audiofile/management/commands/import_files.py +++ b/api/funkwhale_api/providers/audiofile/management/commands/import_files.py @@ -13,7 +13,7 @@ class Command(BaseCommand): help = "Import audio files mathinc given glob pattern" def add_arguments(self, parser): - parser.add_argument("path", type=str) + parser.add_argument("path", nargs="+", type=str) parser.add_argument( "--recursive", action="store_true", @@ -65,10 +65,13 @@ class Command(BaseCommand): def handle(self, *args, **options): glob_kwargs = {} + matching = [] if options["recursive"]: glob_kwargs["recursive"] = True try: - matching = sorted(glob.glob(options["path"], **glob_kwargs)) + for import_path in options["path"]: + matching += glob.glob(import_path, **glob_kwargs) + matching = sorted(list(set(matching))) except TypeError: raise Exception("You need Python 3.5 to use the --recursive flag") @@ -109,7 +112,7 @@ class Command(BaseCommand): "No superuser available, please provide a --username" ) - filtered = self.filter_matching(matching, options) + filtered = self.filter_matching(matching) self.stdout.write("Import summary:") self.stdout.write( "- {} files found matching this pattern: {}".format( @@ -153,7 +156,7 @@ class Command(BaseCommand): "For details, please refer to import batch #{}".format(batch.pk) ) - def filter_matching(self, matching, options): + def filter_matching(self, matching): sources = ["file://{}".format(p) for p in matching] # we skip reimport for path that are already found # as a TrackFile.source diff --git a/api/tests/test_import_audio_file.py b/api/tests/test_import_audio_file.py index 67f6c489d..b5ed01e3c 100644 --- a/api/tests/test_import_audio_file.py +++ b/api/tests/test_import_audio_file.py @@ -103,6 +103,18 @@ def test_in_place_import_only_from_music_dir(factories, settings): ) +def test_import_with_multiple_argument(factories, mocker): + factories["users.User"](username="me") + path1 = os.path.join(DATA_DIR, "dummy_file.ogg") + path2 = os.path.join(DATA_DIR, "utf8-éà◌.ogg") + mocked_filter = mocker.patch( + "funkwhale_api.providers.audiofile.management.commands.import_files.Command.filter_matching", + return_value=({"new": [], "skipped": []}), + ) + call_command("import_files", path1, path2, username="me", interactive=False) + mocked_filter.assert_called_once_with([path1, path2]) + + def test_import_files_creates_a_batch_and_job(factories, mocker): m = mocker.patch("funkwhale_api.music.tasks.import_job_run") user = factories["users.User"](username="me") diff --git a/changes/changelog.d/242.feature b/changes/changelog.d/242.feature new file mode 100644 index 000000000..9579b6ac8 --- /dev/null +++ b/changes/changelog.d/242.feature @@ -0,0 +1 @@ +Command line import now accepts unlimited args (#242)