2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2013-02-13 03:50:47 +08:00
|
|
|
|
2013-02-13 00:58:49 +08:00
|
|
|
import os
|
|
|
|
from subprocess import PIPE, Popen
|
2013-02-13 03:50:47 +08:00
|
|
|
import sys
|
2013-02-13 00:58:49 +08:00
|
|
|
|
2013-03-04 02:58:13 +08:00
|
|
|
from django.utils.encoding import force_text, DEFAULT_LOCALE_ENCODING
|
2013-02-13 03:50:47 +08:00
|
|
|
from django.utils import six
|
|
|
|
|
|
|
|
from .base import CommandError
|
2013-03-04 02:58:13 +08:00
|
|
|
|
2013-02-13 00:58:49 +08:00
|
|
|
|
2013-02-13 03:50:47 +08:00
|
|
|
def popen_wrapper(args, os_err_exc_type=CommandError):
|
2013-02-13 00:58:49 +08:00
|
|
|
"""
|
|
|
|
Friendly wrapper around Popen.
|
|
|
|
|
|
|
|
Returns stdout output, stderr output and OS status code.
|
|
|
|
"""
|
2013-02-13 03:50:47 +08:00
|
|
|
try:
|
|
|
|
p = Popen(args, shell=False, stdout=PIPE, stderr=PIPE,
|
|
|
|
close_fds=os.name != 'nt', universal_newlines=True)
|
|
|
|
except OSError as e:
|
2014-03-26 07:42:19 +08:00
|
|
|
strerror = force_text(e.strerror, DEFAULT_LOCALE_ENCODING,
|
|
|
|
strings_only=True)
|
2013-02-13 03:50:47 +08:00
|
|
|
six.reraise(os_err_exc_type, os_err_exc_type('Error executing %s: %s' %
|
2014-03-26 07:42:19 +08:00
|
|
|
(args[0], strerror)), sys.exc_info()[2])
|
2013-02-13 00:58:49 +08:00
|
|
|
output, errors = p.communicate()
|
2013-03-04 02:58:13 +08:00
|
|
|
return (
|
|
|
|
output,
|
|
|
|
force_text(errors, DEFAULT_LOCALE_ENCODING, strings_only=True),
|
|
|
|
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
|
|
|
"""
|
|
|
|
Organizes 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)
|
2013-02-13 03:50:47 +08:00
|
|
|
if isinstance(path, six.string_types):
|
|
|
|
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
|