def migrate(ctx, make=False, app=None, runserver=False):
"""
Run manage.py makemigrations/migrate commands.
"""
app_suffix = ' %s' % app if app else ''
if make or io.yn_input('Execute makemigrations? ') == 'yes':
run("python src/manage.py makemigrations" + app_suffix)
run("python src/manage.py migrate" + app_suffix)
if runserver or io.yn_input('Execute runserver? ') == 'yes':
run('python src/manage.py runserver')
python类run()的实例源码
def run(ctx, bind='localhost', port=8000):
"""
Starts the development server for local testing.
"""
tail = ''
if bind and port:
tail = '%s:%s' % (bind, port)
elif bind:
tail = str(bind)
elif port:
tail = str(port)
django_manage('runserver' + tail)
def project_root():
"""
Return the project root.
"""
try:
return get_option('django', 'project_root')
except ValueError:
pass
# No configuration was found. We need to search for a django project in the
# package tree. The first option is that the project adopts the
# python-boilerplate layout for a django project:
pyname = get_option('options', 'pyname')
if os.path.exists(os.path.join('src', pyname, 'site')):
return pyname + '.site'
# Secondly, it might be a layout for a django app. In this case, the
# project root points to the test project inside the "tests" folder
if os.path.exists(os.path.join('src', pyname, 'tests', 'site')):
return pyname + '.tests.site'
# Everything fails! Quit!
raise RuntimeError(
'Could not determine the project root. Is this a really django '
'project? Please run `python-boilerplate django-project` or '
'`python-boilerplate django-app` in order to create a project '
'structure.'
)
def build(ctx, no_docs=False):
"""
Build python package and docs.
"""
util.add_src_to_python_path()
run("python setup.py build")
if not no_docs:
run("python setup.py build_sphinx")
def get_current_schema(config):
try:
cqlsh = run_cqlsh(config, command="DESCRIBE " + config['keyspace'])
out = run(cqlsh, hide='stdout')
except Exception as e:
click.secho("Unable to get the current DB schema: {}".format(e), fg='red')
sys.exit()
return out.stdout
def verun(cmd):
"""Runs a command inside the virtual environment without
invoking it
Args:
cmd(str): Command to be run in the virtual env
Returns:
None
"""
run('pew in {0} {1}'.format(package_name(), cmd))
def new():
"""Creates a new virtual environment with the package name.
"""
run('pew new --dont-activate --python={0} '
'{1}'.format(python_bin, package_name()))
verun('pip install --upgrade wheel')
verun('pip install --upgrade pip')
def remove():
"""Removes the virtual environment with the package name.
"""
run('pew rm {0}'.format(package_name()))
def bash(cmd):
"""Execute command using /bin/bash instead of /bin/sh."""
return run('/bin/bash -c "{0}"'.format(cmd))
def mkdir(*paths):
"""Create directories and any parent directory that doesn't exist"""
for path in paths:
run('mkdir -p {0}'.format(path))
def readout(cmd):
"""A command for getting the package name from
setup.py file
Args:
cmd (str): 'python setup.py --name'
Returns:
str: package name
"""
return run(cmd, hide='out').stdout.strip()
def lint():
"""check style with flake8"""
run("flake8 {PROJECT_NAME}/ tests/".format(PROJECT_NAME=PROJECT_NAME))
def test():
run("py.test")
def test_all():
"""run tests on every Python version with tox"""
run("tox")
def check():
"""Check setup"""
run("python setup.py --no-user-cfg --verbose check --metadata --restructuredtext --strict")
def coverage():
"""check code coverage quickly with the default Python"""
run("coverage run --source {PROJECT_NAME} -m py.test".format(PROJECT_NAME=PROJECT_NAME))
run("coverage report -m")
run("coverage html")
if sys.stdout.isatty():
# Running in a real terminal
webbrowser.open('file://' + os.path.realpath("htmlcov/index.html"), new=2)
def build():
"""build package"""
run("python setup.py build")
run("python setup.py sdist")
run("python setup.py bdist_wheel")
def publish():
"""publish package"""
warnings.warn("Deprecated", DeprecationWarning, stacklevel=2)
check()
run('python setup.py sdist upload -r pypi') # Use python setup.py REGISTER
run('python setup.py bdist_wheel upload -r pypi')
def publish_twine():
"""publish package"""
check()
run('twine upload dist/* --skip-existing')
def publish_test():
"""publish package"""
check()
run('python setup.py sdist upload -r https://testpypi.python.org/pypi') # Use python setup.py REGISTER
run('python setup.py bdist_wheel upload -r https://testpypi.python.org/pypi')