def compile_message_file(path):
        """Compiles a .po file into a .mo file by path."""
        program = 'msgfmt'
        if find_command(program) is None:
            raise TranslationError(
                "Can't find %s. Make sure you have GNU gettext tools 0.15 or newer installed."
                % program)

        def _has_bom(fn):
            with open(fn, 'rb') as f:
                sample = f.read(4)
            return sample[:3] == b'\xef\xbb\xbf' or \
                sample.startswith(codecs.BOM_UTF16_LE) or \
                sample.startswith(codecs.BOM_UTF16_BE)

        if _has_bom(path):
            raise TranslationError(
                "The %s file has a BOM (Byte Order Mark). Django only supports .po files encoded in UTF-8 and without any BOM."
                % path)
        pf = os.path.splitext(path)[0]
        args = [
            program, '--check-format', '-o',
            npath(pf + '.mo'),
            npath(pf + '.po')
        ]
        output, errors, status = popen_wrapper(args)
        if status:
            if errors:
                msg = "Execution of %s failed: %s" % (program, errors)
            else:
                msg = "Execution of %s failed" % program
            raise TranslationError(msg)
Example #2
0
 def assertFileFoundOnlyNew(self, filename):
     self.clear_autoreload_caches()
     # Test uncached access
     self.assertIn(npath(filename), autoreload.gen_filenames(only_new=True))
     # Test cached access
     self.assertNotIn(npath(filename),
                      autoreload.gen_filenames(only_new=True))
Example #3
0
    def compile_messages(self, locations):
        """
        Locations is a list of tuples: [(directory, file), ...]
        """
        for i, (dirpath, f) in enumerate(locations):
            if self.verbosity > 0:
                self.stdout.write('processing file %s in %s\n' % (f, dirpath))
            po_path = os.path.join(dirpath, f)
            if has_bom(po_path):
                raise CommandError("The %s file has a BOM (Byte Order Mark). "
                                   "Django only supports .po files encoded in "
                                   "UTF-8 and without any BOM." % po_path)
            base_path = os.path.splitext(po_path)[0]

            # Check writability on first location
            if i == 0 and not is_writable(npath(base_path + '.mo')):
                self.stderr.write("The po files under %s are in a seemingly not writable location. "
                                  "mo files will not be updated/created." % dirpath)
                return

            args = [self.program] + self.program_options + ['-o',
                    npath(base_path + '.mo'), npath(base_path + '.po')]
            output, errors, status = popen_wrapper(args)
            if status:
                if errors:
                    msg = "Execution of %s failed: %s" % (self.program, errors)
                else:
                    msg = "Execution of %s failed" % self.program
                raise CommandError(msg)
def compile_messages(stderr, locale=None):
    basedirs = [os.path.join('conf', 'locale'), 'locale']
    if os.environ.get('DJANGO_SETTINGS_MODULE'):
        from django.conf import settings
        basedirs.extend(settings.LOCALE_PATHS)

    # Gather existing directories.
    basedirs = set(map(os.path.abspath, filter(os.path.isdir, basedirs)))

    if not basedirs:
        raise CommandError("This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified.")

    for basedir in basedirs:
        if locale:
            basedir = os.path.join(basedir, locale, 'LC_MESSAGES')
        for dirpath, dirnames, filenames in os.walk(basedir):
            for f in filenames:
                if f.endswith('.po'):
                    stderr.write('processing file %s in %s\n' % (f, dirpath))
                    fn = os.path.join(dirpath, f)
                    if has_bom(fn):
                        raise CommandError("The %s file has a BOM (Byte Order Mark). Django only supports .po files encoded in UTF-8 and without any BOM." % fn)
                    pf = os.path.splitext(fn)[0]
                    # Store the names of the .mo and .po files in an environment
                    # variable, rather than doing a string replacement into the
                    # command, so that we can take advantage of shell quoting, to
                    # quote any malicious characters/escaping.
                    # See http://cyberelk.net/tim/articles/cmdline/ar01s02.html
                    os.environ['djangocompilemo'] = npath(pf + '.mo')
                    os.environ['djangocompilepo'] = npath(pf + '.po')
                    if sys.platform == 'win32': # Different shell-variable syntax
                        cmd = 'msgfmt --check-format -o "%djangocompilemo%" "%djangocompilepo%"'
                    else:
                        cmd = 'msgfmt --check-format -o "$djangocompilemo" "$djangocompilepo"'
                    os.system(cmd)
    def compile_messages(self, locations):
        """
        Locations is a list of tuples: [(directory, file), ...]
        """
        for i, (dirpath, f) in enumerate(locations):
            if self.verbosity > 0:
                self.stdout.write('processing file %s in %s\n' % (f, dirpath))
            po_path = os.path.join(dirpath, f)
            if has_bom(po_path):
                raise CommandError("The %s file has a BOM (Byte Order Mark). "
                                   "Django only supports .po files encoded in "
                                   "UTF-8 and without any BOM." % po_path)
            base_path = os.path.splitext(po_path)[0]

            # Check writability on first location
            if i == 0 and not is_writable(npath(base_path + '.mo')):
                self.stderr.write(
                    "The po files under %s are in a seemingly not writable location. "
                    "mo files will not be updated/created." % dirpath)
                return

            args = [self.program] + self.program_options + [
                '-o', npath(base_path + '.mo'),
                npath(base_path + '.po')
            ]
            output, errors, status = popen_wrapper(args)
            if status:
                if errors:
                    msg = "Execution of %s failed: %s" % (self.program, errors)
                else:
                    msg = "Execution of %s failed" % self.program
                raise CommandError(msg)
