containerbuildsystem/dockerfile-parse

Incorrectly appending 'Dockerfile' to path

Closed this issue · 3 comments

One can provide a file not called Dockerfile to docker build like this: docker build -f Dockerfile.debug .. When instantiating with a file that isn't named Dockerfile dockerfile-parse will append Dockerfile to the path and fail.

>>> test = DockerfileParser('/home/nisha/test_dockerfile')
>>> test.structure
Couldn't retrieve lines from dockerfile: NotADirectoryError(20, 'Not a directory')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nisha/scancodeenv/lib/python3.6/site-packages/dockerfile_parse/parser.py", line 250, in structure
    for line in self.lines:
  File "/home/nisha/scancodeenv/lib/python3.6/site-packages/dockerfile_parse/parser.py", line 143, in lines
    with self._open_dockerfile('rb') as dockerfile:
  File "/usr/lib/python3.6/contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "/home/nisha/scancodeenv/lib/python3.6/site-packages/dockerfile_parse/parser.py", line 131, in _open_dockerfile
    with open(self.dockerfile_path, mode) as dockerfile:
NotADirectoryError: [Errno 20] Not a directory: '/home/nisha/test_dockerfile/Dockerfile'
>>> import os
>>> os.path.exists('/home/nisha/test_dockerfile') <-- the file exists
True

👋 I don't see any contributing guide, but I can submit a PR if you think this is a valid bug :)

With custom named Dockerfile can you open it at your own and pass fileobject as fileobj param instead of passing path?

Docstring declares properly that path is path to dir containing Dockerfile, so it behaves as documented.

With custom named Dockerfile can you open it at your own and pass fileobject as fileobj param instead of passing path?

Yes, that does work in this way:

>>> with open('/home/nisha/test_dockerfile') as f:
...     test = DockerfileParser(fileobj=f) <-- default is path
...     structure = test.structure

Docstring declares properly that path is path to dir containing Dockerfile, so it behaves as documented.

It would be nice to have the above description of how to use it with an external file object though. Thanks so much!