Merge pull request #553 from owengaspard/env-update
Check for missing environment variables
This commit is contained in:
5 files changed
+63
-2
No files matched your search
+1
-1
@@ -36,4 +36,4 @@ VOICE="Matthew" # e.g. en_us_002
|
||||
TTsChoice="polly" # todo add docs
|
||||
|
||||
# IN-PROGRESS - not yet implemented
|
||||
STORYMODE="False"
|
||||
STORYMODE="False"
|
||||
+3
-1
@@ -169,4 +169,6 @@ results/*
|
||||
reddit-bot-351418-5560ebc49cac.json
|
||||
/.idea
|
||||
*.pyc
|
||||
/video_creation/data/videos.json
|
||||
/video_creation/data/videos.json
|
||||
utils/envUpdate.py.old
|
||||
envVars.txt
|
||||
@@ -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 envUpdate
|
||||
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
|
||||
@@ -37,6 +38,7 @@ reddit2fa = getenv("REDDIT_2FA")
|
||||
|
||||
|
||||
def main():
|
||||
envUpdate()
|
||||
cleanup()
|
||||
|
||||
def get_obj():
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import logging
|
||||
|
||||
from os import path
|
||||
from sys import platform, stderr
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
def envUpdate():
|
||||
if path.exists(".env.template"):
|
||||
if platform == "win32" or platform == "cygwin":
|
||||
runPS('utils\envUpdateWin.ps1')
|
||||
f = open("envVars.txt", "rb")
|
||||
envTemplate = f.read()
|
||||
elif platform == "darwin" or platform == "linux":
|
||||
envTemplate = subprocess.check_output(
|
||||
"awk -F '=' 'NF {print $1}' .env.template | grep --regexp=^[a-zA-Z]",
|
||||
shell=True,
|
||||
)
|
||||
return
|
||||
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)
|
||||
|
||||
def runPS(cmd):
|
||||
completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True)
|
||||
return completed
|
||||
@@ -0,0 +1,9 @@
|
||||
$envFile = Get-Content ".\.env.template"
|
||||
|
||||
$envFile -split "=" | Where-Object {$_ -notmatch '\"'} | Set-Content ".\envVarsbefSpl.txt"
|
||||
Get-Content ".\envVarsbefSpl.txt" | Where-Object {$_ -notmatch '\#'} | Set-Content ".\envVarsN.txt"
|
||||
Get-Content ".\envVarsN.txt" | Where-Object {$_ -ne ''} | Set-Content ".\envVars.txt"
|
||||
Remove-Item ".\envVarsbefSpl.txt"
|
||||
Remove-Item ".\envVarsN.txt"
|
||||
|
||||
Write-Host $nowSplit
|
||||
Reference in new issue
Block a user