Example #6
0
def _compile(stdout, locale, basedir, program):
    if locale:
        dirs = [os.path.join(basedir, l, 'LC_MESSAGES') for l in locale]
    else:
        dirs = [basedir]
    for ldir in dirs:
        for dirpath, dirnames, filenames in os.walk(ldir):
            for f in filenames:
                if not f.endswith('.po'):
                    continue
                stdout.write('processing file %s in %s\n' % (f, dirpath))
                fn = os.path.join(dirpath, f)
                if has_bom(fn):
                    raise CommandError(
                        "The %s file has a BOM (Byte Order Mark). Django only supports .po files encoded in UTF-8 and without any BOM."
                        % fn)
                pf = os.path.splitext(fn)[0]
                args = [
                    program, '--check-format', '-o',
                    npath(pf + '.mo'),
                    npath(pf + '.po')
                ]
                output, errors, status = popen_wrapper(args)
                if status:
                    if errors:
                        msg = "Execution of %s failed: %s" % (program, errors)
                    else:
                        msg = "Execution of %s failed" % program
                    raise CommandError(msg)
def compile_messages(stdout, locale=None):
    program = 'msgfmt'
    if find_command(program) is None:
        raise CommandError(
            "Can't find %s. Make sure you have GNU gettext tools 0.15 or newer installed."
            % program)

    basedirs = [os.path.join('conf', 'locale'), 'locale']
    if os.environ.get('DJANGO_SETTINGS_MODULE'):
        from django.conf import settings
        basedirs.extend(settings.LOCALE_PATHS)

    # Gather existing directories.
    basedirs = set(map(os.path.abspath, filter(os.path.isdir, basedirs)))

    if not basedirs:
        raise CommandError(
            "This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified."
        )

    for basedir in basedirs:
        if locale:
            dirs = [
                os.path.join(basedir, l, 'LC_MESSAGES')
                for l in (locale if isinstance(locale, list) else [locale])
            ]
        else:
            dirs = [basedir]
        for ldir in dirs:
            for dirpath, dirnames, filenames in os.walk(ldir):
                for f in filenames:
                    if not f.endswith('.po'):
                        continue
                    stdout.write('processing file %s in %s\n' % (f, dirpath))
                    fn = os.path.join(dirpath, f)
                    if has_bom(fn):
                        raise CommandError(
                            "The %s file has a BOM (Byte Order Mark). Django only supports .po files encoded in UTF-8 and without any BOM."
                            % fn)
                    pf = os.path.splitext(fn)[0]
                    args = [
                        program, '--check-format', '-o',
                        npath(pf + '.mo'),
                        npath(pf + '.po')
                    ]
                    output, errors, status = popen_wrapper(args)
                    if status:
                        if errors:
                            msg = "Execution of %s failed: %s" % (program,
                                                                  errors)
                        else:
                            msg = "Execution of %s failed" % program
                        raise CommandError(msg)
