From a23bf5af6e7be3fcea03e4b81f4d54a2c916e087 Mon Sep 17 00:00:00 2001 From: neodarz Date: Sun, 26 May 2019 17:59:42 +0200 Subject: Move gen_deploy to external file --- cli/cli.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'cli') diff --git a/cli/cli.py b/cli/cli.py index 5a386ed6..7a5c4bd0 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -9,6 +9,8 @@ import re import io import fileinput +import time + from config.config import * from utils import utils @@ -256,3 +258,64 @@ def deploy(args): sys.stderr.write("\n%serror: git push failed%s\n" % (RED, RESET)) return 1 return 0 + + +def gen_deploy(args): + """Regenerate and deploy.""" + # pylint: disable=unused-argument,too-many-branches + + # try to smartly determine the latest post, and prompt to touch it + current_time = time.time() + latest_post = None + latest_postdate = 0 + latest_mtime = 0 + for name in os.listdir(POSTSDIR): + matchobj = re.match(r"^([0-9]{4})-([0-9]{2})-([0-9]{2})-.*\.md", name) + if not matchobj: + continue + fullpath = os.path.join(POSTSDIR, name) + mtime = os.path.getmtime(fullpath) + # get post date from the date metadata field of the post + postdate = 0 + with open(fullpath) as postobj: + for line in postobj: + dateregex = r"^date: (\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}-\d{2}:?\d{2})" + datematch = re.match(dateregex, line.rstrip()) + if datematch: + postdate = dateutil.parser.parse(datematch.group(1)).timestamp() + break + # skip the post if it is dated more than three days ago + if current_time - postdate > 3 * 24 * 3600: + continue + if mtime > latest_mtime: + latest_post = name + latest_postdate = postdate + latest_mtime = mtime + # prompt for touching if the latest post determined above was + # modified within the last hour but the date registered in the post + # isn't within the last ten minutes + if ((latest_post is not None and current_time - latest_mtime < 3600 and + current_time - latest_postdate > 600)): + sys.stderr.write("%sIt appears that %s might be a new post.\n" + "Do you want to touch its timestamp?%s\n" % + (GREEN, latest_post, RESET)) + while True: + yesnoquit = input("[ynq]: ") + if yesnoquit.startswith(("Y", "y")): + yesno = True + break + elif yesnoquit.startswith(("N", "n")): + yesno = False + break + elif yesnoquit.startswith(("Q", "q")): + sys.stderr.write("%saborting gen_deploy%s\n" % (RED, RESET)) + return 1 + else: + sys.stderr.write("Please answer yes, no, or quit.\n") + if yesno: + sys.stderr.write("%stouching %s%s\n" % (BLUE, latest_post, RESET)) + touch(latest_post) + sys.stderr.write("\n") + + generators.generate_blog(fresh=True) + deploy(None) -- cgit v1.2.1