-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* extend fastapi scanner into poetry scanner * fix pre-commit issues * cleanup
- Loading branch information
1 parent
96b6ebc
commit 4fefe6b
Showing
10 changed files
with
153 additions
and
80 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fly.toml | ||
.git/ | ||
__pycache__/ | ||
.envrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fly.toml | ||
.git/ | ||
__pycache__/ | ||
.envrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fly.toml | ||
.git/ | ||
__pycache__/ | ||
.envrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}"] |