Example #8
0
    def test_only_new_files(self):
        """
        When calling a second time gen_filenames with only_new = True, only
        files from newly loaded modules should be given.
        """
        dirname = tempfile.mkdtemp()
        filename = os.path.join(dirname, 'test_only_new_module.py')
        self.addCleanup(shutil.rmtree, dirname)
        with open(filename, 'w'):
            pass

        # Test uncached access
        self.clear_autoreload_caches()
        filenames = set(autoreload.gen_filenames(only_new=True))
        filenames_reference = set(autoreload.gen_filenames())
        self.assertEqual(filenames, filenames_reference)

        # Test cached access: no changes
        filenames = set(autoreload.gen_filenames(only_new=True))
        self.assertEqual(filenames, set())

        # Test cached access: add a module
        with extend_sys_path(dirname):
            import_module('test_only_new_module')
        filenames = set(autoreload.gen_filenames(only_new=True))
        self.assertEqual(filenames, {npath(filename)})
Example #9
0
def load_backend(backend_name):
    """
    Return a database backend's "base" module given a fully qualified database
    backend name, or raise an error if it doesn't exist.
    """
    # This backend was renamed in Django 1.9.
    if backend_name == 'django.db.backends.postgresql_psycopg2':
        backend_name = 'django.db.backends.postgresql'

    try:
        return import_module('%s.base' % backend_name)
    except ImportError as e_user:
        # The database backend wasn't found. Display a helpful error message
        # listing all possible (built-in) database backends.
        backend_dir = os.path.join(os.path.dirname(upath(__file__)), 'backends')
        try:
            builtin_backends = [
                name for _, name, ispkg in pkgutil.iter_modules([npath(backend_dir)])
                if ispkg and name not in {'base', 'dummy', 'postgresql_psycopg2'}
            ]
        except EnvironmentError:
            builtin_backends = []
        if backend_name not in ['django.db.backends.%s' % b for b in
                                builtin_backends]:
            backend_reprs = map(repr, sorted(builtin_backends))
            error_msg = ("%r isn't an available database backend.\n"
                         "Try using 'django.db.backends.XXX', where XXX "
                         "is one of:\n    %s\nError was: %s" %
                         (backend_name, ", ".join(backend_reprs), e_user))
            raise ImproperlyConfigured(error_msg)
        else:
            # If there's some other error, this must be an error in Django
            raise
Example #10
0
    def test_unpickling_when_appregistrynotready(self):
        """
        #24007 -- Verifies that a pickled model can be unpickled without having
        to manually setup the apps registry beforehand.
        """
        script_template = """#!/usr/bin/env python
import pickle

from django.conf import settings

data = %r

settings.configure(DEBUG=False, INSTALLED_APPS=('model_regress',), SECRET_KEY = "blah")
article = pickle.loads(data)
print(article.headline)"""
        a = Article.objects.create(
            headline="Some object", pub_date=datetime.datetime.now(), article_text="This is an article"
        )

        with NamedTemporaryFile(mode="w+", suffix=".py") as script:
            script.write(script_template % pickle.dumps(a))
            script.flush()
            # A path to model_regress must be set in PYTHONPATH
            model_regress_dir = os.path.dirname(upath(__file__))
            model_regress_path = os.path.abspath(model_regress_dir)
            tests_path = os.path.split(model_regress_path)[0]
            pythonpath = os.environ.get("PYTHONPATH", "")
            pythonpath = npath(os.pathsep.join([tests_path, pythonpath]))

            with mock.patch.dict("os.environ", {"PYTHONPATH": pythonpath}):
                try:
                    result = subprocess.check_output([sys.executable, script.name])
                except subprocess.CalledProcessError:
                    self.fail("Unable to reload model pickled data")
        self.assertEqual(result.strip().decode(), "Some object")
