Revert "Pdm"

This commit is contained in:
Andreas Zweili 2023-08-27 19:06:06 +02:00 committed by GitHub
parent e5012bc0f8
commit 8181db93ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 1501 additions and 1274 deletions

View File

@ -1,3 +0,0 @@
[*.sh]
indent_style = space
indent_size = 4

23
.envrc
View File

@ -9,30 +9,7 @@ layout_postgres() {
echo "CREATE DATABASE django;" | postgres --single -E postgres
fi
}
layout_pdm() {
PYPROJECT_TOML="${PYPROJECT_TOML:-pyproject.toml}"
if [ ! -f "$PYPROJECT_TOML" ]; then
log_status "No pyproject.toml found. Executing \`pmd init\` to create a \`$PYPROJECT_TOML\` first."
pdm init --non-interactive --python "$(python3 --version 2>/dev/null | cut -d' ' -f2 | cut -d. -f1-2)"
fi
VIRTUAL_ENV=$(pdm venv list | grep "^\*" | awk -F" " '{print $3}')
if [ -z "$VIRTUAL_ENV" ] || [ ! -d "$VIRTUAL_ENV" ]; then
log_status "No virtual environment exists. Executing \`pdm info\` to create one."
pdm info
VIRTUAL_ENV=$(pdm venv list | grep "^\*" | awk -F" " '{print $3}')
fi
PATH_add "$VIRTUAL_ENV/bin"
export PDM_ACTIVE=1
export VIRTUAL_ENV
}
layout postgres
layout pdm
export PROJECT_DIR=$(pwd)
export WEBPORT=$(($RANDOM + 1100))
export PGPORT=$(($WEBPORT + 100))

View File

@ -32,7 +32,7 @@ jobs:
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: Buid container
run: |
nix build --no-sandbox .#container
nix build .#container
docker load < result
- name: Log into registry

View File

@ -13,9 +13,6 @@ jobs:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: Setup dependencies
run: nix develop --command bash -c "pdm sync"
- name: Linting
run: nix develop --command bash -c "pdm run dev linting"
- name: Tests
run: nix develop --command bash -c "pdm run pytest --ds=network_inventory.settings.ram_test -nauto --nomigrations --cov=./src ./src"
- name: Test
run: |
nix flake check -L -j auto

1
.gitignore vendored
View File

@ -181,4 +181,3 @@ db_data
.direnv
/result
/static/
.pdm-python

58
dev.sh
View File

@ -2,12 +2,12 @@
# Helper functions not exposed to the user {
# Load example data
_init() {
_init () {
python ./src/manage.py loaddata src/network_inventory.yaml
}
# Setup the database
_setup() {
_setup () {
overmind start -l db -D
if [ -f .direnv/first_run ]; then
sleep 2
@ -41,15 +41,15 @@ _setup() {
sleep 2
}
_open_url() {
_open_url () {
if [[ ! -z "${DEFAULT_BROWSER}" ]]; then
$DEFAULT_BROWSER $url
elif type explorer.exe &>/dev/null; then
elif type explorer.exe &> /dev/null; then
explorer.exe $url
fi
}
_create_url() {
_create_url () {
if [ -f /etc/wsl.conf ]; then
echo "http://localhost:$WEBPORT"
else
@ -62,7 +62,7 @@ _create_url() {
declare -A tasks
declare -A descriptions
run() {
run () {
_setup
find . -name __pycache__ -o -name "*.pyc" -delete
url=$(_create_url)
@ -76,25 +76,25 @@ tasks["run"]=run
descriptions["start"]="Alias for run."
tasks["start"]=run
stop() {
stop () {
overmind quit
}
descriptions["stop"]="Stop the webserver and DB."
tasks["stop"]=stop
venv() {
venv () {
nix build .#venv -o .venv
}
descriptions["venv"]="Build a pseudo venv that editors like VS Code can use."
tasks["venv"]=venv
build-container() {
nix build && docker load <result && docker run --rm -ti network-inventory:latest
build-container (){
nix build && docker load < result && docker run --rm -ti network-inventory:latest
}
descriptions["build-container"]="Build and load OCI container."
tasks["build-container"]=build-container
clean() {
clean () {
find . \( -name __pycache__ -o -name "*.pyc" \) -delete
rm -rf htmlcov/
rm -f .direnv/first_run
@ -104,44 +104,35 @@ clean() {
descriptions["clean"]="Reset the project to a fresh state including the database."
tasks["clean"]=clean
cleanall() {
cleanall () {
git clean -xdf
}
descriptions["cleanall"]="Completly remove any files which are not checked into git."
tasks["cleanall"]=cleanall
debug() {
debug () {
pytest --pdb --nomigrations --cov=. --cov-report=html ./src/
}
descriptions["debug"]="Run the tests and drop into the debugger on failure."
tasks["debug"]=debug
linting() {
pylint --rc-file pyproject.toml -j 0 -E ./src/
mypy ./src/
}
descriptions["linting"]="Run the linters agains the source code. Tests are excluded."
tasks["linting"]=linting
check() {
linting
test
check (){
nix flake check
}
descriptions["check"]="Run the linter and tests."
tasks["check"]=check
test() {
DJANGO_SETTINGS_MODULE=network_inventory.settings.ram_test \
pytest -nauto \
--nomigrations \
--cov-config="$PROJECT_DIR/.coveragerc" \
--cov-report=html "$PROJECT_DIR/src"
test (){
export DJANGO_SETTINGS_MODULE=network_inventory.settings.ram_test
pytest -nauto --nomigrations --cov-config="$PROJECT_DIR/.coveragerc" --cov-report=html "$PROJECT_DIR/src"
}
descriptions["test"]="Run the tests in the RAM DB and write a coverage report."
tasks["test"]=test
update() {
pdm update
update (){
poetry update --lock
}
descriptions["update"]="Update the dependencies."
tasks["update"]=update
@ -149,13 +140,14 @@ tasks["update"]=update
# only one task at a time
if [ $# != 1 ]; then
printf "usage: dev <task_name>\n\n"
for task in "${!tasks[@]}"; do
for task in "${!tasks[@]}"
do
echo "$task - ${descriptions[$task]}"
done
else
# Check if task is available
if [[ -v "tasks[$1]" ]]; then
if [[ -v "tasks[$1]" ]] ; then
${tasks["$1"]}
else
echo "Task not found."

View File

@ -73,27 +73,13 @@
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1693003285,
"narHash": "sha256-5nm4yrEHKupjn62MibENtfqlP6pWcRTuSKrMiH9bLkc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "5690c4271f2998c304a45c91a0aeb8fb69feaea7",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"poetry2nix": {
"inputs": {
"flake-utils": "flake-utils_2",
"nix-github-actions": "nix-github-actions",
"nixpkgs": "nixpkgs_2"
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1693051011,
@ -104,8 +90,9 @@
"type": "github"
},
"original": {
"id": "poetry2nix",
"type": "indirect"
"owner": "nix-community",
"repo": "poetry2nix",
"type": "github"
}
},
"root": {

136
flake.nix
View File

@ -3,40 +3,53 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
poetry2nix = {
url = "github:nix-community/poetry2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, flake-utils, poetry2nix }:
(flake-utils.lib.eachDefaultSystem (system:
{
overlays.default = nixpkgs.lib.composeManyExtensions [
poetry2nix.overlay
(final: prev: rec {
inventoryDevEnv = prev.poetry2nix.mkPoetryEnv
{
projectDir = ./.;
groups = [ "main" "dev" ];
};
inventoryPackage = prev.poetry2nix.mkPoetryApplication {
projectDir = ./.;
groups = [ "main" ];
};
inventoryEnv = inventoryPackage.dependencyEnv;
})
];
} // (flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ self.overlays.default ];
};
ld_path = pkgs.lib.makeLibraryPath [ pkgs.openssl ];
inventory = pkgs.stdenv.mkDerivation {
src = ./.;
version = "latest";
pname = "network-inventory";
buildInputs = [
pkgs.pdm
pkgs.python310
];
installPhase = ''
runHook preInstall
mkdir -p $out/code
pdm sync --production --no-editable --fail-fast
cp pdm.toml pdm.lock pyproject.toml $out/code/
cp -r .venv $out/code/.venv
cp -r ./src $out/code/src
runHook postInstall
mkdir -p $out
cp -r ./src $out/code
'';
};
in
rec {
devShells.default = pkgs.mkShell {
buildInputs = [
pkgs.pdm
pkgs.inventoryDevEnv
pkgs.poetry
pkgs.python310Packages.pip
pkgs.overmind
pkgs.postgresql_15
pkgs.python310
(pkgs.writeScriptBin "dev" "${builtins.readFile ./dev.sh}")
];
PYTHON_KEYRING_BACKEND = "keyring.backends.fail.Keyring";
@ -44,7 +57,44 @@
export DJANGO_SETTINGS_MODULE=network_inventory.settings.local
'';
};
checks = {
lint = pkgs.stdenv.mkDerivation {
dontPatch = true;
dontConfigure = true;
dontBuild = true;
dontInstall = true;
doCheck = true;
name = "lint";
src = ./.;
checkInputs = [ pkgs.inventoryDevEnv ];
checkPhase = ''
mkdir -p $out
pylint --rc-file pyproject.toml -j 0 -E src/
cd src/ && mypy --config-file=../pyproject.toml .
'';
DJANGO_SETTINGS_MODULE = "network_inventory.settings.ram_test";
};
tests = pkgs.stdenv.mkDerivation {
dontPatch = true;
dontConfigure = true;
dontBuild = true;
dontInstall = true;
doCheck = true;
name = "test";
src = ./.;
checkInputs = [ pkgs.inventoryDevEnv pkgs.postgresql_15 pkgs.overmind ];
checkPhase = ''
mkdir -p $out
pytest --ds=network_inventory.settings.ram_test \
-nauto \
--nomigrations \
--cov=./src \
./src
'';
};
};
packages = {
venv = pkgs.inventoryEnv;
container = pkgs.dockerTools.buildImage {
name = "network-inventory";
tag = "latest";
@ -52,52 +102,48 @@
copyToRoot = pkgs.buildEnv {
name = "image-root";
paths = [
inventory
pkgs.bashInteractive
pkgs.coreutils
pkgs.pdm
pkgs.python310
inventory
(pkgs.writeShellScriptBin "start-inventory" ''
if [ -f .first_run ]; then
sleep 2
django-admin collectstatic --noinput
django-admin makemigrations
django-admin migrate
${pkgs.inventoryEnv}/bin/django-admin collectstatic --noinput
${pkgs.inventoryEnv}/bin/django-admin makemigrations
${pkgs.inventoryEnv}/bin/django-admin migrate
else
django-admin collectstatic --noinput
django-admin makemigrations backups
django-admin makemigrations computers
django-admin makemigrations core
django-admin makemigrations customers
django-admin makemigrations devices
django-admin makemigrations licenses
django-admin makemigrations nets
django-admin makemigrations softwares
django-admin makemigrations users
django-admin makemigrations
django-admin migrate
django-admin loaddata backups
django-admin loaddata computers
django-admin loaddata core
django-admin loaddata devices
django-admin loaddata nets
django-admin loaddata softwares
django-admin shell -c "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'admin@example.com', 'password')"
${pkgs.inventoryEnv}/bin/django-admin collectstatic --noinput
${pkgs.inventoryEnv}/bin/django-admin makemigrations backups
${pkgs.inventoryEnv}/bin/django-admin makemigrations computers
${pkgs.inventoryEnv}/bin/django-admin makemigrations core
${pkgs.inventoryEnv}/bin/django-admin makemigrations customers
${pkgs.inventoryEnv}/bin/django-admin makemigrations devices
${pkgs.inventoryEnv}/bin/django-admin makemigrations licenses
${pkgs.inventoryEnv}/bin/django-admin makemigrations nets
${pkgs.inventoryEnv}/bin/django-admin makemigrations softwares
${pkgs.inventoryEnv}/bin/django-admin makemigrations users
${pkgs.inventoryEnv}/bin/django-admin makemigrations
${pkgs.inventoryEnv}/bin/django-admin migrate
${pkgs.inventoryEnv}/bin/django-admin loaddata backups
${pkgs.inventoryEnv}/bin/django-admin loaddata computers
${pkgs.inventoryEnv}/bin/django-admin loaddata core
${pkgs.inventoryEnv}/bin/django-admin loaddata devices
${pkgs.inventoryEnv}/bin/django-admin loaddata nets
${pkgs.inventoryEnv}/bin/django-admin loaddata softwares
${pkgs.inventoryEnv}/bin/django-admin shell -c "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'admin@example.com', 'password')"
touch .first_run
fi
gunicorn network_inventory.wsgi:application --reload --bind 0.0.0.0:8000 --workers 3
${pkgs.inventoryEnv}/bin/gunicorn network_inventory.wsgi:application --reload --bind 0.0.0.0:8000 --workers 3
'')
];
};
config = {
Cmd = [ "pdm run start-inventory" ];
Cmd = [ "start-inventory" ];
WorkingDir = "/code";
Env = [
"POSTGRES_DB=network_inventory"
"DJANGO_SETTINGS_MODULE=network_inventory.settings.production"
"PYTHONPATH=$PYTHONPATH:/lib/python3.10:/lib/python3.10/site-packages:/code/src:/code/.venv/lib/python3.10/site-packages"
"PATH=$PATH:/bin:/code/.venv/bin"
"LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${ld_path}"
"PYTHONPATH=/lib/python3.10:/lib/python3.10/site-packages:/code"
];
};
};

1098
pdm.lock

File diff suppressed because it is too large Load Diff

View File

@ -1,2 +0,0 @@
[venv]
backend = "venv"

1330
poetry.lock generated Normal file

File diff suppressed because it is too large Load Diff

2
poetry.toml Normal file
View File

@ -0,0 +1,2 @@
[virtualenvs]
in-project = true

View File

@ -1,35 +1,20 @@
[project]
description = "A Django application designed to track IT inventory of multiple customers."
authors = [
{name = "Andreas Zweili", email = "andreas@zweili.ch"},
]
dependencies = [
"Django>=4.1.3",
"django-crispy-forms>=1.14,<2.0.0", # version 2 breaks tests
"django-filter>=23.2",
"django-floppyforms>=1.9.0",
"django-guardian>=2.4.0",
"django-htmx>=1.16.0",
"django-model-utils>=4.3.1",
"django-nested-admin>=4.0.2",
"django-tables2>=2.4.1,<2.6.0", # version 2.6 breaks tests
"gunicorn>=21.2.0",
"psycopg2-binary>=2.9.7",
"PyYAML>=6.0.1",
"setuptools>=68.1.2",
]
requires-python = ">=3.10"
license = {text = "GPLv3+"}
classifiers = [
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
]
[tool.pylint]
max-line-length = 88
load-plugins = [
"pylint_django",
]
[tool.poetry]
name = "network_inventory"
version = "0.1.0"
description = ""
authors = ["Andreas Zweili <andreas@zweili.ch>"]
license = "GPLv3"
packages = [
{ include = "src" },
]
virtualenvs.in-project = true
[tool.mypy]
exclude = [
"tests/",
@ -69,20 +54,35 @@ module = [
]
ignore_missing_imports = true
[tool.pdm.dev-dependencies]
linting = [
"black>=23.7.0",
"django-stubs>=4.2.3",
"mypy>=1.5.1",
"pylint>=2.17.5",
"pylint-django>=2.5.3",
]
test = [
"coverage>=7.3.0",
"mixer>=7.2.2",
"pytest>=7.4.0",
"pytest-cov>=4.1.0",
"pytest-django>=4.5.2",
"pytest-xdist>=3.3.1",
"python-lsp-server>=1.7.4",
]
[tool.poetry.group.main.dependencies]
python = "^3.9"
Django = "^4.1.3"
django-crispy-forms = "^1.14.0"
django-filter = "^23.2"
django-floppyforms = "^1.9.0"
django-guardian = "^2.4.0"
django-htmx = "^1.13.0"
django-model-utils = "^4.2.0"
django-nested-admin = "^4.0.2"
django-tables2 = "^2.4.1"
gunicorn = "^20.1.0"
psycopg2-binary = "^2.9.5"
PyYAML = "^6.0"
[tool.poetry.group.dev.dependencies]
black = "^22.10.0"
coverage = "^6.5.0"
mixer = "^7.2.2"
pylint = "^2.15.8"
pytest = "^7.2.0"
pytest-cov = "^4.0.0"
pytest-django = "^4.5.2"
pytest-xdist = "^3.1.0"
python-lsp-server = "^1.7.3"
mypy = "^1.4.1"
django-stubs = "^4.2.3"
pylint-django = "^2.5.3"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"