Skip to content

Commit f709b52

Browse files
committed
fix #13
1 parent ab10826 commit f709b52

File tree

4 files changed

+69
-45
lines changed

4 files changed

+69
-45
lines changed

django_routines/__init__.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,19 @@ class _RoutineCommand(ABC):
7474
or for all invocations of the routine if no switches are configured.
7575
"""
7676

77+
# todo
7778
# pre_hook: t.Optional[t.Callable[[Command], t.Optional[bool]]] = None
78-
"""
79-
A function to run before the command is run. The function should take the command instance and
80-
may return True to skip the command.
81-
"""
79+
# """
80+
# A function to run before the command is run. The function should take the command instance and
81+
# may return True to skip the command.
82+
# """
8283

8384
# post_hook: t.Optional[t.Callable[[Command, t.Any], t.Any]] = None
84-
"""
85-
A function to run after the command has been run. The function should take the command instance
86-
and the result of the function which will be whatever call_command returns if run in the same
87-
process, or the result object returned by subprocess.run() if run in a subprocess.
88-
"""
85+
# """
86+
# A function to run after the command has been run. The function should take the command instance
87+
# and the result of the function which will be whatever call_command returns if run in the same
88+
# process, or the result object returned by subprocess.run() if run in a subprocess.
89+
# """
8990

9091
@property
9192
@abstractmethod

django_routines/management/commands/routine.py

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib
12
import os
23
import subprocess
34
import sys
@@ -162,6 +163,8 @@ def _call_command(self, command: ManagementCommand):
162163
if self.verbosity > 0:
163164
self.secho(command.command_str, fg="cyan")
164165
call_command(cmd, *command.command_args, **options)
166+
if command.command_name == "makemigrations":
167+
importlib.invalidate_caches()
165168
except KeyError:
166169
raise CommandError(f"Command not found: {command.command_name}")
167170

@@ -217,38 +220,11 @@ def _subprocess(self, command: RCommand):
217220
if self.verbosity > 0:
218221
self.secho(" ".join(args), fg="cyan")
219222

220-
# result = subprocess.run(args, env=os.environ.copy(), capture_output=True)
221-
# self.stdout.write(result.stdout.decode())
222-
# self.stderr.write(result.stderr.decode())
223-
224-
with subprocess.Popen(
225-
args,
226-
env=os.environ.copy(),
227-
stdout=subprocess.PIPE,
228-
stderr=subprocess.PIPE,
229-
text=True,
230-
) as proc:
231-
assert proc.stdout
232-
assert proc.stderr
233-
while True:
234-
output = proc.stdout.readline()
235-
if output:
236-
self.stdout.write(output)
237-
self.stdout.flush()
238-
error = proc.stderr.readline()
239-
if error:
240-
self.stderr.write(error)
241-
self.stderr.flush()
242-
243-
if output == "" and error == "" and proc.poll() is not None:
244-
break
245-
246-
if proc.returncode != 0:
247-
for line in proc.stderr:
248-
self.stderr.write(line)
249-
self.stderr.flush()
250-
251-
return proc.returncode
223+
# todo make this async
224+
result = subprocess.run(args, env=os.environ.copy(), capture_output=True)
225+
self.stdout.write(result.stdout.decode())
226+
self.stderr.write(result.stderr.decode())
227+
return result.returncode
252228

253229
def _list(self) -> None:
254230
"""

doc/source/changelog.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Change Log
55
v1.1.0
66
======
77

8-
* `Support function pre and post hooks to be run prior to and after given commands. <https://github.com/bckohan/django-routines/issues/9>`_
8+
* `Invalidate importlib caches if command is makemigrations. <https://github.com/bckohan/django-routines/issues/13>`_
99
* `Command type for system commands (i.e. non-management commands) to be run as subprocesses <https://github.com/bckohan/django-routines/issues/7>`_
1010
* `Option to run management commands as subprocesses instead of in the same process space. <https://github.com/bckohan/django-routines/issues/6>`_
1111

tests/pytest_routine.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,53 @@
1-
from .settings import *
1+
import os
2+
from pathlib import Path
23
from django_routines import system
34

4-
DJANGO_ROUTINES = {}
5+
USE_TZ = True
56

6-
system('test', 'pytest')
7+
8+
# Set default terminal width for help outputs - necessary for
9+
# testing help output in CI environments.
10+
os.environ["TERMINAL_WIDTH"] = "80"
11+
12+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
13+
BASE_DIR = Path(__file__).resolve().parent.parent
14+
15+
# Application definition
16+
17+
INSTALLED_APPS = [
18+
"tests.django_routines_tests",
19+
"django_routines",
20+
"django_typer",
21+
"django.contrib.auth",
22+
"django.contrib.contenttypes",
23+
"django.contrib.sessions",
24+
"django.contrib.messages",
25+
"django.contrib.staticfiles",
26+
]
27+
28+
29+
DATABASES = {
30+
"default": {
31+
"ENGINE": "django.db.backends.sqlite3",
32+
"NAME": BASE_DIR / "db.sqlite3",
33+
"TEST": {"NAME": BASE_DIR / "db.sqlite3"},
34+
}
35+
}
36+
37+
MIDDLEWARE = [
38+
"django.middleware.security.SecurityMiddleware",
39+
"django.contrib.sessions.middleware.SessionMiddleware",
40+
"django.middleware.common.CommonMiddleware",
41+
"django.middleware.csrf.CsrfViewMiddleware",
42+
"django.contrib.auth.middleware.AuthenticationMiddleware",
43+
"django.contrib.messages.middleware.MessageMiddleware",
44+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
45+
]
46+
47+
DJANGO_ROUTINES = None
48+
49+
STATIC_URL = "static/"
50+
51+
SECRET_KEY = "fake"
52+
53+
system("pytest", "pytest", "-k", "test_core", "-s")

0 commit comments

Comments
 (0)