Last active
May 10, 2017 18:56
-
-
Save smokinggoats/e66726c2d54c49f1f57e489a6068ee86 to your computer and use it in GitHub Desktop.
A Gist to migrate from envdir to .env ( like Python Decouple )
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
from glob import glob | |
import os | |
import sys | |
import argparse | |
class AbsPathAction(argparse.Action): | |
def __init__(self, option_strings, dest, nargs=None, **kwargs): | |
super().__init__(option_strings, dest, **kwargs) | |
def __call__(self, parser, namespace, values, option_string=None): | |
setattr(namespace, self.dest, self.abspath(values)) | |
@classmethod | |
def abspath(cls, path): | |
return os.path.abspath(path) if path[0] != '/' else path | |
parser = argparse.ArgumentParser(description="Migrations and stuff...") | |
parser.add_argument('path', type=str, help='the envidir path (abs or relative)', action=AbsPathAction) | |
parser.add_argument('-d', '--destination', type=str, help='the path where the .env file should be created.', dest='final_path', action=AbsPathAction) | |
def main(path, final_path): | |
files = glob(os.path.join(path, '*')) | |
with open(os.path.join(final_path, '.env'), 'w') as env: | |
for f in files: | |
with open(f, 'r') as file: | |
content = ''.join(file.readlines()) | |
name = f.rsplit('/', 1)[1] | |
file.close() | |
env.write("{}={}\n".format(name, content)) | |
env.close() | |
def create_tests(path): | |
for i in range(10): | |
with open(os.path.join(path, 'ENV-{}'.format(i)), 'w') as file: | |
file.write("AQUI TEM UM VAL ENV: {}".format(i)) | |
file.close() | |
if __name__ == '__main__': | |
args = parser.parse_args() | |
if not args.final_path: | |
args.final_path = AbsPathAction.abspath(args.path) | |
main(args.path, args.final_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment