zulip_tools.py: Add `get_recent_deployments()` function.

This function returns a list of all the deployments directories
which are newer than some threshold number of days including the
`/root/zulip` directory if it exists.
This commit is contained in:
Harshit Bansal 2017-08-18 18:34:00 +00:00 committed by Tim Abbott
parent 931e4752aa
commit 8954605726
1 changed files with 19 additions and 1 deletions

View File

@ -12,7 +12,7 @@ import sys
import time import time
if False: if False:
from typing import Sequence, Text, Any from typing import Sequence, Set, Text, Any
DEPLOYMENTS_DIR = "/home/zulip/deployments" DEPLOYMENTS_DIR = "/home/zulip/deployments"
LOCK_DIR = os.path.join(DEPLOYMENTS_DIR, "lock") LOCK_DIR = os.path.join(DEPLOYMENTS_DIR, "lock")
@ -155,3 +155,21 @@ def get_environment():
if os.environ.get("TRAVIS"): if os.environ.get("TRAVIS"):
return "travis" return "travis"
return "dev" return "dev"
def get_recent_deployments(threshold_days):
# type: (int) -> Set[Text]
# Returns a list of deployments not older than threshold days
# including `/root/zulip` directory if it exists.
recent = set()
threshold_date = datetime.datetime.now() - datetime.timedelta(days=threshold_days)
for dir_name in os.listdir(DEPLOYMENTS_DIR):
try:
date = datetime.datetime.strptime(dir_name, TIMESTAMP_FORMAT)
if date >= threshold_date:
recent.add(os.path.join(DEPLOYMENTS_DIR, dir_name))
except ValueError:
# Always include deployments whose name is not in the format of a timestamp.
recent.add(os.path.join(DEPLOYMENTS_DIR, dir_name))
if os.path.exists("/root/zulip"):
recent.add("/root/zulip")
return recent