Erste Fassung eines einfache Projektwerkzeugs
This commit is contained in:
@@ -0,0 +1,557 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
mkpyproject.py - Standard Python Projekt Template erstellen
|
||||
|
||||
Verwendung:
|
||||
python mkpyproject.py <projektname> [zielverzeichnis]
|
||||
|
||||
Beispiel:
|
||||
python mkpyproject.py myapp
|
||||
python mkpyproject.py myapp C:\\10-Develop\\gitrepos
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import stat
|
||||
import argparse
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
# =====================================================================
|
||||
# Datei-Templates
|
||||
# =====================================================================
|
||||
|
||||
def tpl_setenv_bat(name_upper: str) -> str:
|
||||
return f"""@echo off
|
||||
|
||||
REM ================================================================
|
||||
REM {name_upper} - Umgebungsvariablen Setup
|
||||
REM ================================================================
|
||||
|
||||
echo Setting up environment variables for {name_upper} ...
|
||||
|
||||
REM Basis-Projektpfad (aktueller Ordner)
|
||||
set "PROJECT=%~dp0.."
|
||||
if "%PROJECT:~-6%"=="bin\\.." set "PROJECT=%PROJECT:~0,-6%"
|
||||
if "%PROJECT:~-1%"=="\\" set "PROJECT=%PROJECT:~0,-1%"
|
||||
|
||||
REM Pfade fuer verschiedene Komponenten
|
||||
set "PV_BIN=%PROJECT%\\bin"
|
||||
set "PV_LIB=%PROJECT%\\lib"
|
||||
set "PV_DATA=%PROJECT%\\data"
|
||||
set "PV_CFG=%PROJECT%\\cfg"
|
||||
set "PV_LOG=%PROJECT%\\log"
|
||||
set "PV_TESTS=%PROJECT%\\tests"
|
||||
set "PV_RESULTS=%PROJECT%\\results"
|
||||
set "PV_EXAMPLES=%PROJECT%\\examples"
|
||||
|
||||
REM Python-Pfad erweitern (nur wenn noch nicht vorhanden)
|
||||
echo %PYTHONPATH% | find /i "%PV_LIB%" >nul
|
||||
if errorlevel 1 (
|
||||
set "PYTHONPATH=%PV_LIB%;%PYTHONPATH%"
|
||||
)
|
||||
|
||||
REM Ordner erstellen falls sie nicht existieren
|
||||
if not exist "%PV_BIN%" mkdir "%PV_BIN%"
|
||||
if not exist "%PV_CFG%" mkdir "%PV_CFG%"
|
||||
if not exist "%PV_LIB%" mkdir "%PV_LIB%"
|
||||
if not exist "%PV_DATA%" mkdir "%PV_DATA%"
|
||||
if not exist "%PV_LOG%" mkdir "%PV_LOG%"
|
||||
if not exist "%PV_RESULTS%" mkdir "%PV_RESULTS%"
|
||||
if not exist "%PV_EXAMPLES%" mkdir "%PV_EXAMPLES%"
|
||||
|
||||
REM Umgebungsvariablen anzeigen
|
||||
echo.
|
||||
echo ================================================================
|
||||
echo {name_upper} ENVIRONMENT SETUP COMPLETE
|
||||
echo ================================================================
|
||||
echo PROJECT = %PROJECT%
|
||||
echo PV_BIN = %PV_BIN%
|
||||
echo PV_CFG = %PV_CFG%
|
||||
echo PV_LIB = %PV_LIB%
|
||||
echo PV_DATA = %PV_DATA%
|
||||
echo PV_RESULTS = %PV_RESULTS%
|
||||
echo PV_LOG = %PV_LOG%
|
||||
echo PV_EXAMPLES = %PV_EXAMPLES%
|
||||
echo PYTHONPATH = %PYTHONPATH%
|
||||
echo ================================================================
|
||||
echo.
|
||||
|
||||
REM Optionally keep window open
|
||||
if "%1"=="--keep-open" pause
|
||||
"""
|
||||
|
||||
|
||||
def tpl_setenv_sh(name_upper: str) -> str:
|
||||
return f"""#!/usr/bin/env bash
|
||||
# ================================================================
|
||||
# {name_upper} - Umgebungsvariablen Setup
|
||||
# ================================================================
|
||||
# Dieses Skript muss gesourct werden: source bin/setenv.sh
|
||||
# ================================================================
|
||||
|
||||
echo "Setting up environment variables for {name_upper} ..."
|
||||
|
||||
# Basis-Projektpfad (uebergeordnetes Verzeichnis von bin/)
|
||||
SCRIPT_DIR="$(cd "$(dirname "${{BASH_SOURCE[0]}}")" && pwd)"
|
||||
export PROJECT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
|
||||
# Pfade fuer verschiedene Komponenten
|
||||
export PV_BIN="$PROJECT/bin"
|
||||
export PV_LIB="$PROJECT/lib"
|
||||
export PV_DATA="$PROJECT/data"
|
||||
export PV_CFG="$PROJECT/cfg"
|
||||
export PV_LOG="$PROJECT/log"
|
||||
export PV_TESTS="$PROJECT/tests"
|
||||
export PV_RESULTS="$PROJECT/results"
|
||||
export PV_EXAMPLES="$PROJECT/examples"
|
||||
|
||||
# Python-Pfad erweitern (nur wenn noch nicht vorhanden)
|
||||
if [[ ":$PYTHONPATH:" != *":$PV_LIB:"* ]]; then
|
||||
export PYTHONPATH="$PV_LIB:$PYTHONPATH"
|
||||
fi
|
||||
|
||||
# Ordner erstellen falls sie nicht existieren
|
||||
mkdir -p "$PV_BIN" "$PV_CFG" "$PV_LIB" "$PV_DATA" "$PV_LOG" "$PV_RESULTS" "$PV_EXAMPLES"
|
||||
|
||||
echo ""
|
||||
echo "================================================================"
|
||||
echo "{name_upper} ENVIRONMENT SETUP COMPLETE"
|
||||
echo "================================================================"
|
||||
echo "PROJECT = $PROJECT"
|
||||
echo "PV_BIN = $PV_BIN"
|
||||
echo "PV_CFG = $PV_CFG"
|
||||
echo "PV_LIB = $PV_LIB"
|
||||
echo "PV_DATA = $PV_DATA"
|
||||
echo "PV_RESULTS = $PV_RESULTS"
|
||||
echo "PV_LOG = $PV_LOG"
|
||||
echo "PV_EXAMPLES = $PV_EXAMPLES"
|
||||
echo "PYTHONPATH = $PYTHONPATH"
|
||||
echo "================================================================"
|
||||
echo ""
|
||||
"""
|
||||
|
||||
|
||||
def tpl_install_py_bat(name_upper: str) -> str:
|
||||
return f"""@echo off
|
||||
REM ================================================================
|
||||
REM {name_upper} - Python Virtual Environment einrichten
|
||||
REM ================================================================
|
||||
|
||||
call "%~dp0setenv.bat"
|
||||
|
||||
if not exist "%PROJECT%\\.venv" (
|
||||
echo Initialisiere Python virtual environment...
|
||||
py -m venv "%PROJECT%\\.venv" --upgrade-deps
|
||||
echo Erfolgreich.
|
||||
|
||||
call "%PROJECT%\\.venv\\Scripts\\activate.bat"
|
||||
echo Installiere erforderliche Python Packages...
|
||||
pip install -r "%PROJECT%\\requirements.txt" -q
|
||||
echo Erfolgreich.
|
||||
deactivate
|
||||
) else (
|
||||
echo Erforderliche Python Packages bereits installiert!
|
||||
)
|
||||
"""
|
||||
|
||||
|
||||
def tpl_install_py_sh(name_upper: str) -> str:
|
||||
return f"""#!/usr/bin/env bash
|
||||
# ================================================================
|
||||
# {name_upper} - Python Virtual Environment einrichten
|
||||
# ================================================================
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${{BASH_SOURCE[0]}}")" && pwd)"
|
||||
source "$SCRIPT_DIR/setenv.sh"
|
||||
|
||||
if [ ! -d "$PROJECT/.venv" ]; then
|
||||
echo "Initialisiere Python virtual environment..."
|
||||
python3 -m venv "$PROJECT/.venv" --upgrade-deps
|
||||
echo "Erfolgreich."
|
||||
|
||||
source "$PROJECT/.venv/bin/activate"
|
||||
echo "Installiere erforderliche Python Packages..."
|
||||
pip install -r "$PROJECT/requirements.txt" -q
|
||||
echo "Erfolgreich."
|
||||
deactivate
|
||||
else
|
||||
echo "Erforderliche Python Packages bereits installiert!"
|
||||
fi
|
||||
"""
|
||||
|
||||
|
||||
def tpl_activate_venv_bat(name_upper: str) -> str:
|
||||
return f"""@echo off
|
||||
REM ================================================================
|
||||
REM {name_upper} - Python Virtual Environment aktivieren
|
||||
REM ================================================================
|
||||
|
||||
call "%~dp0setenv.bat"
|
||||
|
||||
if not exist "%PROJECT%\\.venv" (
|
||||
echo FEHLER: Virtual environment nicht gefunden.
|
||||
echo Bitte zuerst bin\\install_py.bat ausfuehren.
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
call "%PROJECT%\\.venv\\Scripts\\activate.bat"
|
||||
echo Virtuelle Umgebung aktiviert.
|
||||
echo Python-Version:
|
||||
python --version
|
||||
echo.
|
||||
echo Installierte Pakete:
|
||||
pip list
|
||||
"""
|
||||
|
||||
|
||||
def tpl_activate_venv_sh(name_upper: str) -> str:
|
||||
return f"""#!/usr/bin/env bash
|
||||
# ================================================================
|
||||
# {name_upper} - Python Virtual Environment aktivieren
|
||||
# ================================================================
|
||||
# Dieses Skript muss gesourct werden: source bin/activate_venv.sh
|
||||
# ================================================================
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${{BASH_SOURCE[0]}}")" && pwd)"
|
||||
source "$SCRIPT_DIR/setenv.sh"
|
||||
|
||||
if [ ! -d "$PROJECT/.venv" ]; then
|
||||
echo "FEHLER: Virtual environment nicht gefunden."
|
||||
echo "Bitte zuerst bin/install_py.sh ausfuehren."
|
||||
return 1
|
||||
fi
|
||||
|
||||
source "$PROJECT/.venv/bin/activate"
|
||||
echo "Virtuelle Umgebung aktiviert."
|
||||
echo "Python-Version:"
|
||||
python --version
|
||||
echo ""
|
||||
echo "Installierte Pakete:"
|
||||
pip list
|
||||
"""
|
||||
|
||||
|
||||
def tpl_get_cmd_bat(name_upper: str) -> str:
|
||||
return f"""@echo off
|
||||
REM ================================================================
|
||||
REM {name_upper} - Shell mit gesetzten Umgebungsvariablen oeffnen
|
||||
REM ================================================================
|
||||
|
||||
call "%~dp0setenv.bat"
|
||||
start cmd /k "echo {name_upper} Umgebung aktiv. && echo PROJECT=%PROJECT%"
|
||||
"""
|
||||
|
||||
|
||||
def tpl_get_cmd_sh(name_upper: str) -> str:
|
||||
return f"""#!/usr/bin/env bash
|
||||
# ================================================================
|
||||
# {name_upper} - Shell mit gesetzten Umgebungsvariablen oeffnen
|
||||
# ================================================================
|
||||
# Verwendung: source bin/get_cmd.sh
|
||||
# ================================================================
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${{BASH_SOURCE[0]}}")" && pwd)"
|
||||
source "$SCRIPT_DIR/setenv.sh"
|
||||
|
||||
exec "$SHELL"
|
||||
"""
|
||||
|
||||
|
||||
GITIGNORE = """\
|
||||
# ---> Python
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
*.so
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
*.manifest
|
||||
*.spec
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
*.mo
|
||||
*.pot
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
.spyderproject
|
||||
.spyproject
|
||||
.ropeproject
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
.pyre/
|
||||
.pytype/
|
||||
cython_debug/
|
||||
.pdm.toml
|
||||
__pypackages__/
|
||||
/site-packages
|
||||
/site
|
||||
.ipynb_checkpoints
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# Benutzerdefiniert
|
||||
/data
|
||||
/work
|
||||
/log
|
||||
/results
|
||||
"""
|
||||
|
||||
|
||||
def tpl_license(year: int) -> str:
|
||||
return f"""MIT License
|
||||
|
||||
Copyright (c) {year} Michael Stangl, on GitHub mistamichael
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
"""
|
||||
|
||||
|
||||
def tpl_readme(project_name: str) -> str:
|
||||
return f"""# {project_name}
|
||||
|
||||
> Kurze Projektbeschreibung hier eintragen.
|
||||
|
||||
## Projektstruktur
|
||||
|
||||
```
|
||||
{project_name}/
|
||||
├── bin/ # Skripte zur Umgebungsverwaltung
|
||||
│ ├── setenv.bat/.sh # Umgebungsvariablen setzen
|
||||
│ ├── install_py.bat/.sh # venv erstellen + pip install
|
||||
│ ├── activate_venv.bat/.sh # venv aktivieren
|
||||
│ └── get_cmd.bat/.sh # Shell mit Umgebung oeffnen
|
||||
├── cfg/ # Konfigurationsdateien (INI/JSON)
|
||||
├── data/ # Eingabedaten (nicht im Git)
|
||||
├── doc/ # Dokumentation
|
||||
├── examples/ # Beispieldateien
|
||||
├── lib/ # Python-Quellcode / Bibliothek
|
||||
├── log/ # Log-Dateien (nicht im Git)
|
||||
├── results/ # Ergebnisse / Ausgaben (nicht im Git)
|
||||
├── tests/ # Unit Tests
|
||||
├── .gitignore
|
||||
├── LICENSE
|
||||
├── README.md
|
||||
└── requirements.txt
|
||||
```
|
||||
|
||||
## Umgebungsvariablen
|
||||
|
||||
| Variable | Beschreibung |
|
||||
|----------------|---------------------------|
|
||||
| `PROJECT` | Wurzelverzeichnis |
|
||||
| `PV_BIN` | Skriptverzeichnis |
|
||||
| `PV_LIB` | Python-Quellcode |
|
||||
| `PV_CFG` | Konfigurationsdateien |
|
||||
| `PV_DATA` | Eingabedaten |
|
||||
| `PV_LOG` | Log-Dateien |
|
||||
| `PV_RESULTS` | Ergebnisse |
|
||||
| `PV_EXAMPLES` | Beispieldateien |
|
||||
| `PYTHONPATH` | Erweitert um `PV_LIB` |
|
||||
|
||||
## Installation
|
||||
|
||||
### Voraussetzungen
|
||||
|
||||
- Python 3.10 oder hoeher
|
||||
|
||||
### Setup (Windows)
|
||||
|
||||
```bat
|
||||
bin\\install_py.bat
|
||||
```
|
||||
|
||||
### Setup (Linux / macOS)
|
||||
|
||||
```bash
|
||||
bash bin/install_py.sh
|
||||
```
|
||||
|
||||
## Nutzung
|
||||
|
||||
### Umgebung setzen
|
||||
|
||||
```bat
|
||||
bin\\setenv.bat # Windows
|
||||
```
|
||||
```bash
|
||||
source bin/setenv.sh # Linux / macOS
|
||||
```
|
||||
|
||||
### Shell mit gesetzten Variablen oeffnen
|
||||
|
||||
```bat
|
||||
bin\\get_cmd.bat # Windows
|
||||
```
|
||||
```bash
|
||||
source bin/get_cmd.sh # Linux / macOS
|
||||
```
|
||||
|
||||
### venv aktivieren
|
||||
|
||||
```bat
|
||||
bin\\activate_venv.bat # Windows
|
||||
```
|
||||
```bash
|
||||
source bin/activate_venv.sh # Linux / macOS
|
||||
```
|
||||
|
||||
## Lizenz
|
||||
|
||||
MIT License — siehe [LICENSE](LICENSE)
|
||||
|
||||
## Autor
|
||||
|
||||
Michael Stangl (GitHub: mistamichael)
|
||||
"""
|
||||
|
||||
|
||||
REQUIREMENTS = """\
|
||||
# Python-Abhaengigkeiten
|
||||
# Installieren mit: pip install -r requirements.txt
|
||||
|
||||
# Beispiel-Abhaengigkeiten – anpassen nach Bedarf:
|
||||
# pydantic >= 2.0.0
|
||||
# pytest >= 9.0.0
|
||||
"""
|
||||
|
||||
|
||||
# =====================================================================
|
||||
# Hauptlogik
|
||||
# =====================================================================
|
||||
|
||||
DIRECTORIES = ["bin", "cfg", "data", "doc", "examples", "lib", "log", "results", "tests"]
|
||||
|
||||
|
||||
def create_project(project_name: str, target_base: str) -> None:
|
||||
name_upper = project_name.upper()
|
||||
year = datetime.now().year
|
||||
target = Path(target_base) / project_name
|
||||
|
||||
if target.exists():
|
||||
print(f"FEHLER: Verzeichnis '{target}' existiert bereits.")
|
||||
sys.exit(1)
|
||||
|
||||
print("=" * 64)
|
||||
print(f"Erstelle Python-Projekt: {project_name}")
|
||||
print(f"Zielverzeichnis: {target}")
|
||||
print("=" * 64)
|
||||
|
||||
# Verzeichnisse
|
||||
for d in DIRECTORIES:
|
||||
(target / d).mkdir(parents=True, exist_ok=True)
|
||||
print("[OK] Verzeichnisse angelegt")
|
||||
|
||||
# bin/ Skripte
|
||||
files = {
|
||||
"bin/setenv.bat": tpl_setenv_bat(name_upper),
|
||||
"bin/setenv.sh": tpl_setenv_sh(name_upper),
|
||||
"bin/install_py.bat": tpl_install_py_bat(name_upper),
|
||||
"bin/install_py.sh": tpl_install_py_sh(name_upper),
|
||||
"bin/activate_venv.bat": tpl_activate_venv_bat(name_upper),
|
||||
"bin/activate_venv.sh": tpl_activate_venv_sh(name_upper),
|
||||
"bin/get_cmd.bat": tpl_get_cmd_bat(name_upper),
|
||||
"bin/get_cmd.sh": tpl_get_cmd_sh(name_upper),
|
||||
}
|
||||
for rel_path, content in files.items():
|
||||
p = target / rel_path
|
||||
p.write_text(content, encoding="utf-8")
|
||||
if rel_path.endswith(".sh"):
|
||||
p.chmod(p.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
|
||||
print("[OK] bin/ Skripte erstellt")
|
||||
|
||||
# .gitignore
|
||||
(target / ".gitignore").write_text(GITIGNORE, encoding="utf-8")
|
||||
print("[OK] .gitignore erstellt")
|
||||
|
||||
# LICENSE
|
||||
(target / "LICENSE").write_text(tpl_license(year), encoding="utf-8")
|
||||
print("[OK] LICENSE erstellt")
|
||||
|
||||
# README.md
|
||||
(target / "README.md").write_text(tpl_readme(project_name), encoding="utf-8")
|
||||
print("[OK] README.md erstellt")
|
||||
|
||||
# requirements.txt
|
||||
(target / "requirements.txt").write_text(REQUIREMENTS, encoding="utf-8")
|
||||
print("[OK] requirements.txt erstellt")
|
||||
|
||||
print()
|
||||
print("=" * 64)
|
||||
print(f"FERTIG! Projekt '{project_name}' wurde erstellt.")
|
||||
print("=" * 64)
|
||||
print()
|
||||
print("Naechste Schritte:")
|
||||
print(" Windows: bin\\install_py.bat")
|
||||
print(" Linux: bash bin/install_py.sh")
|
||||
print()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Standard Python Projekt Template erstellen"
|
||||
)
|
||||
parser.add_argument("projektname", help="Name des neuen Projekts")
|
||||
parser.add_argument(
|
||||
"zielverzeichnis",
|
||||
nargs="?",
|
||||
default=".",
|
||||
help="Zielverzeichnis (Standard: aktuelles Verzeichnis)",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
create_project(args.projektname, args.zielverzeichnis)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user