diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/cli.py | 63 |
1 files changed, 63 insertions, 0 deletions
@@ -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) |