Skip to content

Commit

Permalink
Updates for different python and django versions.
Browse files Browse the repository at this point in the history
Version bump and new core.
Actions betterification.

Co-authored-by: Piotr Kaznowski <[email protected]>
  • Loading branch information
filiplajszczak and caseneuve committed Nov 28, 2024
1 parent 6adad1d commit 8eb74e6
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 102 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
name: Tests

on: [push]
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ "3.8", "3.9", "3.10", "3.11" ]
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12", "3.13" ]

name: Python ${{ matrix.python-version }}
steps:

- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4

- name: Setup timezone
uses: zcong1993/setup-timezone@master
with:
timezone: UTC

- name: Set up Python
uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5
with:
Expand Down
7 changes: 7 additions & 0 deletions cli/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
@app.command()
def autoconfigure(
repo_url: str = typer.Argument(..., help="url of remote git repository of your django project"),
branch: str = typer.Option(
"None",
"-b",
"--branch",
help="Branch name in case of multiple branches",
),
domain_name: str = typer.Option(
"your-username.pythonanywhere.com",
"-d",
Expand Down Expand Up @@ -43,6 +49,7 @@ def autoconfigure(
project = DjangoProject(domain, python_version)
project.sanity_checks(nuke=nuke)
project.download_repo(repo_url, nuke=nuke),
project.ensure_branch(branch),
project.create_virtualenv(nuke=nuke)
project.create_webapp(nuke=nuke)
project.add_static_file_mappings()
Expand Down
2 changes: 1 addition & 1 deletion pythonanywhere/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.15.4"
__version__ = "0.15.5"
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="pythonanywhere",
version="0.15.4",
version="0.15.5",
description="PythonAnywhere helper tools for users",
long_description=long_description,
long_description_content_type="text/markdown",
Expand All @@ -22,6 +22,8 @@
"Intended Audience :: Developers",
"Topic :: Software Development :: Libraries",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.9",
Expand All @@ -36,7 +38,7 @@
"docopt",
"packaging",
"python-dateutil",
"pythonanywhere_core==0.2.3",
"pythonanywhere_core==0.2.4",
"requests",
"schema",
"snakesay",
Expand Down
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tempfile
from getpass import getuser
from pathlib import Path
from platform import python_version
from unittest.mock import Mock, patch

import psutil
Expand Down Expand Up @@ -120,3 +121,21 @@ def process_killer():
for child in psutil.Process(p.pid).children():
child.kill()
p.kill()

@pytest.fixture
def running_python_version():
return ".".join(python_version().split(".")[:2])

@pytest.fixture
def new_django_version(running_python_version):
if running_python_version in ["3.10", "3.11", "3.12", "3.13"]:
return "5.1.3"
else:
return "4.2.16"

@pytest.fixture
def old_django_version(running_python_version):
if running_python_version in ["3.10", "3.11", "3.12", "3.13"]:
return "5.1.2"
else:
return "4.2.15"
20 changes: 12 additions & 8 deletions tests/test_cli_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def test_autoconfigure_calls_all_stuff_in_right_order(mock_django_project):
assert mock_django_project.return_value.method_calls == [
call.sanity_checks(nuke=True),
call.download_repo("repo.url", nuke=True),
call.ensure_branch("None"),
call.create_virtualenv(nuke=True),
call.create_webapp(nuke=True),
call.add_static_file_mappings(),
Expand Down Expand Up @@ -86,6 +87,8 @@ def test_autoconfigure_actually_works_against_example_repo(
process_killer,
running_python_version,
):
git_ref = "non-nested-old" if running_python_version in ["3.8", "3.9"] else "master"
expected_django_version = "4.2.16" if running_python_version in ["3.8", "3.9"] else "5.1.3"
mocker.patch("cli.django.DjangoProject.start_bash")
repo = "https://github.com/pythonanywhere/example-django-project.git"
domain = "mydomain.com"
Expand All @@ -99,10 +102,11 @@ def test_autoconfigure_actually_works_against_example_repo(
domain,
"-p",
running_python_version,
"--branch",
git_ref,
],
)

expected_django_version = "3.0.6"
expected_virtualenv = virtualenvs_folder / domain
expected_project_path = fake_home / domain
django_project_name = "myproject"
Expand All @@ -122,7 +126,7 @@ def test_autoconfigure_actually_works_against_example_repo(

with expected_settings_path.open() as f:
lines = f.read().split("\n")
assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" in lines
assert "MEDIA_ROOT = Path(BASE_DIR / 'media')" in lines
assert "ALLOWED_HOSTS = ['mydomain.com'] # type: List[str]" in lines

assert "base.css" in os.listdir(str(fake_home / domain / "static/admin/css"))
Expand Down Expand Up @@ -180,6 +184,7 @@ def test_start_actually_creates_django_project_in_virtualenv_with_hacked_setting
virtualenvs_folder,
api_token,
running_python_version,
new_django_version,
):
runner.invoke(
app,
Expand All @@ -188,7 +193,7 @@ def test_start_actually_creates_django_project_in_virtualenv_with_hacked_setting
"-d",
"mydomain.com",
"-j",
"2.2.12",
new_django_version,
"-p",
running_python_version,
],
Expand All @@ -204,11 +209,11 @@ def test_start_actually_creates_django_project_in_virtualenv_with_hacked_setting
.decode()
.strip()
)
assert django_version == "2.2.12"
assert django_version == new_django_version

with (fake_home / "mydomain.com/mysite/settings.py").open() as f:
lines = f.read().split("\n")
assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" in lines
assert "MEDIA_ROOT = Path(BASE_DIR / 'media')" in lines
assert "ALLOWED_HOSTS = ['mydomain.com']" in lines

assert "base.css" in os.listdir(str(fake_home / "mydomain.com/static/admin/css"))
Expand All @@ -222,10 +227,9 @@ def test_nuke_option_lets_you_run_twice(
virtualenvs_folder,
api_token,
running_python_version,
old_django_version,
new_django_version,
):
old_django_version = "2.2.12"
new_django_version = "3.0.6"

runner.invoke(
app,
[
Expand Down
22 changes: 11 additions & 11 deletions tests/test_django_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,21 @@ def test_nuke_option_handles_directory_not_existing(self, mock_subprocess, fake_


@pytest.fixture
def non_nested_submodule():
def non_nested_submodule(running_python_version):
git_ref = "non-nested-old" if running_python_version in ["3.8", "3.9"] else "master"
subprocess.check_call(["git", "submodule", "update", "--init", "--recursive"])
submodule_path = Path(__file__).parents[1] / "submodules" / "example-django-project"
subprocess.check_call(["git", "checkout", "master"], cwd=str(submodule_path))
subprocess.check_call(["git", "checkout", git_ref], cwd=str(submodule_path))
yield submodule_path
subprocess.check_call(["git", "submodule", "update", "--init", "--recursive"])


@pytest.fixture
def more_nested_submodule():
def more_nested_submodule(running_python_version):
git_ref = "more-nested-old" if running_python_version in ["3.8", "3.9"] else "morenested"
subprocess.check_call(["git", "submodule", "update", "--init", "--recursive"])
submodule_path = Path(__file__).parents[1] / "submodules" / "example-django-project"
subprocess.check_call(["git", "checkout", "morenested"], cwd=str(submodule_path))
subprocess.check_call(["git", "checkout", git_ref], cwd=str(submodule_path))
yield submodule_path
subprocess.check_call(["git", "submodule", "update", "--init", "--recursive"])

Expand Down Expand Up @@ -509,9 +511,8 @@ def test_updates_wsgi_file_from_template(self, virtualenvs_folder):

@pytest.mark.slowtest
def test_actually_produces_wsgi_file_that_can_import_project_non_nested(
self, fake_home, non_nested_submodule, virtualenvs_folder
self, fake_home, non_nested_submodule, virtualenvs_folder, running_python_version
):
running_python_version = ".".join(python_version().split(".")[:2])
project = DjangoProject("mydomain.com", running_python_version)
shutil.copytree(str(non_nested_submodule), str(project.project_path))
if running_python_version in ["3.8", "3.9", "3.10", "3.11"]:
Expand All @@ -529,15 +530,14 @@ def test_actually_produces_wsgi_file_that_can_import_project_non_nested(

@pytest.mark.slowtest
def test_actually_produces_wsgi_file_that_can_import_nested_project(
self, fake_home, more_nested_submodule, virtualenvs_folder
self, fake_home, more_nested_submodule, virtualenvs_folder, running_python_version
):
running_python_version = ".".join(python_version().split(".")[:2])
project = DjangoProject("mydomain.com", running_python_version)
shutil.copytree(str(more_nested_submodule), str(project.project_path))
if running_python_version in ["3.8", "3.9", "3.10", "3.11"]:
project.create_virtualenv(django_version="latest")
else:
if running_python_version in ["3.8", "3.9"]:
project.create_virtualenv()
else:
project.create_virtualenv(django_version="latest")

project.find_django_files()
project.wsgi_file_path = Path(tempfile.NamedTemporaryFile().name)
Expand Down
13 changes: 6 additions & 7 deletions tests/test_pa_autoconfigure_django.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from platform import python_version
from unittest.mock import call, patch
import os
import pytest
Expand All @@ -7,6 +6,7 @@
import time

from scripts.pa_autoconfigure_django import main
from tests.conftest import new_django_version


class TestMain:
Expand All @@ -33,23 +33,22 @@ def test_calls_all_stuff_in_right_order(self):

@pytest.mark.slowtest
def test_actually_works_against_example_repo(
self, fake_home, virtualenvs_folder, api_token, process_killer
self, fake_home, virtualenvs_folder, api_token, process_killer, running_python_version, new_django_version
):
running_python_version = ".".join(python_version().split(".")[:2])
git_ref = "non-nested-old" if running_python_version in ["3.8", "3.9"] else "master"
repo = 'https://github.com/pythonanywhere/example-django-project.git'
domain = 'mydomain.com'
with patch('scripts.pa_autoconfigure_django.DjangoProject.update_wsgi_file'):
with patch('scripts.pa_autoconfigure_django.DjangoProject.start_bash'):
with patch('pythonanywhere_core.webapp.call_api'):
main(
repo_url=repo,
branch="master",
branch=git_ref,
domain=domain,
python_version=running_python_version,
nuke=False
)

expected_django_version = '3.0.6'
expected_virtualenv = virtualenvs_folder / domain
expected_project_path = fake_home / domain
django_project_name = 'myproject'
Expand All @@ -60,11 +59,11 @@ def test_actually_works_against_example_repo(
'-c'
'import django; print(django.get_version())'
]).decode().strip()
assert django_version == expected_django_version
assert django_version == new_django_version

with expected_settings_path.open() as f:
lines = f.read().split('\n')
assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" in lines
assert "MEDIA_ROOT = Path(BASE_DIR / 'media')" in lines
assert "ALLOWED_HOSTS = ['mydomain.com'] # type: List[str]" in lines

assert 'base.css' in os.listdir(str(fake_home / domain / 'static/admin/css'))
Expand Down
Loading

0 comments on commit 8eb74e6

Please sign in to comment.