update_dotenv.py 977 B

1234567891011121314151617181920212223242526
  1. import json
  2. from pathlib import Path
  3. # Update the .env file with the answers from the .copier-answers.yml file
  4. # without using Jinja2 templates in the .env file, this way the code works as is
  5. # without needing Copier, but if Copier is used, the .env file will be updated
  6. root_path = Path(__file__).parent.parent
  7. answers_path = Path(__file__).parent / ".copier-answers.yml"
  8. answers = json.loads(answers_path.read_text())
  9. env_path = root_path / ".env"
  10. env_content = env_path.read_text()
  11. lines = []
  12. for line in env_content.splitlines():
  13. for key, value in answers.items():
  14. upper_key = key.upper()
  15. if line.startswith(f"{upper_key}="):
  16. if " " in value:
  17. content = f"{upper_key}={value!r}"
  18. else:
  19. content = f"{upper_key}={value}"
  20. new_line = line.replace(line, content)
  21. lines.append(new_line)
  22. break
  23. else:
  24. lines.append(line)
  25. env_path.write_text("\n".join(lines))