cookiecutter-django/{{cookiecutter.project_slug}}/merge_production_dotenvs_in_dotenv.py
Nikita Shupeyko 3f8aa26d0f
Group environment variables by the corresponding directories (#1295)
* Update generated project's .gitignore

* Post-gen gitignore .env/ and .env

* Fix linesep between gitignored entries

* Persist `.env/**/*` files into cookiecutter-django's VCS

* Rename .env/ to .envs/

* Reference the newly created .envs/**/.* files in local.yml

* Reference the newly created .envs/**/.* files in production.yml

* Delete .env.example

* Refactor post-gen-project.py

Closes #1299.

* Implement production-dotenv-files-to-dotenv-file merge script

* Create shared PyCharm Run Configuration for the automation script

* Randomize POSTGRES_PASSWORD in ./envs/(.local|.production)/.postgres

* Default POSTGRES_PASSWORD and POSTGRES_USER to random values

* Fix jinja linebreaks in local.yml

* Spaces in production.yml

* Fix post-merge leftovers & set DJANGO_ADMIN_URL automatically

* Prettify here and there

* Fix FileNotFoundError

* Leave a TODO in post_gen_hook.py

* Introduce keep_local_envs_in_vcs option

* Remove envs when not opted for

* Inline pre_gen_project.py if-condition

* Get rid of PROJECT_DIR_PATH in post_gen_project.py

* Clean up the docs

* Match copyright notices

* Document envs ins and outs
2018-03-08 15:56:15 +03:00

70 lines
2.2 KiB
Python

import os
from typing import Sequence
import pytest
ROOT_DIR_PATH = os.path.dirname(os.path.realpath(__file__))
PRODUCTION_DOTENVS_DIR_PATH = os.path.join(ROOT_DIR_PATH, '.envs', '.production')
PRODUCTION_DOTENV_FILE_PATHS = [
os.path.join(PRODUCTION_DOTENVS_DIR_PATH, '.django'),
os.path.join(PRODUCTION_DOTENVS_DIR_PATH, '.postgres'),
os.path.join(PRODUCTION_DOTENVS_DIR_PATH, '.caddy'),
]
DOTENV_FILE_PATH = os.path.join(ROOT_DIR_PATH, '.env')
def merge(output_file_path: str,
merged_file_paths: Sequence[str],
append_linesep: bool = True) -> None:
with open(output_file_path, 'w') as output_file:
for merged_file_path in merged_file_paths:
with open(merged_file_path, 'r') as merged_file:
merged_file_content = merged_file.read()
output_file.write(merged_file_content)
if append_linesep:
output_file.write(os.linesep)
def main():
merge(DOTENV_FILE_PATH, PRODUCTION_DOTENV_FILE_PATHS)
@pytest.mark.parametrize('merged_file_count', range(3))
@pytest.mark.parametrize('append_linesep', [True, False])
def test_merge(tmpdir_factory,
merged_file_count: int,
append_linesep: bool):
tmp_dir_path = str(tmpdir_factory.getbasetemp())
output_file_path = os.path.join(tmp_dir_path, '.env')
expected_output_file_content = ''
merged_file_paths = []
for i in range(merged_file_count):
merged_file_ord = i + 1
merged_filename = '.service{}'.format(merged_file_ord)
merged_file_path = os.path.join(tmp_dir_path, merged_filename)
merged_file_content = merged_filename * merged_file_ord
with open(merged_file_path, 'w+') as file:
file.write(merged_file_content)
expected_output_file_content += merged_file_content
if append_linesep:
expected_output_file_content += os.linesep
merged_file_paths.append(merged_file_path)
merge(output_file_path, merged_file_paths, append_linesep)
with open(output_file_path, 'r') as output_file:
actual_output_file_content = output_file.read()
assert actual_output_file_content == expected_output_file_content
if __name__ == '__main__':
main()