From d7aa5d0cfbcc050ede82ba64bafde9e74051e6e9 Mon Sep 17 00:00:00 2001 From: Owen Gaspard Date: Sat, 18 Jun 2022 13:38:50 -0500 Subject: [PATCH 1/5] 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 From 565a8dd58f8db2fa90e98ff819f79fa5eb9a61c0 Mon Sep 17 00:00:00 2001 From: Owen Gaspard Date: Sat, 18 Jun 2022 13:47:46 -0500 Subject: [PATCH 2/5] Fix incorrect call I changed the name of a function but forgot to update it in the main file. It should work now. --- main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 92395b1..bfb3ba3 100755 --- a/main.py +++ b/main.py @@ -6,7 +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 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 @@ -23,7 +23,6 @@ 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/)" @@ -39,7 +38,7 @@ reddit2fa = getenv("REDDIT_2FA") def main(): - checkUpdate() + envUpdate() cleanup() def get_obj(): From c4274d1d149f33df63ce192d13333b2e64f9c4f6 Mon Sep 17 00:00:00 2001 From: owengaspard Date: Sat, 18 Jun 2022 15:57:54 -0500 Subject: [PATCH 3/5] Add OS check, PS script (unfinished) Added a check for your operating system and a PowerShell script which is unfinished at the moment. --- utils/envUpdate.py | 24 ++++++++++++++++++------ utils/envUpdateWin.ps1 | 2 ++ 2 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 utils/envUpdateWin.ps1 diff --git a/utils/envUpdate.py b/utils/envUpdate.py index a95b48f..0b53ab3 100644 --- a/utils/envUpdate.py +++ b/utils/envUpdate.py @@ -1,18 +1,30 @@ import os import subprocess import tempfile -from os import path import logging +from os import path +from sys import platform + 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, - ) + if platform == "win32" or platform == "cygwin": + #envTemplate = subprocess.run( + # [r'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe', + # r'.\envUpdateWin.ps1'], + # stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + # shell=True + #) + print("Updating is not yet supported on Windows.") + return + 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) diff --git a/utils/envUpdateWin.ps1 b/utils/envUpdateWin.ps1 new file mode 100644 index 0000000..a045a4b --- /dev/null +++ b/utils/envUpdateWin.ps1 @@ -0,0 +1,2 @@ +$envVars = Get-Content '.\.env' +$envVars -split "=" \ No newline at end of file From 928e50e08fe5c3e98c9a00bfea68e8835e194b04 Mon Sep 17 00:00:00 2001 From: owengaspard Date: Sat, 18 Jun 2022 16:05:18 -0500 Subject: [PATCH 4/5] Added PowerShell Script --- utils/envUpdate.py | 14 +++++++------- utils/envUpdateWin.ps1 | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/utils/envUpdate.py b/utils/envUpdate.py index 0b53ab3..05b08da 100644 --- a/utils/envUpdate.py +++ b/utils/envUpdate.py @@ -11,13 +11,13 @@ log = logging.getLogger(__name__) def envUpdate(): if path.exists(".env.template"): if platform == "win32" or platform == "cygwin": - #envTemplate = subprocess.run( - # [r'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe', - # r'.\envUpdateWin.ps1'], - # stdout=subprocess.PIPE, stderr=subprocess.STDOUT, - # shell=True - #) - print("Updating is not yet supported on Windows.") + envTemplate = subprocess.run( + [r'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe', + r'.\envUpdateWin.ps1'], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + shell=True + print("Updating .env is unsupported on Windows.") + ) return elif platform == "darwin" or platform == "linux": envTemplate = subprocess.check_output( diff --git a/utils/envUpdateWin.ps1 b/utils/envUpdateWin.ps1 index a045a4b..79c8a04 100644 --- a/utils/envUpdateWin.ps1 +++ b/utils/envUpdateWin.ps1 @@ -1,2 +1,3 @@ -$envVars = Get-Content '.\.env' -$envVars -split "=" \ No newline at end of file +#$envVars = Get-Content '.\.env' +#$envVars -split "=" +Write-Host 'Updating the env is unsupported on Windows.' \ No newline at end of file From ddc402c48d79bbe26cbf93e76f9e3eb401581175 Mon Sep 17 00:00:00 2001 From: owengaspard Date: Sat, 18 Jun 2022 17:16:07 -0500 Subject: [PATCH 5/5] Added Windows support I have added Windows support using PowerShell. I made a script that does effectively what the GNU awk and grep commands do but in PowerShell for Windows compatibility. --- .env.template | 2 +- .gitignore | 3 ++- utils/envUpdate.py | 19 +++++++++---------- utils/envUpdateWin.ps1 | 12 +++++++++--- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/.env.template b/.env.template index 173bacf..c94fa6a 100644 --- a/.env.template +++ b/.env.template @@ -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" \ No newline at end of file diff --git a/.gitignore b/.gitignore index d365d9e..1c28a20 100644 --- a/.gitignore +++ b/.gitignore @@ -170,4 +170,5 @@ reddit-bot-351418-5560ebc49cac.json /.idea *.pyc /video_creation/data/videos.json -utils/envUpdate.py.old \ No newline at end of file +utils/envUpdate.py.old +envVars.txt \ No newline at end of file diff --git a/utils/envUpdate.py b/utils/envUpdate.py index 05b08da..f225419 100644 --- a/utils/envUpdate.py +++ b/utils/envUpdate.py @@ -4,21 +4,16 @@ import tempfile import logging from os import path -from sys import platform +from sys import platform, stderr log = logging.getLogger(__name__) def envUpdate(): if path.exists(".env.template"): if platform == "win32" or platform == "cygwin": - envTemplate = subprocess.run( - [r'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe', - r'.\envUpdateWin.ps1'], - stdout=subprocess.PIPE, stderr=subprocess.STDOUT, - shell=True - print("Updating .env is unsupported on Windows.") - ) - return + 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]", @@ -46,4 +41,8 @@ def envUpdate(): log.error( f"[ERROR] The following environment variables are missing: {missing}.)" ) - exit(-1) \ No newline at end of file + exit(-1) + +def runPS(cmd): + completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True) + return completed \ No newline at end of file diff --git a/utils/envUpdateWin.ps1 b/utils/envUpdateWin.ps1 index 79c8a04..1eb8f64 100644 --- a/utils/envUpdateWin.ps1 +++ b/utils/envUpdateWin.ps1 @@ -1,3 +1,9 @@ -#$envVars = Get-Content '.\.env' -#$envVars -split "=" -Write-Host 'Updating the env is unsupported on Windows.' \ No newline at end of file +$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 \ No newline at end of file