From 8cd97682d0f9c2731a6c36d7957f671229db6a06 Mon Sep 17 00:00:00 2001 From: Muayyad alsadi Date: Fri, 10 Dec 2021 01:01:45 +0200 Subject: [PATCH] FIXES #370: bug-for-bug hanlding of .env --- README.md | 8 +++++++- podman_compose.py | 11 ++++++----- requirements.txt | 2 ++ setup.py | 3 ++- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e147035..7b55e8f 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,13 @@ This project focus on: * rootless * daemon-less process model, we directly execute podman, no running daemon. -This project only depend on `podman` and Python3 and [PyYAML](https://pyyaml.org/). +This project only depend on: + +* `podman` +* Python3 +* [PyYAML](https://pyyaml.org/) +* [python-dotenv](https://pypi.org/project/python-dotenv/) + And it's formed as a single python file script that you can drop into your PATH and run. ## References: diff --git a/podman_compose.py b/podman_compose.py index bb59576..215e7f5 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -32,8 +32,8 @@ except ImportError: # import fnmatch # fnmatch.fnmatchcase(env, "*_HOST") -import json import yaml +from dotenv import dotenv_values __version__ = '1.0.1' @@ -908,6 +908,10 @@ def resolve_extends(services, service_names, environ): new_service = rec_merge({}, from_service, service) services[name] = new_service +def dotenv_to_dict(dotenv_path): + if not os.path.isfile(dotenv_path): + return {} + return dotenv_values(dotenv_path) class PodmanCompose: def __init__(self): @@ -1021,10 +1025,7 @@ class PodmanCompose: dotenv_path = os.path.join(dirname, ".env") self.environ = dict(os.environ) - if os.path.isfile(dotenv_path): - with open(dotenv_path, 'r') as f: - dotenv_ls = [l.strip() for l in f if l.strip() and not l.startswith('#')] - self.environ.update(dict([l.split("=", 1) for l in dotenv_ls if "=" in l])) + self.environ.update(dotenv_to_dict(dotenv_path)) # TODO: should read and respect those env variables # see: https://docs.docker.com/compose/reference/envvars/ # see: https://docs.docker.com/compose/env-file/ diff --git a/requirements.txt b/requirements.txt index 8b537a2..d61cdb7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,5 @@ # process, which may cause wedges in the gate later. pyyaml +python-dotenv + diff --git a/setup.py b/setup.py index 9ffe3d0..5a1921d 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,8 @@ setup( include_package_data=True, license='GPL-2.0-only', install_requires=[ - 'pyyaml' + 'pyyaml', + 'python-dotenv', ], # test_suite='tests', # tests_require=[