Advertisement
izznogooood

Store/Get .env files

May 4th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. import os
  2. import shutil
  3. import configparser
  4. import argparse
  5. from sys import platform, exit
  6.  
  7. config_file_path = os.path.join(
  8.     os.path.expanduser('~'), '.config', 'getenv.ini'
  9.     )
  10. config = configparser.ConfigParser()
  11. args = None
  12.  
  13.  
  14. def parse_args():
  15.     """initialize CLI arguments"""
  16.     global args
  17.  
  18.     parser = argparse.ArgumentParser(
  19.         description='Copies .env files from "<source_dir>/<current_dir_name>" to current dir.'
  20.     )
  21.     parser.add_argument('-c', '--copy', help='Copy "<current_dir_name>/.env to <source_dir>', action='store_true')
  22.     parser.add_argument('-o', '--override', help='Override <current_dir_name> (where getenv looks for .env).')
  23.     parser.add_argument('-s', '--source', help='<source_dir> Permanantly change source dir.')
  24.     args = parser.parse_args()
  25.     main()
  26.  
  27.  
  28. def main():
  29.     """Main Program"""
  30.     check_config()
  31.     if args.source:
  32.         exit(0)
  33.  
  34.     config.read(config_file_path)
  35.     source = config['SETTINGS']['source']
  36.     project_name = args.override if args.override else os.path.basename(os.getcwd())
  37.  
  38.     if args.copy:
  39.         copy_env_to_source(source, project_name)
  40.         exit(0)
  41.  
  42.     if not os.path.exists(os.path.join(source, project_name, '.env')):
  43.         print(f'No .env file for {project_name} found in: {os.path.join(source, project_name)}')
  44.         exit(1)
  45.  
  46.     shutil.copy2(os.path.join(source, project_name, '.env'), os.path.join(os.getcwd(), '.env'))
  47.  
  48.  
  49. def create_config(source_dir):
  50.     config['SETTINGS'] = {
  51.         'source': source_dir
  52.     }
  53.     with open(config_file_path, 'w') as config_file:
  54.         config.write(config_file)
  55.     print(f'"{source_dir}" configered as source for .env files.')
  56.  
  57.  
  58. def check_config():
  59.     if not os.path.exists(config_file_path) or args.source:
  60.         if args.source:
  61.             create_config(args.source)
  62.         else:
  63.             source_dir = input('Enter full path to env source dir: ')
  64.             create_config(source_dir)
  65.  
  66.  
  67. def copy_env_to_source(source_dir, project_name):
  68.  
  69.     if not os.path.exists(os.path.join(source_dir, project_name)):
  70.         os.mkdir(os.path.join(source_dir, project_name))
  71.  
  72.     if not os.path.exists(os.path.join(os.getcwd(), '.env')):
  73.         print('No .env file found!')
  74.         exit(1)
  75.     shutil.copy2(os.path.join(os.getcwd(), '.env'), os.path.join(source_dir, os.path.join(project_name, '.env')))
  76.  
  77.  
  78. if __name__ == "__main__":
  79.     if platform != 'linux':
  80.         raise OSError('This program only runs on linux!')
  81.     parse_args()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement