Skip to content

Commit

Permalink
Poetry scanner (#3623)
Browse files Browse the repository at this point in the history
* extend fastapi scanner into poetry scanner

* fix pre-commit issues

* cleanup
  • Loading branch information
fliepeltje committed Jun 12, 2024
1 parent 96b6ebc commit 4fefe6b
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 80 deletions.
60 changes: 0 additions & 60 deletions scanner/fastapi.go

This file was deleted.

17 changes: 0 additions & 17 deletions scanner/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,3 @@ func readTomlFile(file string) (map[string]interface{}, error) {
}
return tomlData, nil
}

func listSuffixedFiles(src string, suffix string) ([]string, error) {
var files []string
err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, suffix) {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
88 changes: 88 additions & 0 deletions scanner/python-poetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package scanner

import (
"bufio"
"os"
"path/filepath"
"strings"
)

func findEntrypoint(dep string) *os.File {
var entrypoint *os.File = nil
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) == ".py" && !strings.Contains(path, ".venv") {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close() // skipcq: GO-S2307

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "import") && strings.Contains(line, dep) {
entrypoint = file
}
}

if err := scanner.Err(); err != nil {
return err
}
}
return nil
})
return entrypoint
}

func configurePoetry(sourceDir string, _ *ScannerConfig) (*SourceInfo, error) {
if !checksPass(sourceDir, fileExists("pyproject.toml", "poetry.lock")) {
return nil, nil
}
pyProject, err := readTomlFile("pyproject.toml")
if err != nil {
return nil, err
}
deps := pyProject["tool"].(map[string]interface{})["poetry"].(map[string]interface{})["dependencies"].(map[string]interface{})

pyVersion := deps["python"].(string)
pyVersion = strings.TrimPrefix(pyVersion, "^")

appName := pyProject["tool"].(map[string]interface{})["poetry"].(map[string]interface{})["name"].(string)
appName = strings.ReplaceAll(appName, "-", "_")

vars := make(map[string]interface{})
vars["pyVersion"] = pyVersion
vars["appName"] = appName

if _, ok := deps["fastapi"]; ok {
return &SourceInfo{
Files: templatesExecute("templates/python-fastapi", vars),
Family: "FastAPI",
Port: 8000,
}, nil
} else if _, ok := deps["flask"]; ok {
return &SourceInfo{
Files: templatesExecute("templates/python-flask-poetry", vars),
Family: "Flask",
Port: 8080,
}, nil
} else if _, ok := deps["streamlit"]; ok {
entrypoint := findEntrypoint("streamlit")
if entrypoint == nil {
return nil, nil
} else {
vars["entrypoint"] = entrypoint.Name()
}
return &SourceInfo{
Files: templatesExecute("templates/python-streamlit", vars),
Family: "Streamlit",
Port: 8501,
}, nil
} else {
return nil, nil
}

}
2 changes: 1 addition & 1 deletion scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Scan(sourceDir string, config *ScannerConfig) (*SourceInfo, error) {
configureRuby,
configureGo,
configureElixir,
configureFastAPI,
configurePoetry,
configureFlask,
configurePython,
configureDeno,
Expand Down
4 changes: 4 additions & 0 deletions scanner/templates/python-fastapi/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fly.toml
.git/
__pycache__/
.envrc
4 changes: 2 additions & 2 deletions scanner/templates/python-fastapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN pip install poetry && poetry config virtualenvs.in-project true
WORKDIR /app

COPY pyproject.toml poetry.lock ./
COPY {{ .appName }}/ ./{{ .appName }}
COPY . .

RUN poetry install

Expand All @@ -22,4 +22,4 @@ COPY --from=builder /app .
# COPY static/ static


CMD ["/app/.venv/bin/fastapi", "run", "{{ .appFile }}"]
CMD ["/app/.venv/bin/fastapi", "run"]
4 changes: 4 additions & 0 deletions scanner/templates/python-flask-poetry/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fly.toml
.git/
__pycache__/
.envrc
25 changes: 25 additions & 0 deletions scanner/templates/python-flask-poetry/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM python:{{ .pyVersion }} AS builder

ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1

RUN pip install poetry && poetry config virtualenvs.in-project true

WORKDIR /app

COPY pyproject.toml poetry.lock ./
COPY . .

RUN poetry install

FROM python:{{ .pyVersion }}-slim

WORKDIR /app

COPY --from=builder /app .
# Copy additional resources for your app here
# COPY templates/ templates
# COPY static/ static


CMD ["/app/.venv/bin/flask", "run", "--host=0.0.0.0", "--port=8080"]
4 changes: 4 additions & 0 deletions scanner/templates/python-streamlit/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fly.toml
.git/
__pycache__/
.envrc
25 changes: 25 additions & 0 deletions scanner/templates/python-streamlit/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM python:{{ .pyVersion }} AS builder

ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1

RUN pip install poetry && poetry config virtualenvs.in-project true

WORKDIR /app

COPY pyproject.toml poetry.lock ./
COPY . .

RUN poetry install

FROM python:{{ .pyVersion }}-slim

WORKDIR /app

COPY --from=builder /app .
# Copy additional resources for your app here
# COPY templates/ templates
# COPY static/ static


CMD ["/app/.venv/bin/streamlit", "run", "{{ .entrypoint }}"]

0 comments on commit 4fefe6b

Please sign in to comment.