Repository: getnikola/nikola

Issue:
DEPLOY_DRAFTS and DEPLOY_FUTURE ignored by github_deploy
```
FUTURE_IS_NOW = False
DEPLOY_FUTURE = False
DEPLOY_DRAFTS = False
```

With these settings, drafts with past dates are getting deployed.



Discussion:
Cannot reproduce. Please apply this patch over `deploy.py` in your Nikola install and show output of `nikola deploy`:

``` diff
diff --git a/nikola/plugins/command/deploy.py b/nikola/plugins/command/deploy.py
index 365c0a3..95fbfec 100644
--- a/nikola/plugins/command/deploy.py
+++ b/nikola/plugins/command/deploy.py
@@ -97,9 +97,14 @@ class CommandDeploy(Command):
             for post in self.site.timeline:
                 if (not deploy_drafts and post.is_draft) or \
                    (not deploy_future and post.publish_later):
+                    print("Delete", post, post.is_draft, post.publish_later)
                     remove_file(os.path.join(out_dir, post.destination_path()))
                     remove_file(os.path.join(out_dir, post.source_path))
                     undeployed_posts.append(post)
+                else:
+                    print("Deploy", post, post.is_draft, post.publish_later)
+        else:
+            print("DEPLOY_DRAFTS and DEPLOY_FUTURE are both True")

         if args:
             presets = args
```

I suspect `os.path.join(out_dir, post.destination_path())` is pointing to a wrong path. Can you also print that? 

The source file path is incorrect, and the following patch should help. 

``` diff
diff --git a/nikola/plugins/command/deploy.py b/nikola/plugins/command/deploy.py
index 365c0a3..1e772c9 100644
--- a/nikola/plugins/command/deploy.py
+++ b/nikola/plugins/command/deploy.py
@@ -98,7 +98,8 @@ class CommandDeploy(Command):
                 if (not deploy_drafts and post.is_draft) or \
                    (not deploy_future and post.publish_later):
                     remove_file(os.path.join(out_dir, post.destination_path()))
-                    remove_file(os.path.join(out_dir, post.source_path))
+                    source_path = post.destination_path(extension=post.source_ext(True))
+                    remove_file(os.path.join(out_dir, source_path))
                     undeployed_posts.append(post)

         if args:
```

Output is here https://gist.github.com/anonymous/d8a192cc52ecfeed8011da8c81c472e3

@ChillarAnand could you test @punchagan’s patch and see if it helps in your case?

My bad. Looks like I overlooked the issue. I was deploying to github pages and ghp-import was commiting even drafts to github. Is there a way to disable this?

Were you using `nikola github_deploy`? (It does not handle any of these options.)

Yes, I was using that.


Resolving patch:
diff --git a/nikola/plugins/command/deploy.py b/nikola/plugins/command/deploy.py
--- a/nikola/plugins/command/deploy.py
+++ b/nikola/plugins/command/deploy.py
@@ -38,7 +38,7 @@
 from blinker import signal
 
 from nikola.plugin_categories import Command
-from nikola.utils import get_logger, remove_file, STDERR_HANDLER
+from nikola.utils import get_logger, clean_before_deployment, STDERR_HANDLER
 
 
 class CommandDeploy(Command):
@@ -46,7 +46,7 @@ class CommandDeploy(Command):
 
     name = "deploy"
 
-    doc_usage = "[[preset [preset...]]"
+    doc_usage = "[preset [preset...]]"
     doc_purpose = "deploy the site"
     doc_description = "Deploy the site by executing deploy commands from the presets listed on the command line.  If no presets are specified, `default` is executed."
     logger = None
@@ -84,22 +84,14 @@ def _execute(self, command, args):
                              "You are deploying using the nikolademo Disqus account.\n"
                              "That means you will not be able to moderate the comments in your own site.\n"
                              "And is probably not what you want to do.\n"
-                             "Think about it for 5 seconds, I'll wait :-)\n\n")
+                             "Think about it for 5 seconds, I'll wait :-)\n"
+                             "(press Ctrl+C to abort)\n")
             time.sleep(5)
 
