django1/django/core/management/utils.py

46 lines
1.5 KiB
Python
Raw Normal View History

import os
from subprocess import PIPE, Popen
from django.utils.encoding import force_text, DEFAULT_LOCALE_ENCODING
def popen_wrapper(args):
"""
Friendly wrapper around Popen.
Returns stdout output, stderr output and OS status code.
"""
p = Popen(args, shell=False, stdout=PIPE, stderr=PIPE,
close_fds=os.name != 'nt', universal_newlines=True)
output, errors = p.communicate()
return (
output,
force_text(errors, DEFAULT_LOCALE_ENCODING, strings_only=True),
p.returncode
)
def handle_extensions(extensions=('html',), ignored=('py',)):
"""
Organizes multiple extensions that are separated with commas or passed by
using --extension/-e multiple times. Note that the .py extension is ignored
here because of the way non-*.py files are handled in make_messages() (they
are copied to file.ext.py files to trick xgettext to parse them as Python
files).
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'])
set(['.html', '.js'])
>>> handle_extensions(['.html, txt,.tpl'])
set(['.html', '.tpl', '.txt'])
"""
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]
return set([x for x in ext_list if x.strip('.') not in ignored])