From 68f0f0340d7b6420e263cab648ff7de1ea851a0e Mon Sep 17 00:00:00 2001 From: Bryan Bonvallet Date: Mon, 19 May 2014 15:11:57 -0400 Subject: [PATCH] manage commented out variables properly --- export_env_to_heroku.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/export_env_to_heroku.py b/export_env_to_heroku.py index 1268122..6e652bc 100644 --- a/export_env_to_heroku.py +++ b/export_env_to_heroku.py @@ -2,6 +2,7 @@ import subprocess def export_env(filename='.env'): data=['heroku', 'config:set'] + unset=['heroku', 'config:unset'] with open(filename, 'r') as config: for line in config.readlines(): # ignore whitespace padding @@ -9,10 +10,21 @@ def export_env(filename='.env'): tmp = line.split('=') # further ignore whitespace padding that was around the = tmp = map(str.strip, tmp) + if len(tmp[0]) and tmp[0][0] == '#': + # the heroku CLI cannot return if a variable is not yet set + # or if it has been set to the empty string. + # delete commented-out variables to be safe. + unset.append(tmp[0][1:]) # check for nonempty variable and content - if len(tmp) == 2 and len(tmp[0]) and len(tmp[1]): + elif len(tmp) == 2 and len(tmp[0]) and len(tmp[1]): + data.append('{0}={1}'.format(*tmp)) - return subprocess.check_call(data) + # run heroku configuration + subprocess.check_call(data) + subprocess.check_call(unset) + # check_call fires an exception on failure. + # if we're here, both calls succeeded. + return 0 if __name__ == '__main__': import sys -- 2.25.1