Example #11
0
def load_backend(backend_name):
    """
    Return a database backend's "base" module given a fully qualified database
    backend name, or raise an error if it doesn't exist.
    """
    # 加载: django.db.backends.mysql.base 这个文件
    try:
        return import_module('%s.base' % backend_name)
    except ImportError as e_user:
        # The database backend wasn't found. Display a helpful error message
        # listing all possible (built-in) database backends.
        backend_dir = os.path.join(os.path.dirname(upath(__file__)),
                                   'backends')
        try:
            builtin_backends = [
                name for _, name, ispkg in pkgutil.iter_modules(
                    [npath(backend_dir)]) if ispkg
                and name not in {'base', 'dummy', 'postgresql_psycopg2'}
            ]
        except EnvironmentError:
            builtin_backends = []
        if backend_name not in [
                'django.db.backends.%s' % b for b in builtin_backends
        ]:
            backend_reprs = map(repr, sorted(builtin_backends))
            error_msg = ("%r isn't an available database backend.\n"
                         "Try using 'django.db.backends.XXX', where XXX "
                         "is one of:\n    %s\nError was: %s" %
                         (backend_name, ", ".join(backend_reprs), e_user))
            raise ImproperlyConfigured(error_msg)
        else:
            # If there's some other error, this must be an error in Django
            raise
Example #12
0
    def test_only_new_files(self):
        """
        When calling a second time gen_filenames with only_new = True, only
        files from newly loaded modules should be given.
        """
        dirname = tempfile.mkdtemp()
        filename = os.path.join(dirname, 'test_only_new_module.py')
        self.addCleanup(shutil.rmtree, dirname)
        with open(filename, 'w'):
            pass

        # Test uncached access
        self.clear_autoreload_caches()
        filenames = set(autoreload.gen_filenames(only_new=True))
        filenames_reference = set(autoreload.gen_filenames())
        self.assertEqual(filenames, filenames_reference)

        # Test cached access: no changes
        filenames = set(autoreload.gen_filenames(only_new=True))
        self.assertEqual(filenames, set())

        # Test cached access: add a module
        with extend_sys_path(dirname):
            import_module('test_only_new_module')
        filenames = set(autoreload.gen_filenames(only_new=True))
        self.assertEqual(filenames, {npath(filename)})
Example #13
0
def compile_messages(stderr, locale=None):
    basedirs = [os.path.join('conf', 'locale'), 'locale']
    if os.environ.get('DJANGO_SETTINGS_MODULE'):
        from django.conf import settings
        basedirs.extend(settings.LOCALE_PATHS)

    # Gather existing directories.
    basedirs = set(map(os.path.abspath, filter(os.path.isdir, basedirs)))

    if not basedirs:
        raise CommandError(
            "This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified."
        )

    for basedir in basedirs:
        if locale:
            dirs = [
                os.path.join(basedir, l, 'LC_MESSAGES')
                for l in (locale if isinstance(locale, list) else [locale])
            ]
        else:
            dirs = [basedir]
        for ldir in dirs:
            for dirpath, dirnames, filenames in os.walk(ldir):
                for f in filenames:
                    if not f.endswith('.po'):
                        continue
                    stderr.write('processing file %s in %s\n' % (f, dirpath))
                    fn = os.path.join(dirpath, f)
                    if has_bom(fn):
                        raise CommandError(
                            "The %s file has a BOM (Byte Order Mark). Django only supports .po files encoded in UTF-8 and without any BOM."
                            % fn)
                    pf = os.path.splitext(fn)[0]
                    # Store the names of the .mo and .po files in an environment
                    # variable, rather than doing a string replacement into the
                    # command, so that we can take advantage of shell quoting, to
                    # quote any malicious characters/escaping.
                    # See http://cyberelk.net/tim/articles/cmdline/ar01s02.html
                    os.environ['djangocompilemo'] = npath(pf + '.mo')
                    os.environ['djangocompilepo'] = npath(pf + '.po')
                    if sys.platform == 'win32':  # Different shell-variable syntax
                        cmd = 'msgfmt --check-format -o "%djangocompilemo%" "%djangocompilepo%"'
                    else:
                        cmd = 'msgfmt --check-format -o "$djangocompilemo" "$djangocompilepo"'
                    os.system(cmd)
Example #14
0
def find_commands(management_dir):
    """
    Given a path to a management directory, returns a list of all the command
    names that are available.

    Returns an empty list if no commands are defined.
    """
    command_dir = os.path.join(management_dir, 'commands')
    return [name for _, name, is_pkg in pkgutil.iter_modules([npath(command_dir)])
            if not is_pkg and not name.startswith('_')]
