Repository: translate/pootle

Issue:
Dont create project if command fails in init_fs_project
atm if for some reason this command fails it leaves a project behind



Discussion:

Resolving patch:
diff --git a/pootle/apps/pootle_fs/localfs.py b/pootle/apps/pootle_fs/localfs.py
--- a/pootle/apps/pootle_fs/localfs.py
+++ b/pootle/apps/pootle_fs/localfs.py
@@ -16,6 +16,7 @@
 from pootle.core.delegate import revision
 from pootle_project.models import Project
 
+from .exceptions import FSFetchError
 from .plugin import Plugin
 
 
@@ -39,13 +40,16 @@ def push(self, response):
         return response
 
     def fetch(self):
-        synced = dirsync.sync(
-            self.fs_url,
-            self.project.local_fs_path,
-            "sync",
-            create=True,
-            purge=True,
-            logger=logging.getLogger(dirsync.__name__))
+        try:
+            synced = dirsync.sync(
+                self.fs_url,
+                self.project.local_fs_path,
+                "sync",
+                create=True,
+                purge=True,
+                logger=logging.getLogger(dirsync.__name__))
+        except ValueError as e:
+            raise FSFetchError(e)
         if synced:
             revision.get(Project)(self.project).set(
                 keys=["pootle.fs.fs_hash"], value=uuid.uuid4().hex)
diff --git a/pootle/apps/pootle_fs/management/commands/init_fs_project.py b/pootle/apps/pootle_fs/management/commands/init_fs_project.py
--- a/pootle/apps/pootle_fs/management/commands/init_fs_project.py
+++ b/pootle/apps/pootle_fs/management/commands/init_fs_project.py
@@ -12,6 +12,7 @@
 from django.core.management import BaseCommand, CommandError
 
 from pootle_format.models import Format
+from pootle_fs.exceptions import FSFetchError
 from pootle_fs.utils import FSPlugin, parse_fs_url
 from pootle_language.models import Language
 from pootle_project.models import Project
@@ -112,7 +113,11 @@ def handle(self, **options):
             'default': options['translation_mapping']
         }
         if options['sync']:
-            plugin = FSPlugin(project)
-            plugin.fetch()
-            plugin.add()
-            plugin.sync()
+            try:
+                plugin = FSPlugin(project)
+                plugin.fetch()
+                plugin.add()
+                plugin.sync()
+            except FSFetchError as e:
+                project.delete()
+                raise CommandError(e)


Test patch:
diff --git a/tests/pootle_fs/commands/init_fs_project.py b/tests/pootle_fs/commands/init_fs_project.py
--- a/tests/pootle_fs/commands/init_fs_project.py
+++ b/tests/pootle_fs/commands/init_fs_project.py
@@ -117,3 +117,20 @@ def test_cmd_init_fs_project_bad_filetype(capsys):
             fs_path,
             tr_path,
             "--filetypes", "NO_SUCH_FILETYPE")
+
+
+@pytest.mark.django_db
+@pytest.mark.cmd
+def test_cmd_init_fs_project_bad_filepath(capsys):
+    fs_path = "/test/fs/path"
+    tr_path = "<language_code>/<filename>.<ext>"
+
+    with pytest.raises(CommandError) as e:
+        call_command(
+            "init_fs_project",
+            "foo",
+            fs_path,
+            tr_path,
+            "--filetypes", "po")
+    assert not Project.objects.filter(code="foo").exists()
+    assert "Source directory does not exist" in str(e)