Skip to content

Commit b0aa08c

Browse files
committed
Merge branch 'vpetersson-master'
2 parents 05ecc81 + 20a7e86 commit b0aa08c

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,31 @@ 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, v = parse_line
537+
environment[k] = v
538+
else:
539+
raise errors.DockerException(
540+
'Invalid line in environment file {0}:\n{1}'.format(
541+
env_file, line))
542+
543+
return environment
544+
545+
521546
def create_container_config(
522547
version, image, command, hostname=None, user=None, detach=False,
523548
stdin_open=False, tty=False, mem_limit=None, ports=None, environment=None,
@@ -528,6 +553,7 @@ def create_container_config(
528553
):
529554
if isinstance(command, six.string_types):
530555
command = shlex.split(str(command))
556+
531557
if isinstance(environment, dict):
532558
environment = [
533559
six.text_type('{0}={1}').format(k, v)

docker/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version = "1.3.1"
1+
version = "1.4.0-dev"
22
version_info = tuple([int(d) for d in version.split("-")[0].split(".")])

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)