Last active
December 18, 2015 15:23
-
-
Save bennylope/9eddfcd27094812c4845 to your computer and use it in GitHub Desktop.
Copy the contents of a Foreman compatible .env file to an envdir compatible configuration directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Script that copies contents of .env file to envdir compatible folder | |
:copyright: Ben Lopatin | |
:license: BSD | |
""" | |
import os | |
import sys | |
def env_variables(open_file): | |
""" | |
Generator function that yields tuple of var name and value | |
For a file like: | |
> some command | |
> #OLD_VAR=whatever | |
> DATABASE_URL=postgres://blahblahblah | |
> HELLO=world | |
Result as a list will be: | |
[('DATABASE_URL', 'postgres://blahblahlbah'), | |
('HELLO', 'world')] | |
""" | |
for line in open_file: | |
if line.strip().startswith("#"): | |
continue | |
try: | |
key, value = line.split("=", 1) | |
except ValueError: | |
continue | |
yield key.strip(), value.strip() | |
def main(*args): | |
try: | |
source, target = args | |
except: | |
print("You must pass the source file path and the target directory path as arguments.") | |
sys.exit(9) | |
with open(source, 'r') as env_file: | |
for key, value in env_variables(env_file): | |
file_name = os.path.join(target, key) | |
with open(file_name, 'wb') as env_key: | |
env_key.write(value.strip("'")) | |
print("Successfully updated {0} envdir from {1}".format( | |
target, source)) | |
if __name__ == "__main__": | |
main(*sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment