Navigation Menu

Skip to content

Commit

Permalink
use new state framework for last_deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed Jan 25, 2016
1 parent 27d24b6 commit 9b964c1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
36 changes: 26 additions & 10 deletions nikola/plugins/command/deploy.py
Expand Up @@ -30,6 +30,7 @@
import io
from datetime import datetime
from dateutil.tz import gettz
import dateutil
import os
import subprocess
import time
Expand All @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion nikola/state.py
Expand Up @@ -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.