45 lines
3 KiB
Python
45 lines
3 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import subprocess
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from django.core.management.base import BaseCommand, CommandError
|
||
|
|
|
||
|
|
|
||
|
|
def write(path: Path, content: str) -> None:
|
||
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||
|
|
path.write_text(content, encoding="utf-8")
|
||
|
|
|
||
|
|
|
||
|
|
class Command(BaseCommand):
|
||
|
|
help = "Create a disposable Django Git repository for Artifex smoke tests."
|
||
|
|
|
||
|
|
def add_arguments(self, parser):
|
||
|
|
parser.add_argument("path")
|
||
|
|
|
||
|
|
def handle(self, *args, **options):
|
||
|
|
repo = Path(options["path"]).resolve()
|
||
|
|
if repo.exists() and any(repo.iterdir()):
|
||
|
|
raise CommandError("Target path exists and is not empty")
|
||
|
|
repo.mkdir(parents=True, exist_ok=True)
|
||
|
|
write(repo / "manage.py", "#!/usr/bin/env python\nimport os, sys\nos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')\nfrom django.core.management import execute_from_command_line\nexecute_from_command_line(sys.argv)\n")
|
||
|
|
write(repo / "app" / "__init__.py", "")
|
||
|
|
write(repo / "app" / "settings.py", "SECRET_KEY='test'\nDEBUG=True\nALLOWED_HOSTS=['testserver','localhost']\nROOT_URLCONF='app.urls'\nUSE_TZ=True\nDEFAULT_AUTO_FIELD='django.db.models.BigAutoField'\nDATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}}\nINSTALLED_APPS=['django.contrib.auth','django.contrib.contenttypes','items']\nMIDDLEWARE=[]\n")
|
||
|
|
write(repo / "app" / "urls.py", "from django.urls import path\n\nurlpatterns = []\n")
|
||
|
|
write(repo / "items" / "__init__.py", "")
|
||
|
|
write(repo / "items" / "models.py", "from django.db import models\n\n\nclass Item(models.Model):\n name = models.CharField(max_length=100)\n\n def __str__(self):\n return self.name\n")
|
||
|
|
write(repo / "items" / "admin.py", "from django.contrib import admin\n\nfrom items.models import Item\n\nadmin.site.register(Item)\n")
|
||
|
|
write(repo / "items" / "migrations" / "__init__.py", "")
|
||
|
|
write(repo / "items" / "migrations" / "0001_initial.py", "from django.db import migrations, models\n\n\nclass Migration(migrations.Migration):\n initial = True\n dependencies = []\n operations = [migrations.CreateModel(name='Item', fields=[('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=100))])]\n")
|
||
|
|
write(repo / "tests" / "__init__.py", "")
|
||
|
|
write(repo / "tests" / "test_baseline.py", "def test_baseline():\n assert True\n")
|
||
|
|
for command in [
|
||
|
|
["git", "init", "-b", "main"],
|
||
|
|
["git", "-c", "user.name=Test", "-c", "user.email=test@example.invalid", "add", "."],
|
||
|
|
["git", "-c", "user.name=Test", "-c", "user.email=test@example.invalid", "commit", "-m", "Initial disposable repo"],
|
||
|
|
]:
|
||
|
|
completed = subprocess.run(command, cwd=repo, capture_output=True, text=True, check=False)
|
||
|
|
if completed.returncode != 0:
|
||
|
|
raise CommandError(completed.stderr or completed.stdout)
|
||
|
|
self.stdout.write(str(repo))
|