Example #15
0
 def test_deleted_removed(self):
     dirname = tempfile.mkdtemp()
     filename = os.path.join(dirname, 'test_deleted_removed_module.py')
     with open(filename, 'w'):
         pass
     with extend_sys_path(dirname):
         import_module('test_deleted_removed_module')
     self.assertIn(npath(filename), gen_filenames())
     os.unlink(filename)
     self.assertNotIn(filename, gen_filenames())
Example #16
0
def find_commands(management_dir):
    """
    Given a path to a management directory, returns a list of all the command
    names that are available.

    Returns an empty list if no commands are defined.
    """
    command_dir = os.path.join(management_dir, 'commands')
    return [name for _, name, is_pkg in pkgutil.iter_modules([npath(command_dir)])
            if not is_pkg and not name.startswith('_')]
Example #17
0
 def test_deleted_removed(self):
     fd, filepath = tempfile.mkstemp(dir=os.path.dirname(upath(__file__)), suffix='.py')
     try:
         _, filename = os.path.split(filepath)
         import_module('.%s' % filename.replace('.py', ''), package='utils_tests')
         self.assertIn(npath(filepath), gen_filenames())
     finally:
         os.close(fd)
         os.remove(filepath)
     self.assertNotIn(filepath, gen_filenames())
Example #18
0
 def test_deleted_removed(self):
     dirname = tempfile.mkdtemp()
     filename = os.path.join(dirname, 'test_deleted_removed_module.py')
     with open(filename, 'w'):
         pass
     with extend_sys_path(dirname):
         import_module('test_deleted_removed_module')
     self.assertIn(npath(filename), gen_filenames())
     os.unlink(filename)
     self.assertNotIn(filename, gen_filenames())
Example #19
0
 def test_deleted_removed(self):
     fd, filepath = tempfile.mkstemp(dir=os.path.dirname(upath(__file__)),
                                     suffix='.py')
     try:
         _, filename = os.path.split(filepath)
         import_module('.%s' % filename.replace('.py', ''),
                       package='utils_tests')
         self.assertIn(npath(filepath), gen_filenames())
     finally:
         os.close(fd)
         os.remove(filepath)
     self.assertNotIn(filepath, gen_filenames())
Example #20
0
def autodiscover_items(module):
    """
    Given a path to discover, auto register all items
    """
    # Workaround for a Python 3.2 bug with pkgutil.iter_modules
    module_dir = upath(module.__path__[0])
    sys.path_importer_cache.pop(module_dir, None)
    modules = [name for _, name, is_pkg in
               pkgutil.iter_modules([npath(module_dir)])
               if not is_pkg and not name.startswith('_')]
    for name in modules:
        __import__("%s.%s" % (module.__name__, name))
Example #21
0
def find_commands(management_dir):
    """
    Given a path to a management directory, returns a list of all the command
    names that are available.

    Returns an empty list if no commands are defined.
    """
    command_dir = os.path.join(management_dir, 'commands')
    # Workaround for a Python 3.2 bug with pkgutil.iter_modules
    sys.path_importer_cache.pop(command_dir, None)
    return [name for _, name, is_pkg in pkgutil.iter_modules([npath(command_dir)])
            if not is_pkg and not name.startswith('_')]
Example #22
0
def compile_messages(stdout, locale=None):
    program = 'msgfmt'
    if find_command(program) is None:
        raise CommandError("Can't find %s. Make sure you have GNU gettext tools 0.15 or newer installed." % program)

    basedirs = [os.path.join('conf', 'locale'), 'locale']
    if os.environ.get('DJANGO_SETTINGS_MODULE'):
        from django.conf import settings
        basedirs.extend(settings.LOCALE_PATHS)

    # Gather existing directories.
    basedirs = set(map(os.path.abspath, filter(os.path.isdir, basedirs)))

    if not basedirs:
        raise CommandError("This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified.")

    for basedir in basedirs:
        if locale:
            dirs = [os.path.join(basedir, l, 'LC_MESSAGES') for l in locale]
        else:
            dirs = [basedir]
        for ldir in dirs:
            for dirpath, dirnames, filenames in os.walk(ldir):
                for f in filenames:
                    if not f.endswith('.po'):
                        continue
                    stdout.write('processing file %s in %s\n' % (f, dirpath))
                    fn = os.path.join(dirpath, f)
                    if has_bom(fn):
                        raise CommandError("The %s file has a BOM (Byte Order Mark). Django only supports .po files encoded in UTF-8 and without any BOM." % fn)
                    pf = os.path.splitext(fn)[0]
                    args = [program, '--check-format', '-o', npath(pf + '.mo'), npath(pf + '.po')]
                    output, errors, status = popen_wrapper(args)
                    if status:
                        if errors:
                            msg = "Execution of %s failed: %s" % (program, errors)
                        else:
                            msg = "Execution of %s failed" % program
                        raise CommandError(msg)
