manage commented out variables properly
authorBryan Bonvallet <btbonval@gmail.com>
Mon, 19 May 2014 19:11:57 +0000 (15:11 -0400)
committerBryan Bonvallet <btbonval@gmail.com>
Mon, 19 May 2014 19:11:57 +0000 (15:11 -0400)
export_env_to_heroku.py

index 1268122711ff086e2b11f5207181de84aaab3a10..6e652bc7d5c2f515156220f7b3d5f25ce000d692 100644 (file)
@@ -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