Skip to content
Permalink
Browse files

use new state framework for last_deploy

  • Loading branch information
ralsina committed Jan 25, 2016
1 parent 27d24b6 commit 9b964c19a6ae0ae73eb34e0d9766f5edca574d04
Showing with 27 additions and 11 deletions.
  1. +26 −10 nikola/plugins/command/deploy.py
  2. +1 −1 nikola/state.py
@@ -30,6 +30,7 @@
import io
from datetime import datetime
from dateutil.tz import gettz
import dateutil
import os
import subprocess
import time
@@ -52,9 +53,33 @@ class CommandDeploy(Command):

def _execute(self, command, args):
"""Execute the deploy command."""

self.logger = get_logger('deploy', STDERR_HANDLER)
# Get last successful deploy date
timestamp_path = os.path.join(self.site.config['CACHE_FOLDER'], 'lastdeploy')

# Get last-deploy from persistent state
last_deploy = self.site.state.get('last_deploy')
if last_deploy is None:
# If there is a last-deploy saved, move it to the new state persistence thing
# FIXME: remove in Nikola 8
if os.path.isfile(timestamp_path):
try:
with io.open(timestamp_path, 'r', encoding='utf8') as inf:
last_deploy = dateutil.parser.parse(last_deploy)
clean = False
except (IOError, Exception) as e:
self.logger.debug("Problem when reading `{0}`: {1}".format(timestamp_path, e))
last_deploy = datetime(1970, 1, 1)
clean = True
os.unlink(timestamp_path) # Remove because from now on it's in state
else: # Just a default
last_deploy = datetime(1970, 1, 1)
clean = True
else:
last_deploy = dateutil.parser.parse(last_deploy)
clean = False

if self.site.config['COMMENT_SYSTEM_ID'] == 'nikolademo':
self.logger.warn("\nWARNING WARNING WARNING WARNING\n"
"You are deploying using the nikolademo Disqus account.\n"
@@ -102,22 +127,13 @@ def _execute(self, command, args):
return e.returncode

self.logger.info("Successful deployment")
try:
with io.open(timestamp_path, 'r', encoding='utf8') as inf:
last_deploy = datetime.strptime(inf.read().strip(), "%Y-%m-%dT%H:%M:%S.%f")
clean = False
except (IOError, Exception) as e:
self.logger.debug("Problem when reading `{0}`: {1}".format(timestamp_path, e))
last_deploy = datetime(1970, 1, 1)
clean = True

new_deploy = datetime.utcnow()
self._emit_deploy_event(last_deploy, new_deploy, clean, undeployed_posts)

makedirs(self.site.config['CACHE_FOLDER'])
# Store timestamp of successful deployment
with io.open(timestamp_path, 'w+', encoding='utf8') as outf:
outf.write(unicode_str(new_deploy.isoformat()))
self.site.state.set('last_deploy', new_deploy.isoformat())

def _emit_deploy_event(self, last_deploy, new_deploy, clean=False, undeployed=None):
"""Emit events for all timeline entries newer than last deploy.
@@ -80,4 +80,4 @@ def _save(self):
with tempfile.NamedTemporaryFile(dir=dname, delete=False) as outf:
tname = outf.name
json.dump(self._local.data, outf, sort_keys=True, indent=2)
shutil.move(tname, self.path)
shutil.move(tname, self._path)

0 comments on commit 9b964c1

Please sign in to comment.
You can’t perform that action at this time.