Example #23
0
def gen_filenames(only_new=False):
    """
    Returns a list of filenames referenced in sys.modules and translation
    files.
    """
    # N.B. ``list(...)`` is needed, because this runs in parallel with
    # application code which might be mutating ``sys.modules``, and this will
    # fail with RuntimeError: cannot mutate dictionary while iterating
    global _cached_modules, _cached_filenames
    module_values = set(sys.modules.values())
    _cached_filenames = clean_files(_cached_filenames)
    if _cached_modules == module_values:
        # No changes in module list, short-circuit the function
        if only_new:
            return []
        else:
            return _cached_filenames + clean_files(_error_files)

    new_modules = module_values - _cached_modules
    new_filenames = clean_files([
        filename.__file__ for filename in new_modules
        if hasattr(filename, '__file__')
    ])

    if not _cached_filenames and settings.USE_I18N:
        # Add the names of the .mo files that can be generated
        # by compilemessages management command to the list of files watched.
        basedirs = [
            os.path.join(os.path.dirname(os.path.dirname(__file__)), 'conf',
                         'locale'), 'locale'
        ]
        for app_config in reversed(list(apps.get_app_configs())):
            basedirs.append(os.path.join(npath(app_config.path), 'locale'))
        basedirs.extend(settings.LOCALE_PATHS)
        basedirs = [
            os.path.abspath(basedir) for basedir in basedirs
            if os.path.isdir(basedir)
        ]
        for basedir in basedirs:
            for dirpath, dirnames, locale_filenames in os.walk(basedir):
                for filename in locale_filenames:
                    if filename.endswith('.mo'):
                        new_filenames.append(os.path.join(dirpath, filename))

    _cached_modules = _cached_modules.union(new_modules)
    _cached_filenames += new_filenames
    if only_new:
        return new_filenames + clean_files(_error_files)
    else:
        return _cached_filenames + clean_files(_error_files)
Example #24
0
def find_commands(management_dir):
    """
    Given a path to a management directory, returns a list of all the command
    names that are available.

    Returns an empty list if no commands are defined.
    """
    command_dir = os.path.join(management_dir, 'commands')
    # Workaround for a Python 3.2 bug with pkgutil.iter_modules
    sys.path_importer_cache.pop(command_dir, None)
    return [
        name for _, name, is_pkg in pkgutil.iter_modules([npath(command_dir)])
        if not is_pkg and not name.startswith('_')
    ]
    def compile_message_file(path):
        """Compiles a .po file into a .mo file by path."""
        program = 'msgfmt'
        if find_command(program) is None:
            raise TranslationError("Can't find %s. Make sure you have GNU gettext tools 0.15 or newer installed." % program)

        def _has_bom(fn):
            with open(fn, 'rb') as f:
                sample = f.read(4)
            return sample[:3] == b'\xef\xbb\xbf' or \
                sample.startswith(codecs.BOM_UTF16_LE) or \
                sample.startswith(codecs.BOM_UTF16_BE)

        if _has_bom(path):
            raise TranslationError("The %s file has a BOM (Byte Order Mark). Django only supports .po files encoded in UTF-8 and without any BOM." % path)
        pf = os.path.splitext(path)[0]
        args = [program, '--check-format', '-o', npath(pf + '.mo'), npath(pf + '.po')]
        output, errors, status = popen_wrapper(args)
        if status:
            if errors:
                msg = "Execution of %s failed: %s" % (program, errors)
            else:
                msg = "Execution of %s failed" % program
            raise TranslationError(msg)
