Skip to content

Commit d400717

Browse files
committed
Squashed commit of the following:
commit 4f053a06c1e9e3f63fd5afde60322f676acbdf45 Merge: 9177380 07a99ea Author: Viktor Petersson <[email protected]> Date: Thu Jul 30 14:37:16 2015 +0100 Merge branch 'master' into fixes commit 9177380 Author: Viktor Petersson <[email protected]> Date: Thu Jul 30 14:00:51 2015 +0100 Tweaks exception message. commit 6a5832e Author: Viktor Petersson <[email protected]> Date: Thu Jul 30 13:17:32 2015 +0100 Simplifies logic as per feedback. commit f750edd Author: Viktor Petersson <[email protected]> Date: Thu Jul 30 11:09:14 2015 +0100 Move return from list to dict. Adds exception handling. commit 8e50f57 Author: Viktor Petersson <[email protected]> Date: Thu Jul 30 10:15:58 2015 +0100 Reverts change to .gitignore. commit 5ba2c1b Author: Viktor Petersson <[email protected]> Date: Wed Jul 29 21:15:21 2015 +0100 Fixes feedback. Adds three unittests. commit e1c719e Author: Viktor Petersson <[email protected]> Date: Wed Jul 29 17:00:16 2015 +0100 WIP Adds test for parse_env_file commit 4448ae7 Author: Viktor Petersson <[email protected]> Date: Wed Jul 29 16:42:49 2015 +0100 Excludes coverage files. commit 19a5d01 Author: Viktor Petersson <[email protected]> Date: Wed Jul 29 16:42:42 2015 +0100 Switch fixes logic. commit a8094c6 Author: Viktor Petersson <[email protected]> Date: Wed Jul 29 11:45:56 2015 +0100 Implements logic for envfile parsing from Docker-cli Ref: https://github.com/docker/docker/blob/master/opts/envfile.go#L19-L51 commit ea9bfd9 Author: Viktor Petersson <[email protected]> Date: Wed Jul 29 11:41:23 2015 +0100 Replaces CSV module with manual splitting. commit a001d28 Author: Viktor Petersson <[email protected]> Date: Wed Jul 29 11:35:37 2015 +0100 Removes isinstance on filename. commit 419d596 Author: Viktor Petersson <[email protected]> Date: Tue Jul 28 22:39:33 2015 +0100 Reflects @aanand's feedback. commit e81e3c8 Author: Viktor Petersson <[email protected]> Date: Tue Jul 28 15:43:32 2015 +0100 Typo fix. commit 2898389 Author: Viktor Petersson <[email protected]> Date: Tue Jul 28 15:41:08 2015 +0100 Refs #565. Adds minimal implementation of env_file client-side support.
1 parent 07a99ea commit d400717

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

docker/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
compare_version, convert_port_bindings, convert_volume_binds,
33
mkbuildcontext, tar, parse_repository_tag, parse_host,
44
kwargs_from_env, convert_filters, create_host_config,
5-
create_container_config, parse_bytes, ping_registry
5+
create_container_config, parse_bytes, ping_registry, parse_env_file
66
) # flake8: noqa
77

88
from .types import Ulimit, LogConfig # flake8: noqa

docker/utils/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,32 @@ def create_host_config(
518518
return host_config
519519

520520

521+
def parse_env_file(env_file):
522+
"""
523+
Reads a line-separated environment file.
524+
The format of each line should be "key=value".
525+
"""
526+
environment = {}
527+
528+
with open(env_file, 'r') as f:
529+
for line in f:
530+
531+
if line[0] == '#':
532+
continue
533+
534+
parse_line = line.strip().split('=')
535+
if len(parse_line) == 2:
536+
k = parse_line[0]
537+
v = parse_line[1]
538+
environment[k] = v
539+
else:
540+
raise errors.DockerException(
541+
'Invalid line in environment file {0}:\n{1}'.format(
542+
env_file, line))
543+
544+
return environment
545+
546+
521547
def create_container_config(
522548
version, image, command, hostname=None, user=None, detach=False,
523549
stdin_open=False, tty=False, mem_limit=None, ports=None, environment=None,
@@ -528,6 +554,7 @@ def create_container_config(
528554
):
529555
if isinstance(command, six.string_types):
530556
command = shlex.split(str(command))
557+
531558
if isinstance(environment, dict):
532559
environment = [
533560
six.text_type('{0}={1}').format(k, v)

docs/api.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,27 @@ from. Optionally a single string joining container id's with commas
234234
'Warnings': None}
235235
```
236236

237+
### parse_env_file
238+
239+
A utility for parsing an environment file.
240+
241+
The expected format of the file is as follows:
242+
243+
```
244+
USERNAME=jdoe
245+
PASSWORD=secret
246+
```
247+
248+
The utility can be used as follows:
249+
250+
```python
251+
>> import docker.utils
252+
>> my_envs = docker.utils.parse_env_file('/path/to/file')
253+
>> docker.utils.create_container_config('1.18', '_mongodb', 'foobar', environment=my_envs)
254+
```
255+
256+
You can now use this with 'environment' for `create_container`.
257+
237258
## diff
238259

239260
Inspect changes on a container's filesystem

tests/utils_test.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import os
22
import os.path
33
import unittest
4+
import tempfile
45

56
from docker.client import Client
67
from docker.errors import DockerException
78
from docker.utils import (
89
parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
9-
create_host_config, Ulimit, LogConfig, parse_bytes
10+
create_host_config, Ulimit, LogConfig, parse_bytes, parse_env_file
1011
)
1112
from docker.utils.ports import build_port_bindings, split_port
1213
from docker.auth import resolve_repository_name, resolve_authconfig
@@ -17,6 +18,17 @@
1718
class UtilsTest(base.BaseTestCase):
1819
longMessage = True
1920

21+
def generate_tempfile(self, file_content=None):
22+
"""
23+
Generates a temporary file for tests with the content
24+
of 'file_content' and returns the filename.
25+
Don't forget to unlink the file with os.unlink() after.
26+
"""
27+
local_tempfile = tempfile.NamedTemporaryFile(delete=False)
28+
local_tempfile.write(file_content.encode('UTF-8'))
29+
local_tempfile.close()
30+
return local_tempfile.name
31+
2032
def setUp(self):
2133
self.os_environ = os.environ.copy()
2234

@@ -95,6 +107,28 @@ def test_kwargs_from_env(self):
95107
except TypeError as e:
96108
self.fail(e)
97109

110+
def test_parse_env_file_proper(self):
111+
env_file = self.generate_tempfile(
112+
file_content='USER=jdoe\nPASS=secret')
113+
get_parse_env_file = parse_env_file(env_file)
114+
self.assertEqual(get_parse_env_file,
115+
{'USER': 'jdoe', 'PASS': 'secret'})
116+
os.unlink(env_file)
117+
118+
def test_parse_env_file_commented_line(self):
119+
env_file = self.generate_tempfile(
120+
file_content='USER=jdoe\n#PASS=secret')
121+
get_parse_env_file = parse_env_file((env_file))
122+
self.assertEqual(get_parse_env_file, {'USER': 'jdoe'})
123+
os.unlink(env_file)
124+
125+
def test_parse_env_file_invalid_line(self):
126+
env_file = self.generate_tempfile(
127+
file_content='USER jdoe')
128+
self.assertRaises(
129+
DockerException, parse_env_file, env_file)
130+
os.unlink(env_file)
131+
98132
def test_convert_filters(self):
99133
tests = [
100134
({'dangling': True}, '{"dangling": ["true"]}'),

0 commit comments

Comments
 (0)