Skip to content

Commit 8cd9768

Browse files
FIXES containers#370: bug-for-bug hanlding of .env
1 parent 8524427 commit 8cd9768

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ This project focus on:
66
* rootless
77
* daemon-less process model, we directly execute podman, no running daemon.
88

9-
This project only depend on `podman` and Python3 and [PyYAML](https://pyyaml.org/).
9+
This project only depend on:
10+
11+
* `podman`
12+
* Python3
13+
* [PyYAML](https://pyyaml.org/)
14+
* [python-dotenv](https://pypi.org/project/python-dotenv/)
15+
1016
And it's formed as a single python file script that you can drop into your PATH and run.
1117

1218
## References:

podman_compose.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
# import fnmatch
3333
# fnmatch.fnmatchcase(env, "*_HOST")
3434

35-
import json
3635
import yaml
36+
from dotenv import dotenv_values
3737

3838
__version__ = '1.0.1'
3939

@@ -908,6 +908,10 @@ def resolve_extends(services, service_names, environ):
908908
new_service = rec_merge({}, from_service, service)
909909
services[name] = new_service
910910

911+
def dotenv_to_dict(dotenv_path):
912+
if not os.path.isfile(dotenv_path):
913+
return {}
914+
return dotenv_values(dotenv_path)
911915

912916
class PodmanCompose:
913917
def __init__(self):
@@ -1021,10 +1025,7 @@ def _parse_compose_file(self):
10211025

10221026
dotenv_path = os.path.join(dirname, ".env")
10231027
self.environ = dict(os.environ)
1024-
if os.path.isfile(dotenv_path):
1025-
with open(dotenv_path, 'r') as f:
1026-
dotenv_ls = [l.strip() for l in f if l.strip() and not l.startswith('#')]
1027-
self.environ.update(dict([l.split("=", 1) for l in dotenv_ls if "=" in l]))
1028+
self.environ.update(dotenv_to_dict(dotenv_path))
10281029
# TODO: should read and respect those env variables
10291030
# see: https://docs.docker.com/compose/reference/envvars/
10301031
# see: https://docs.docker.com/compose/env-file/

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
# process, which may cause wedges in the gate later.
44

55
pyyaml
6+
python-dotenv
7+

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
include_package_data=True,
3737
license='GPL-2.0-only',
3838
install_requires=[
39-
'pyyaml'
39+
'pyyaml',
40+
'python-dotenv',
4041
],
4142
# test_suite='tests',
4243
# tests_require=[

0 commit comments

Comments
 (0)