Repository: CS-SI/eodag

Issue:
flatten_top_directories on single file
The following error is raised when trying to use `flatten_top_directories()` on a directory containing a single file or when downloading a product having a single asset and the download plugin configured with `flatten_top_dirs: true`:
```
Traceback (most recent call last):
  File "/home/sylvain/workspace/eodag-debug/20230207_download_issue.py", line 78, in <module>
    downloaded_path = dag.download(search_results[0])
  File "/home/sylvain/workspace/eodag/eodag/api/core.py", line 1753, in download
    path = product.download(
  File "/home/sylvain/workspace/eodag/eodag/api/product/_product.py", line 313, in download
    fs_path = self.downloader.download(
  File "/home/sylvain/workspace/eodag/eodag/plugins/download/http.py", line 260, in download
    fs_path = self._download_assets(
  File "/home/sylvain/workspace/eodag/eodag/plugins/download/http.py", line 607, in _download_assets
    flatten_top_directories(fs_dir_path)
  File "/home/sylvain/workspace/eodag/eodag/utils/__init__.py", line 1159, in flatten_top_directories
    shutil.copytree(common_subdirs_path, tmp_path)
  File "/usr/lib/python3.8/shutil.py", line 555, in copytree
    with os.scandir(src) as itr:
NotADirectoryError: [Errno 20] Not a directory: '/tmp/foo_path'
```

Discussion:
github-actions[bot]: ## Unit Test Results
    2 files  ±0      2 suites  ±0   4m 22s [:stopwatch:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "duration of all tests") - 1m 53s
356 tests +1  353 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "passed tests") +1  2 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "skipped / disabled tests")  - 1  1 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "failed tests") +1 
712 runs  +2  706 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "passed tests") +2  5 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "skipped / disabled tests")  - 1  1 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "failed tests") +1 

For more details on these failures, see [this check](https://github.com/CS-SI/eodag/runs/11170982115).

Results for commit 976a5454. ± Comparison against base commit 69d24e85.

[test-results]:data:application/gzip;base64,H4sIAEF14mMC/03MSQ7CIACF4as0rF0AZRAvYxgTYlsMw8p4d6Et2OX/veR9gPOLTeAx4dsEUvF5hClRZh+2lqxBnXIbZ8p6PVPReqf5Ty//Pi8OcNIvFdAAG2OIVWCVWLb2yRE+o19yyIYcj7T35XDv658O6+pzDSA4k5RQgpQwVhljBMUEWSOVtNRBKGcG745j8P0BpV/4CgUBAAA=


Patch:
diff --git a/eodag/utils/__init__.py b/eodag/utils/__init__.py
index 35841aaf..d968c733 100644
--- a/eodag/utils/__init__.py
+++ b/eodag/utils/__init__.py
@@ -1148,6 +1148,9 @@ def flatten_top_directories(nested_dir_root, common_subdirs_path=None):
         subpaths_list = [p for p in Path(nested_dir_root).glob("**/*") if p.is_file()]
         common_subdirs_path = os.path.commonpath(subpaths_list)
 
+    if Path(common_subdirs_path).is_file():
+        common_subdirs_path = os.path.dirname(common_subdirs_path)
+
     if nested_dir_root != common_subdirs_path:
         logger.debug(f"Flatten {common_subdirs_path} to {nested_dir_root}")
         tmp_path = mkdtemp()


Test patch:
diff --git a/tests/units/test_download_plugins.py b/tests/units/test_download_plugins.py
index b3514aec..e15a565b 100644
--- a/tests/units/test_download_plugins.py
+++ b/tests/units/test_download_plugins.py
@@ -487,7 +487,7 @@ class TestDownloadPluginHttpRetry(BaseDownloadPluginTest):
                 self.plugin.download(
                     self.product,
                     outputs_prefix=self.output_dir,
-                    wait=0.1 / 60,
+                    wait=0.01 / 60,
                     timeout=0.2 / 60,
                 )
 
diff --git a/tests/units/test_utils.py b/tests/units/test_utils.py
index 04b4ad9b..90697d31 100644
--- a/tests/units/test_utils.py
+++ b/tests/units/test_utils.py
@@ -241,6 +241,20 @@ class TestUtils(unittest.TestCase):
             self.assertIn(Path(nested_dir_root) / "c2" / "bar", dir_content)
             self.assertIn(Path(nested_dir_root) / "c2" / "baz", dir_content)
 
+    def test_flatten_top_dirs_single_file(self):
+        """flatten_top_dirs must flatten directory structure containing a single file"""
+        with TemporaryDirectory() as nested_dir_root:
+            os.makedirs(os.path.join(nested_dir_root, "a", "b", "c1"))
+            # create empty file
+            open(os.path.join(nested_dir_root, "a", "b", "c1", "foo"), "a").close()
+
+            flatten_top_directories(nested_dir_root)
+
+            dir_content = list(Path(nested_dir_root).glob("**/*"))
+
+            self.assertEqual(len(dir_content), 1)
+            self.assertIn(Path(nested_dir_root) / "foo", dir_content)
+
     def test_flatten_top_dirs_given_subdir(self):
         """flatten_top_dirs must flatten directory structure using given subdirectory"""
         with TemporaryDirectory() as nested_dir_root: