Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes for python file entry point detection #217

Merged
merged 11 commits into from
Jul 7, 2023
39 changes: 38 additions & 1 deletion cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/languages"
"github.com/Azure/draft/pkg/linguist"
"github.com/Azure/draft/pkg/reporeader"
"github.com/Azure/draft/pkg/templatewriter/writers"
"github.com/Azure/draft/template"
)
Expand Down Expand Up @@ -53,7 +54,6 @@ func TestRun(t *testing.T) {
assert.False(t, lowerLang == "")
assert.True(t, err == nil)
err = mockCC.generateDockerfile(detectedLang, lowerLang)
println(err)
assert.True(t, err == nil)

//Write back old Dockerfile
Expand Down Expand Up @@ -105,6 +105,43 @@ func TestRun(t *testing.T) {
}
}

func TestRunCreateDockerfileWithRepoReader(t *testing.T) {
mockCC := &createCmd{}
bosesuneha marked this conversation as resolved.
Show resolved Hide resolved
mockCC.repoReader = &reporeader.TestRepoReader{Files: map[string][]byte{
"foo.py": []byte("print('Hello World')"),
"main.py": []byte("print('Hello World')"),
}}
mockCC.createConfig = &CreateConfig{}
mockCC.createConfig.LanguageType = "python"
mockCC.createConfig.LanguageVariables = []UserInputs{}
mockPortInput := UserInputs{Name: "PORT", Value: "8080"}
mockCC.createConfig.LanguageVariables = append(mockCC.createConfig.LanguageVariables, mockPortInput)
mockCC.templateWriter = &writers.LocalFSWriter{}

detectedLang, lowerLang, err := mockCC.mockDetectLanguage()
assert.False(t, detectedLang == nil)
assert.True(t, lowerLang == "python")
assert.Nil(t, err)

err = mockCC.generateDockerfile(detectedLang, lowerLang)
assert.True(t, err == nil)

dockerFileContent, err := ioutil.ReadFile("Dockerfile")
if err != nil {
t.Error(err)
}
assert.Contains(t, string(dockerFileContent), "CMD [\"main.py\"]")

err = os.Remove("Dockerfile")
if err != nil {
t.Error(err)
}
os.RemoveAll(".dockerignore")
if err != nil {
t.Error(err)
}
}

func TestInitConfig(t *testing.T) {
mockCC := &createCmd{}
mockCC.createConfig = &CreateConfig{}
Expand Down
30 changes: 28 additions & 2 deletions pkg/languages/defaults/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package defaults

import (
"fmt"
"path/filepath"
"regexp"

"github.com/Azure/draft/pkg/reporeader"
)
Expand All @@ -16,8 +18,32 @@ func (p PythonExtractor) ReadDefaults(r reporeader.RepoReader) (map[string]strin
if err != nil {
return nil, fmt.Errorf("error finding python files: %v", err)
}
if len(files) > 0 {
extractedValues["ENTRYPOINT"] = files[0]

entryPointPattern := `if\s*__name__\s*==\s*["']__main__["']`
compiledPattern, err := regexp.Compile(entryPointPattern)
if err != nil {
return nil, fmt.Errorf("error compiling regex pattern: %v", err)
}

for _, filePath := range files {
davidgamero marked this conversation as resolved.
Show resolved Hide resolved
fileContent, err := r.ReadFile(filePath)
baseFile := filepath.Base(filePath)

if err != nil {
return nil, fmt.Errorf(("error reading python files"))
}
fileContentInString := string(fileContent)

if compiledPattern.MatchString(fileContentInString) || baseFile == "main.py" || baseFile == "app.py" {
extractedValues["ENTRYPOINT"] = baseFile
break
}
}

if _, ok := extractedValues["ENTRYPOINT"]; !ok {
if len(files) > 0 {
extractedValues["ENTRYPOINT"] = files[0]
}
}

return extractedValues, nil
Expand Down
33 changes: 32 additions & 1 deletion pkg/languages/defaults/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,38 @@ func TestPythonExtractor_ReadDefaults(t *testing.T) {
"ENTRYPOINT": "foo.py",
},
wantErr: false,
}, {
},
{
name: "extract python file containing the string \"__name__=='__main__'\" as the entrypoint",
args: args{
r: reporeader.TestRepoReader{
Files: map[string][]byte{
"foo.py": []byte("print('hello world')"),
"bar.py": []byte("if __name__ == '__main__': print('hello world')"),
bosesuneha marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
want: map[string]string{
"ENTRYPOINT": "bar.py",
},
wantErr: false,
},
{
name: "extract python file named app.py as the entrypoint",
args: args{
r: reporeader.TestRepoReader{
Files: map[string][]byte{
"foo.py": []byte("print('Hello World')"),
"app.py": []byte("print('Hello World')"),
},
},
},
want: map[string]string{
"ENTRYPOINT": "app.py",
},
wantErr: false,
},
{
name: "no extraction if no python files",
args: args{
r: reporeader.TestRepoReader{
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/python/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ RUN pip install --no-cache-dir -r requirements.txt
COPY . .

ENTRYPOINT ["python"]
CMD ["app.py"]
CMD ["{{ENTRYPOINT}}"]
bosesuneha marked this conversation as resolved.
Show resolved Hide resolved