2019-01-28 03:30:06 +08:00
|
|
|
import fnmatch
|
2013-02-13 00:58:49 +08:00
|
|
|
import os
|
2019-01-28 03:30:06 +08:00
|
|
|
from pathlib import Path
|
2021-09-25 16:18:25 +08:00
|
|
|
from subprocess import run
|
2013-02-13 00:58:49 +08:00
|
|
|
|
2016-05-17 14:52:01 +08:00
|
|
|
from django.apps import apps as installed_apps
|
2015-04-18 16:09:41 +08:00
|
|
|
from django.utils.crypto import get_random_string
|
2018-01-31 23:35:08 +08:00
|
|
|
from django.utils.encoding import DEFAULT_LOCALE_ENCODING
|
2013-02-13 03:50:47 +08:00
|
|
|
|
2018-08-17 03:49:03 +08:00
|
|
|
from .base import CommandError, CommandParser
|
2013-03-04 02:58:13 +08:00
|
|
|
|
2013-02-13 00:58:49 +08:00
|
|
|
|
2018-01-31 23:02:01 +08:00
|
|
|
def popen_wrapper(args, stdout_encoding='utf-8'):
|
2013-02-13 00:58:49 +08:00
|
|
|
"""
|
|
|
|
Friendly wrapper around Popen.
|
|
|
|
|
2017-01-26 03:02:33 +08:00
|
|
|
Return stdout output, stderr output, and OS status code.
|
2013-02-13 00:58:49 +08:00
|
|
|
"""
|
2013-02-13 03:50:47 +08:00
|
|
|
try:
|
2021-09-25 16:18:25 +08:00
|
|
|
p = run(args, capture_output=True, close_fds=os.name != 'nt')
|
2017-01-08 03:13:29 +08:00
|
|
|
except OSError as err:
|
2018-01-31 23:02:01 +08:00
|
|
|
raise CommandError('Error executing %s' % args[0]) from err
|
2013-03-04 02:58:13 +08:00
|
|
|
return (
|
2019-08-23 16:53:36 +08:00
|
|
|
p.stdout.decode(stdout_encoding),
|
|
|
|
p.stderr.decode(DEFAULT_LOCALE_ENCODING, errors='replace'),
|
2013-03-04 02:58:13 +08:00
|
|
|
p.returncode
|
|
|
|
)
|
|
|
|
|
2013-02-27 09:07:22 +08:00
|
|
|
|
2014-11-17 16:24:56 +08:00
|
|
|
def handle_extensions(extensions):
|
2013-02-27 09:07:22 +08:00
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Organize multiple extensions that are separated with commas or passed by
|
2014-11-17 16:24:56 +08:00
|
|
|
using --extension/-e multiple times.
|
2013-02-27 09:07:22 +08:00
|
|
|
|
|
|
|
For example: running 'django-admin makemessages -e js,txt -e xhtml -a'
|
|
|
|
would result in an extension list: ['.js', '.txt', '.xhtml']
|
|
|
|
|
|
|
|
>>> handle_extensions(['.html', 'html,js,py,py,py,.py', 'py,.py'])
|
2014-11-17 16:24:56 +08:00
|
|
|
{'.html', '.js', '.py'}
|
2013-02-27 09:07:22 +08:00
|
|
|
>>> handle_extensions(['.html, txt,.tpl'])
|
2014-09-26 20:31:50 +08:00
|
|
|
{'.html', '.tpl', '.txt'}
|
2013-02-27 09:07:22 +08:00
|
|
|
"""
|
|
|
|
ext_list = []
|
|
|
|
for ext in extensions:
|
|
|
|
ext_list.extend(ext.replace(' ', '').split(','))
|
|
|
|
for i, ext in enumerate(ext_list):
|
|
|
|
if not ext.startswith('.'):
|
|
|
|
ext_list[i] = '.%s' % ext_list[i]
|
2014-11-17 16:24:56 +08:00
|
|
|
return set(ext_list)
|
2013-02-13 03:50:47 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2013-02-13 03:50:47 +08:00
|
|
|
def find_command(cmd, path=None, pathext=None):
|
|
|
|
if path is None:
|
2014-03-22 23:52:05 +08:00
|
|
|
path = os.environ.get('PATH', '').split(os.pathsep)
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(path, str):
|
2013-02-13 03:50:47 +08:00
|
|
|
path = [path]
|
|
|
|
# check if there are funny path extensions for executables, e.g. Windows
|
|
|
|
if pathext is None:
|
|
|
|
pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD').split(os.pathsep)
|
|
|
|
# don't use extensions if the command ends with one of them
|
|
|
|
for ext in pathext:
|
|
|
|
if cmd.endswith(ext):
|
|
|
|
pathext = ['']
|
|
|
|
break
|
|
|
|
# check if we find the command on PATH
|
|
|
|
for p in path:
|
|
|
|
f = os.path.join(p, cmd)
|
|
|
|
if os.path.isfile(f):
|
|
|
|
return f
|
|
|
|
for ext in pathext:
|
|
|
|
fext = f + ext
|
|
|
|
if os.path.isfile(fext):
|
|
|
|
return fext
|
|
|
|
return None
|
2015-04-18 16:09:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_random_secret_key():
|
|
|
|
"""
|
|
|
|
Return a 50 character random string usable as a SECRET_KEY setting value.
|
|
|
|
"""
|
|
|
|
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
|
|
|
|
return get_random_string(50, chars)
|
2016-05-17 14:52:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def parse_apps_and_model_labels(labels):
|
|
|
|
"""
|
|
|
|
Parse a list of "app_label.ModelName" or "app_label" strings into actual
|
|
|
|
objects and return a two-element tuple:
|
|
|
|
(set of model classes, set of app_configs).
|
|
|
|
Raise a CommandError if some specified models or apps don't exist.
|
|
|
|
"""
|
|
|
|
apps = set()
|
|
|
|
models = set()
|
|
|
|
|
|
|
|
for label in labels:
|
|
|
|
if '.' in label:
|
|
|
|
try:
|
|
|
|
model = installed_apps.get_model(label)
|
|
|
|
except LookupError:
|
|
|
|
raise CommandError('Unknown model: %s' % label)
|
|
|
|
models.add(model)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
app_config = installed_apps.get_app_config(label)
|
|
|
|
except LookupError as e:
|
|
|
|
raise CommandError(str(e))
|
|
|
|
apps.add(app_config)
|
|
|
|
|
|
|
|
return models, apps
|
2018-08-17 03:49:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_command_line_option(argv, option):
|
|
|
|
"""
|
|
|
|
Return the value of a command line option (which should include leading
|
2019-08-12 18:53:36 +08:00
|
|
|
dashes, e.g. '--testrunner') from an argument list. Return None if the
|
2018-08-17 03:49:03 +08:00
|
|
|
option wasn't passed or if the argument list couldn't be parsed.
|
|
|
|
"""
|
|
|
|
parser = CommandParser(add_help=False, allow_abbrev=False)
|
|
|
|
parser.add_argument(option, dest='value')
|
|
|
|
try:
|
|
|
|
options, _ = parser.parse_known_args(argv[2:])
|
|
|
|
except CommandError:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return options.value
|
2019-01-28 03:30:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
def normalize_path_patterns(patterns):
|
|
|
|
"""Normalize an iterable of glob style patterns based on OS."""
|
|
|
|
patterns = [os.path.normcase(p) for p in patterns]
|
|
|
|
dir_suffixes = {'%s*' % path_sep for path_sep in {'/', os.sep}}
|
|
|
|
norm_patterns = []
|
|
|
|
for pattern in patterns:
|
|
|
|
for dir_suffix in dir_suffixes:
|
|
|
|
if pattern.endswith(dir_suffix):
|
|
|
|
norm_patterns.append(pattern[:-len(dir_suffix)])
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
norm_patterns.append(pattern)
|
|
|
|
return norm_patterns
|
|
|
|
|
|
|
|
|
|
|
|
def is_ignored_path(path, ignore_patterns):
|
|
|
|
"""
|
|
|
|
Check if the given path should be ignored or not based on matching
|
|
|
|
one of the glob style `ignore_patterns`.
|
|
|
|
"""
|
|
|
|
path = Path(path)
|
|
|
|
|
|
|
|
def ignore(pattern):
|
|
|
|
return fnmatch.fnmatchcase(path.name, pattern) or fnmatch.fnmatchcase(str(path), pattern)
|
|
|
|
|
|
|
|
return any(ignore(pattern) for pattern in normalize_path_patterns(ignore_patterns))
|