From d7aa5d0cfbcc050ede82ba64bafde9e74051e6e9 Mon Sep 17 00:00:00 2001 From: Owen Gaspard Date: Sat, 18 Jun 2022 13:38:50 -0500 Subject: [PATCH] Check for missing environment variables Checks your .env and matches it to the .env.template to find missing variables. --- .gitignore | 3 ++- main.py | 3 +++ utils/envUpdate.py | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 utils/envUpdate.py diff --git a/.gitignore b/.gitignore index 2701ca5..d365d9e 100644 --- a/.gitignore +++ b/.gitignore @@ -169,4 +169,5 @@ results/* reddit-bot-351418-5560ebc49cac.json /.idea *.pyc -/video_creation/data/videos.json \ No newline at end of file +/video_creation/data/videos.json +utils/envUpdate.py.old \ No newline at end of file diff --git a/main.py b/main.py index b4e1123..92395b1 100755 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ from os import getenv, name from reddit.subreddit import get_subreddit_threads from utils.cleanup import cleanup from utils.console import print_markdown, print_step +from utils.envUpdate import checkUpdate from video_creation.background import download_background, chop_background_video from video_creation.final_video import make_final_video from video_creation.screenshot_downloader import download_screenshots_of_reddit_posts @@ -22,6 +23,7 @@ print( """ ) load_dotenv() +envUpdate() # Modified by JasonLovesDoggo print_markdown( "### Thanks for using this tool! [Feel free to contribute to this project on GitHub!](https://lewismenelaws.com) If you have any questions, feel free to reach out to me on Twitter or submit a GitHub issue. You can find solutions to many common problems in the [Documentation](https://luka-hietala.gitbook.io/documentation-for-the-reddit-bot/)" @@ -37,6 +39,7 @@ reddit2fa = getenv("REDDIT_2FA") def main(): + checkUpdate() cleanup() def get_obj(): diff --git a/utils/envUpdate.py b/utils/envUpdate.py new file mode 100644 index 0000000..a95b48f --- /dev/null +++ b/utils/envUpdate.py @@ -0,0 +1,37 @@ +import os +import subprocess +import tempfile +from os import path +import logging + +log = logging.getLogger(__name__) + + +def envUpdate(): + if path.exists(".env.template"): + envTemplate = subprocess.check_output( + "awk -F '=' 'NF {print $1}' .env.template | grep --regexp=^[a-zA-Z]", # noqa + shell=True, + ) + tempEnv = tempfile.TemporaryFile() + tempEnv.write(envTemplate) + tempEnv.seek(0) + envVars = tempEnv.readlines() + + missing = [] + isMissingEnvs = False + for env in envVars: + try: + env = env.decode("utf-8").strip() + except AttributeError: + env = env.strip() + + if env not in os.environ: + isMissingEnvs = True + missing.append(env) + + if isMissingEnvs: + log.error( + f"[ERROR] The following environment variables are missing: {missing}.)" + ) + exit(-1) \ No newline at end of file