Example #26
0
def gen_filenames(only_new=False):
    """
    Returns a list of filenames referenced in sys.modules and translation
    files.
    """
    # N.B. ``list(...)`` is needed, because this runs in parallel with
    # application code which might be mutating ``sys.modules``, and this will
    # fail with RuntimeError: cannot mutate dictionary while iterating
    global _cached_modules, _cached_filenames
    module_values = set(sys.modules.values())
    _cached_filenames = clean_files(_cached_filenames)
    if _cached_modules == module_values:
        # No changes in module list, short-circuit the function
        if only_new:
            return []
        else:
            return _cached_filenames + clean_files(_error_files)

    new_modules = module_values - _cached_modules
    new_filenames = clean_files(
        [filename.__file__ for filename in new_modules
         if hasattr(filename, '__file__')])

    if not _cached_filenames and settings.USE_I18N:
        # Add the names of the .mo files that can be generated
        # by compilemessages management command to the list of files watched.
        basedirs = [os.path.join(os.path.dirname(os.path.dirname(__file__)),
                                 'conf', 'locale'),
                    'locale']
        for app_config in reversed(list(apps.get_app_configs())):
            basedirs.append(os.path.join(npath(app_config.path), 'locale'))
        basedirs.extend(settings.LOCALE_PATHS)
        basedirs = [os.path.abspath(basedir) for basedir in basedirs
                    if os.path.isdir(basedir)]
        for basedir in basedirs:
            for dirpath, dirnames, locale_filenames in os.walk(basedir):
                for filename in locale_filenames:
                    if filename.endswith('.mo'):
                        new_filenames.append(os.path.join(dirpath, filename))

    _cached_modules = _cached_modules.union(new_modules)
    _cached_filenames += new_filenames
    if only_new:
        return new_filenames + clean_files(_error_files)
    else:
        return _cached_filenames + clean_files(_error_files)
    def run_test(self, script, args, settings_file=None, apps=None):
        base_dir = os.path.dirname(test_dir)
        # The base dir for Django's tests is one level up.
        tests_dir = os.path.dirname(os.path.dirname(__file__))
        # The base dir for Django is one level above the test dir. We don't use
        # `import django` to figure that out, so we don't pick up a Django
        # from site-packages or similar.
        django_dir = os.path.dirname(tests_dir)
        ext_backend_base_dirs = self._ext_backend_paths()

        # Define a temporary environment for the subprocess
        test_environ = os.environ.copy()
        if sys.platform.startswith('java'):
            python_path_var_name = 'JYTHONPATH'
        else:
            python_path_var_name = 'PYTHONPATH'

        old_cwd = os.getcwd()

        # Set the test environment
        if settings_file:
            test_environ['DJANGO_SETTINGS_MODULE'] = str(settings_file)
        elif 'DJANGO_SETTINGS_MODULE' in test_environ:
            del test_environ['DJANGO_SETTINGS_MODULE']
        python_path = [base_dir, django_dir, tests_dir]
        python_path.extend(ext_backend_base_dirs)
        # Use native strings for better compatibility
        test_environ[str(python_path_var_name)] = npath(
            os.pathsep.join(python_path))
        test_environ[str('PYTHONWARNINGS')] = str('')

        # Move to the test directory and run
        os.chdir(test_dir)
        out, err = subprocess.Popen([sys.executable, script] + args,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE,
                                    env=test_environ,
                                    universal_newlines=True).communicate()
        # Move back to the old working directory
        os.chdir(old_cwd)

        return out, err
    def run_test(self, script, args, settings_file=None, apps=None):
        base_dir = os.path.dirname(test_dir)
        # The base dir for Django's tests is one level up.
        tests_dir = os.path.dirname(os.path.dirname(__file__))
        # The base dir for Django is one level above the test dir. We don't use
        # `import django` to figure that out, so we don't pick up a Django
        # from site-packages or similar.
        django_dir = os.path.dirname(tests_dir)
        ext_backend_base_dirs = self._ext_backend_paths()

        # Define a temporary environment for the subprocess
        test_environ = os.environ.copy()
        if sys.platform.startswith('java'):
            python_path_var_name = 'JYTHONPATH'
        else:
            python_path_var_name = 'PYTHONPATH'

        old_cwd = os.getcwd()

        # Set the test environment
        if settings_file:
            test_environ['DJANGO_SETTINGS_MODULE'] = str(settings_file)
        elif 'DJANGO_SETTINGS_MODULE' in test_environ:
            del test_environ['DJANGO_SETTINGS_MODULE']
        python_path = [base_dir, django_dir, tests_dir]
        python_path.extend(ext_backend_base_dirs)
        # Use native strings for better compatibility
        test_environ[str(python_path_var_name)] = npath(os.pathsep.join(python_path))
        test_environ[str('PYTHONWARNINGS')] = str('')

        # Move to the test directory and run
        os.chdir(test_dir)
        out, err = subprocess.Popen([sys.executable, script] + args,
                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                env=test_environ, universal_newlines=True).communicate()
        # Move back to the old working directory
        os.chdir(old_cwd)

        return out, err