-        deploy_drafts = self.site.config.get('DEPLOY_DRAFTS', True)
-        deploy_future = self.site.config.get('DEPLOY_FUTURE', False)
-        undeployed_posts = []
-        if not (deploy_drafts and deploy_future):
-            # Remove drafts and future posts
-            out_dir = self.site.config['OUTPUT_FOLDER']
-            self.site.scan_posts()
-            for post in self.site.timeline:
-                if (not deploy_drafts and post.is_draft) or \
-                   (not deploy_future and post.publish_later):
-                    remove_file(os.path.join(out_dir, post.destination_path()))
-                    remove_file(os.path.join(out_dir, post.source_path))
-                    undeployed_posts.append(post)
+        # Remove drafts and future posts if requested
+        undeployed_posts = clean_before_deployment(self.site)
+        if undeployed_posts:
+            self.logger.notice("Deleted {0} posts due to DEPLOY_* settings".format(len(undeployed_posts)))
 
         if args:
             presets = args
diff --git a/nikola/plugins/command/github_deploy.py b/nikola/plugins/command/github_deploy.py
--- a/nikola/plugins/command/github_deploy.py
+++ b/nikola/plugins/command/github_deploy.py
@@ -34,7 +34,7 @@
 
 from nikola.plugin_categories import Command
 from nikola.plugins.command.check import real_scan_files
-from nikola.utils import get_logger, req_missing, STDERR_HANDLER
+from nikola.utils import get_logger, req_missing, clean_before_deployment, STDERR_HANDLER
 from nikola.__main__ import main
 from nikola import __version__
 
@@ -100,6 +100,11 @@ def _execute(self, options, args):
         for f in only_on_output:
             os.unlink(f)
 
+        # Remove drafts and future posts if requested (Issue #2406)
+        undeployed_posts = clean_before_deployment(self.site)
+        if undeployed_posts:
+            self.logger.notice("Deleted {0} posts due to DEPLOY_* settings".format(len(undeployed_posts)))
+
         # Commit and push
         self._commit_and_push(options['commit_message'])
 
diff --git a/nikola/utils.py b/nikola/utils.py
--- a/nikola/utils.py
+++ b/nikola/utils.py
@@ -81,7 +81,7 @@
            'adjust_name_for_index_path', 'adjust_name_for_index_link',
            'NikolaPygmentsHTML', 'create_redirect', 'TreeNode',
            'flatten_tree_structure', 'parse_escaped_hierarchical_category_name',
-           'join_hierarchical_category_path', 'indent')
+           'join_hierarchical_category_path', 'clean_before_deployment', 'indent')
 
 # Are you looking for 'generic_rss_renderer'?
 # It's defined in nikola.nikola.Nikola (the site object).
@@ -1867,6 +1867,25 @@ def dns_sd(port, inet6):
         return None
 
 
+def clean_before_deployment(site):
+    """Clean drafts and future posts before deployment."""
+    undeployed_posts = []
+    deploy_drafts = site.config.get('DEPLOY_DRAFTS', True)
+    deploy_future = site.config.get('DEPLOY_FUTURE', False)
+    if not (deploy_drafts and deploy_future):  # == !drafts || !future
+        # Remove drafts and future posts
+        out_dir = site.config['OUTPUT_FOLDER']
+        site.scan_posts()
+        for post in site.timeline:
+            if (not deploy_drafts and post.is_draft) or (not deploy_future and post.publish_later):
+                for lang in post.translated_to:
+                    remove_file(os.path.join(out_dir, post.destination_path(lang)))
+                    source_path = post.destination_path(lang, post.source_ext(True))
+                    remove_file(os.path.join(out_dir, source_path))
+                undeployed_posts.append(post)
+    return undeployed_posts
+
+
 # Stolen from textwrap in Python 3.4.3.
 def indent(text, prefix, predicate=None):
     """Add 'prefix' to the beginning of selected lines in 'text'.


Test patch: