Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit fb2a5d9

Browse files
Added heredoc structure to go to Python conversion; updated python tests
1 parent 9cf4508 commit fb2a5d9

File tree

3 files changed

+110
-7
lines changed

3 files changed

+110
-7
lines changed

pylib/main.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ package main
1010
// extern PyObject* PyDockerfile_GoParseError;
1111
// extern PyObject* PyDockerfile_NewCommand(
1212
// PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*,
13-
// PyObject*, PyObject*
13+
// PyObject*, PyObject*, PyObject*
14+
// );
15+
// extern PyObject* PyDockerfile_NewHeredoc(
16+
// PyObject*, PyObject*, PyObject*
1417
// );
1518
import "C"
1619
import (
@@ -65,6 +68,40 @@ func sliceToTuple(strs []string) *C.PyObject {
6568
return ret
6669
}
6770

71+
func heredocsToPy(heredocs []dockerfile.Heredoc) *C.PyObject {
72+
var pyName, pyFileDescriptor, pyContent *C.PyObject
73+
var ret *C.PyObject
74+
decrefAll := func() {
75+
C.Py_DecRef(pyName)
76+
C.Py_DecRef(pyFileDescriptor)
77+
C.Py_DecRef(pyContent)
78+
C.Py_DecRef(ret)
79+
}
80+
81+
ret = C.PyTuple_New(C.Py_ssize_t(len(heredocs)))
82+
for i, heredoc := range heredocs {
83+
pyName := stringToPy(heredoc.Name)
84+
if pyName == nil {
85+
decrefAll()
86+
return nil
87+
}
88+
89+
pyFileDescriptor := C.PyLong_FromLong(C.long(heredoc.FileDescriptor))
90+
91+
pyContent := stringToPyOrNone(heredoc.Content)
92+
if pyContent == nil {
93+
decrefAll()
94+
return nil
95+
}
96+
97+
pyHeredoc := C.PyDockerfile_NewHeredoc(
98+
pyName, pyFileDescriptor, pyContent,
99+
)
100+
C.PyTuple_SetItem(ret, C.Py_ssize_t(i), pyHeredoc)
101+
}
102+
return ret
103+
}
104+
68105
func boolToInt(b bool) int {
69106
if b {
70107
return 1
@@ -74,7 +111,7 @@ func boolToInt(b bool) int {
74111
}
75112

76113
func cmdsToPy(cmds []dockerfile.Command) *C.PyObject {
77-
var pyCmd, pySubCmd, pyJson, pyOriginal, pyStartLine, pyEndLine, pyValue *C.PyObject
114+
var pyCmd, pySubCmd, pyJson, pyOriginal, pyStartLine, pyEndLine, pyValue, pyHeredocs *C.PyObject
78115
var pyFlags *C.PyObject
79116
var ret *C.PyObject
80117
decrefAll := func() {
@@ -86,7 +123,9 @@ func cmdsToPy(cmds []dockerfile.Command) *C.PyObject {
86123
C.Py_DecRef(pyEndLine)
87124
C.Py_DecRef(pyFlags)
88125
C.Py_DecRef(pyValue)
126+
C.Py_DecRef(pyHeredocs)
89127
C.Py_DecRef(ret)
128+
90129
}
91130

92131
ret = C.PyTuple_New(C.Py_ssize_t(len(cmds)))
@@ -126,8 +165,14 @@ func cmdsToPy(cmds []dockerfile.Command) *C.PyObject {
126165
return nil
127166
}
128167

168+
pyHeredocs = heredocsToPy(cmd.Heredocs)
169+
if pyHeredocs == nil {
170+
decrefAll()
171+
return nil
172+
}
173+
129174
pyCmd := C.PyDockerfile_NewCommand(
130-
pyCmd, pySubCmd, pyJson, pyOriginal, pyStartLine, pyEndLine, pyFlags, pyValue,
175+
pyCmd, pySubCmd, pyJson, pyOriginal, pyStartLine, pyEndLine, pyFlags, pyValue, pyHeredocs,
131176
)
132177
C.PyTuple_SetItem(ret, C.Py_ssize_t(i), pyCmd)
133178
}

pylib/support.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,26 @@ PyObject* PyDockerfile_NewCommand(
3030
PyObject* start_line,
3131
PyObject* end_line,
3232
PyObject* flags,
33-
PyObject* value
33+
PyObject* value,
34+
PyObject* heredocs
3435
) {
3536
return PyObject_CallFunction(
36-
PyDockerfile_Command, "OOOOOOOO",
37-
cmd, sub_cmd, json, original, start_line, end_line, flags, value
37+
PyDockerfile_Command, "OOOOOOOOO",
38+
cmd, sub_cmd, json, original, start_line, end_line, flags, value, heredocs
39+
);
40+
}
41+
42+
/* Heredoc namedtuple */
43+
PyObject* PyDockerfile_Heredoc;
44+
45+
PyObject* PyDockerfile_NewHeredoc(
46+
PyObject* name,
47+
PyObject* file_descriptor,
48+
PyObject* content
49+
) {
50+
return PyObject_CallFunction(
51+
PyDockerfile_Heredoc, "OOO",
52+
name, file_descriptor, content
3853
);
3954
}
4055

@@ -53,15 +68,27 @@ static PyObject* _setup_module(PyObject* module) {
5368
PyModule_AddObject(module, "GoParseError", PyDockerfile_GoParseError);
5469

5570
PyObject* collections = PyImport_ImportModule("collections");
71+
5672
PyDockerfile_Command = PyObject_CallMethod(
5773
collections, "namedtuple", "ss",
58-
"Command", "cmd sub_cmd json original start_line end_line flags value"
74+
"Command", "cmd sub_cmd json original start_line end_line flags value heredocs"
5975
);
6076
PyObject_SetAttrString(
6177
PyDockerfile_Command, "__module__",
6278
PyObject_GetAttrString(module, "__name__")
6379
);
6480
PyModule_AddObject(module, "Command", PyDockerfile_Command);
81+
82+
PyDockerfile_Heredoc = PyObject_CallMethod(
83+
collections, "namedtuple", "ss",
84+
"Heredoc", "name file_descriptor content"
85+
);
86+
PyObject_SetAttrString(
87+
PyDockerfile_Heredoc, "__module__",
88+
PyObject_GetAttrString(module, "__name__")
89+
);
90+
PyModule_AddObject(module, "Heredoc", PyDockerfile_Heredoc);
91+
6592
Py_XDECREF(collections);
6693
}
6794
return module;

tests/dockerfile_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,37 @@ def test_parse_string_success():
4242
cmd='FROM', sub_cmd=None, json=False, flags=(),
4343
value=('ubuntu:xenial',),
4444
start_line=1, end_line=1, original='FROM ubuntu:xenial',
45+
heredocs=(),
4546
),
4647
dockerfile.Command(
4748
cmd='RUN', sub_cmd=None, json=False, flags=(),
4849
value=('echo hi > /etc/hi.conf',),
4950
start_line=2, end_line=2, original='RUN echo hi > /etc/hi.conf',
51+
heredocs=(),
5052
),
5153
dockerfile.Command(
5254
cmd='CMD', sub_cmd=None, json=True, flags=(), value=('echo',),
5355
start_line=3, end_line=3, original='CMD ["echo"]',
56+
heredocs=(),
5457
),
5558
dockerfile.Command(
5659
cmd='HEALTHCHECK', sub_cmd=None, json=False,
5760
flags=('--retries=5',), value=('CMD', 'echo hi'),
5861
start_line=4, end_line=4,
5962
original='HEALTHCHECK --retries=5 CMD echo hi',
63+
heredocs=(),
6064
),
6165
dockerfile.Command(
6266
cmd='ONBUILD', sub_cmd='ADD', json=False, flags=(),
6367
value=('foo', 'bar'),
6468
start_line=5, end_line=5, original='ONBUILD ADD foo bar',
69+
heredocs=(),
6570
),
6671
dockerfile.Command(
6772
cmd='ONBUILD', sub_cmd='RUN', json=True, flags=(),
6873
value=('cat', 'bar'),
6974
start_line=6, end_line=6, original='ONBUILD RUN ["cat", "bar"]',
75+
heredocs=(),
7076
),
7177
)
7278

@@ -80,10 +86,12 @@ def test_parse_string_text():
8086
dockerfile.Command(
8187
cmd='FROM', sub_cmd=None, json=False, value=('ubuntu:xenial',),
8288
start_line=1, end_line=1, original='FROM ubuntu:xenial', flags=(),
89+
heredocs=(),
8390
),
8491
dockerfile.Command(
8592
cmd='CMD', sub_cmd=None, json=True, value=('echo', '☃'),
8693
start_line=2, end_line=2, original='CMD ["echo", "☃"]', flags=(),
94+
heredocs=(),
8795
),
8896
)
8997

@@ -95,10 +103,12 @@ def test_parse_file_success():
95103
cmd='FROM', sub_cmd=None, json=False, flags=(),
96104
value=('ubuntu:xenial',),
97105
start_line=1, end_line=1, original='FROM ubuntu:xenial',
106+
heredocs=(),
98107
),
99108
dockerfile.Command(
100109
cmd='CMD', sub_cmd=None, json=True, flags=(), value=('echo', 'hi'),
101110
start_line=2, end_line=2, original='CMD ["echo", "hi"]',
111+
heredocs=(),
102112
),
103113
)
104114

@@ -121,6 +131,15 @@ def test_heredoc_string_success():
121131
'echo "World!" >> /hello\n',
122132
),
123133
start_line=1, end_line=5, original=test_string,
134+
heredocs=(
135+
dockerfile.Heredoc(
136+
name='EOF',
137+
content='source $HOME/.bashrc && echo $HOME\n'
138+
'echo "Hello" >> /hello\n'
139+
'echo "World!" >> /hello\n',
140+
file_descriptor=0,
141+
),
142+
),
124143
),
125144
)
126145

@@ -142,5 +161,17 @@ def test_heredoc_string_multiple_success():
142161
'content 2\n',
143162
),
144163
start_line=1, end_line=5, original=test_string,
164+
heredocs=(
165+
dockerfile.Heredoc(
166+
name='FILE1',
167+
content='content 1\n',
168+
file_descriptor=0,
169+
),
170+
dockerfile.Heredoc(
171+
name='FILE2',
172+
content='content 2\n',
173+
file_descriptor=0,
174+
),
175+
),
145176
),
146177
)

0 commit comments

Comments
 (0)