Example #29
0
    def test_unpickling_when_appregistrynotready(self):
        """
        #24007 -- Verifies that a pickled model can be unpickled without having
        to manually setup the apps registry beforehand.
        """
        script_template = """#!/usr/bin/env python
import pickle

from django.conf import settings

data = %r

settings.configure(DEBUG=False, INSTALLED_APPS=['model_regress'], SECRET_KEY = "blah")
article = pickle.loads(data)
print(article.headline)"""
        a = Article.objects.create(
            headline="Some object",
            pub_date=datetime.datetime.now(),
            article_text="This is an article",
        )

        with NamedTemporaryFile(mode='w+', suffix=".py") as script:
            script.write(script_template % pickle.dumps(a))
            script.flush()
            # A path to model_regress must be set in PYTHONPATH
            model_regress_dir = os.path.dirname(upath(__file__))
            model_regress_path = os.path.abspath(model_regress_dir)
            tests_path = os.path.split(model_regress_path)[0]
            pythonpath = os.environ.get('PYTHONPATH', '')
            pythonpath = npath(os.pathsep.join([tests_path, pythonpath]))

            with mock.patch.dict('os.environ', {'PYTHONPATH': pythonpath}):
                try:
                    result = subprocess.check_output(
                        [sys.executable, script.name])
                except subprocess.CalledProcessError:
                    self.fail("Unable to reload model pickled data")
        self.assertEqual(result.strip().decode(), "Some object")
Example #30
0
 def assertFileNotFound(self, filename):
     self.clear_autoreload_caches()
     # Test uncached access
     self.assertNotIn(npath(filename), autoreload.gen_filenames())
     # Test cached access
     self.assertNotIn(npath(filename), autoreload.gen_filenames())
Example #31
0
from __future__ import unicode_literals
Example #32
0
            return _cached_filenames + clean_files(_error_files)

    new_modules = module_values - _cached_modules
    new_filenames = clean_files(
        [filename.__file__ for filename in new_modules
         if hasattr(filename, '__file__')])

    if not _cached_filenames and settings.USE_I18N:
        # Add the names of the .mo files that can be generated
        # by compilemessages management command to the list of files watched.
        basedirs = [os.path.join(os.path.dirname(os.path.dirname(__file__)),
                                 'conf', 'locale'),
                    'locale']
        for app_config in reversed(list(apps.get_app_configs())):
<<<<<<< HEAD
            basedirs.append(os.path.join(npath(app_config.path), 'locale'))
=======
            basedirs.append(os.path.join(app_config.path, 'locale'))
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
        basedirs.extend(settings.LOCALE_PATHS)
        basedirs = [os.path.abspath(basedir) for basedir in basedirs
                    if os.path.isdir(basedir)]
        for basedir in basedirs:
            for dirpath, dirnames, locale_filenames in os.walk(basedir):
                for filename in locale_filenames:
                    if filename.endswith('.mo'):
                        new_filenames.append(os.path.join(dirpath, filename))

    _cached_modules = _cached_modules.union(new_modules)
    _cached_filenames += new_filenames
    if only_new:
from __future__ import unicode_literals
# Autoreloading launcher.
Example #35
0
 def assertFileFoundOnlyNew(self, filename):
     self.clear_autoreload_caches()
     # Test uncached access
     self.assertIn(npath(filename), autoreload.gen_filenames(only_new=True))
     # Test cached access
     self.assertNotIn(npath(filename), autoreload.gen_filenames(only_new=True))
Example #36
0
 def assertFileNotFound(self, filename):
     self.clear_autoreload_caches()
     # Test uncached access
     self.assertNotIn(npath(filename), autoreload.gen_filenames())
     # Test cached access
     self.assertNotIn(npath(filename), autoreload.gen_filenames())