Skip to content

Instantly share code, notes, and snippets.

@kalsky
Created June 18, 2022 04:20
Show Gist options
  • Save kalsky/0cba3618452238f3b00354a90d2a51e5 to your computer and use it in GitHub Desktop.
Save kalsky/0cba3618452238f3b00354a90d2a51e5 to your computer and use it in GitHub Desktop.
some shortcuts I use with manage.py
#!/usr/bin/env python
import os
import sys
import logging
# disable logging during tests
if 'test' in sys.argv:
logging.disable(logging.CRITICAL)
# add support for --time option to measure the time needed to execute each Test Class
if 'test' in sys.argv and '--time' in sys.argv:
sys.argv.remove('--time')
import time
from unittest import TestCase
@classmethod
def setUpClass(cls):
print("\n\033[95m%s.%s started\033[0m" % (cls.__module__, cls.__name__))
cls.class_start_time = time.time()
@classmethod
def tearDownClass(cls):
print("\n\033[94m%s.%s finished in %.3f seconds\033[0m" % (cls.__module__, cls.__name__, time.time() - cls.class_start_time))
TestCase.setUpClass = setUpClass
TestCase.tearDownClass = tearDownClass
# shortcuts
if 'cs' in sys.argv:
sys.argv.remove('cs')
sys.argv.append('collectstatic')
sys.argv.append('--noinput')
print(sys.argv)
if 'run' in sys.argv:
sys.argv.remove('run')
sys.argv.append('runserver')
print(sys.argv)
if 'mm' in sys.argv:
sys.argv.remove('mm')
sys.argv.append('makemigrations')
print(sys.argv)
if 'mig' in sys.argv:
sys.argv.remove('mig')
sys.argv.append('migrate')
print(sys.argv)
if 'ta' in sys.argv:
logging.disable(logging.CRITICAL)
sys.stdout = open(os.devnull, 'w')
sys.argv.remove('ta')
sys.argv.insert(1, 'test')
sys.argv.append('--keepdb')
sys.argv.append('--settings=my_project.settings_tests')
print(sys.argv)
if 'tac' in sys.argv:
logging.disable(logging.CRITICAL)
sys.stdout = open(os.devnull, 'w')
sys.argv.remove('tac')
sys.argv.insert(1, 'test')
sys.argv.append('--settings=my_project.settings_tests')
sys.argv.append('--noinput')
print(sys.argv)
if 'ta+p' in sys.argv:
logging.disable(logging.CRITICAL)
sys.argv.remove('ta+p')
sys.argv.insert(1, 'test')
sys.argv.append('--keepdb')
sys.argv.append('--settings=my_project.settings_tests')
print(sys.argv)
if 'dtf' in sys.argv:
logging.disable(logging.CRITICAL)
sys.argv.remove('dtf')
sys.argv.insert(1, 'test')
f = sys.argv[2].replace('.py', '')
f = '.'.join(f.split('/')[1:])
sys.argv[2] = f
sys.argv.append('--keepdb')
sys.argv.append('--settings=my_project.settings_tests')
print(sys.argv)
if 'tap' in sys.argv:
logging.disable(logging.CRITICAL)
sys.stdout = open(os.devnull, 'w')
sys.argv.remove('tap')
sys.argv.insert(1, 'test')
sys.argv.append('--keepdb')
sys.argv.append('--parallel')
sys.argv.append('3')
sys.argv.append('--settings=my_project.settings_tests')
print(sys.argv)
if 'tap+p' in sys.argv:
logging.disable(logging.CRITICAL)
sys.argv.remove('tap+p')
sys.argv.insert(1, 'test')
sys.argv.append('--keepdb')
sys.argv.append('--parallel')
sys.argv.append('3')
sys.argv.append('--settings=my_project.settings_tests')
print(sys.argv)
if 'tapc' in sys.argv:
logging.disable(logging.CRITICAL)
sys.stdout = open(os.devnull, 'w')
sys.argv.remove('tapc')
sys.argv.insert(1, 'test')
sys.argv.append('--parallel')
sys.argv.append('3')
sys.argv.append('--noinput')
sys.argv.append('--settings=my_project.settings_tests')
print(sys.argv)
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')
os.environ.setdefault('SKIP_GEVENT_PATCH', 'True')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment