diff --git a/scripts/erynwells_me/deployment.py b/scripts/erynwells_me/deployment.py new file mode 100644 index 0000000..2cc4671 --- /dev/null +++ b/scripts/erynwells_me/deployment.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +# Eryn Wells + +import argparse +import datetime +import subprocess +from typing import List +from .scripting import Command + + +DATE_FORMAT = "%Y-%m-%d" + + +class DeploymentCommand(Command): + @property + def help(self) -> str: + return 'Deployment tools' + + def add_arguments(self, parser: argparse.ArgumentParser): + commands = parser.add_subparsers(title="Deployment", required=True) + + deployment_command = commands.add_parser( + 'next-tag', + help='Calculate the next deployment tag for a given date, examining all other tags for that day' + ) + deployment_command.add_argument( + '--date', '-d', + type=self.__class__.parse_date_argument, + default=datetime.date.today(), + help="date to calculate a new deployment tag for (default: today)" + ) + deployment_command.set_defaults(handler=self.handle_next_deployment_tag_command) + + @staticmethod + def parse_date_argument(date_string: str) -> datetime.date: + return datetime.datetime.strptime(date_string, DATE_FORMAT).date() + + def handle_next_deployment_tag_command(self, args: argparse.Namespace): + next_deployment_tag = deployment_tag_for_date(args.date) + print(next_deployment_tag) + +def deployment_tag_for_date(date: datetime.date) -> str: + formatted_date = date.strftime(DATE_FORMAT) + + def deploy_tag_filter(tag): + return tag.startswith("deploy-") and formatted_date in tag + + filtered_tags = sorted(filter(deploy_tag_filter, git_tags())) + + if len(filtered_tags) == 0: + return f"deploy-{formatted_date}-01" + else: + last_tag = filtered_tags[-1] + components = last_tag.split("-") + if len(components) == 5: + # A previous numbered deployment. Add one to the last deployment + # number. + deployment_number = int(components[-1]) + 1 + return f"deploy-{formatted_date}-{deployment_number:02}" + else: + # An old style deployment tag that doesn't have a number. There + # should only ever be one of these. + assert len(filtered_tags) == 1 + return f"deploy-{formatted_date}-02" + + +def git_tags() -> List[str]: + return subprocess.check_output( + "git tag -l", + shell=True, + encoding="utf-8" + ).splitlines() diff --git a/scripts/erynwells_me/scripting/command.py b/scripts/erynwells_me/scripting/command.py index 8014cb5..3e43e39 100644 --- a/scripts/erynwells_me/scripting/command.py +++ b/scripts/erynwells_me/scripting/command.py @@ -20,5 +20,5 @@ class Command: def help(self) -> str: return '' - def add_arguments(self, _: argparse.ArgumentParser): + def add_arguments(self, parser: argparse.ArgumentParser): raise NotImplementedError() diff --git a/scripts/website b/scripts/website index 958d748..356a985 100755 --- a/scripts/website +++ b/scripts/website @@ -9,10 +9,12 @@ import argparse from typing import List from erynwells_me.scripting import Command from erynwells_me.weeknotes import WeeknotesCommand +from erynwells_me.deployment import DeploymentCommand COMMANDS: List[Command] = [ WeeknotesCommand(), + DeploymentCommand(), ]