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))
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, {filename})
def test_deleted_removed(self): _, 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(filepath, gen_filenames()) finally: os.remove(filepath) self.assertNotIn(filepath, gen_filenames())
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. """ list(gen_filenames()) from fractions import Fraction # NOQA filenames2 = list(gen_filenames(only_new=True)) self.assertEqual(len(filenames2), 1) self.assertTrue(filenames2[0].endswith('fractions.py'))
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. """ filenames1 = list(gen_filenames()) from fractions import Fraction filenames2 = list(gen_filenames(only_new=True)) self.assertEqual(len(filenames2), 1) self.assertTrue(filenames2[0].endswith('fractions.py'))
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())
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. """ list(gen_filenames()) from fractions import Fraction # NOQA filenames2 = list(gen_filenames(only_new=True)) self.assertEqual(len(filenames2), 1) self.assertTrue(filenames2[0].endswith("fractions.py")) self.assertFalse(any(f.endswith(".pyc") for f in gen_filenames()))
def test_locale_paths_setting(self): """ Test that gen_filenames also yields from LOCALE_PATHS locales. """ filenames = list(gen_filenames()) self.assertIn(os.path.join(LOCALE_PATH, 'nl', 'LC_MESSAGES', 'django.mo'), filenames)
def test_app_locales(self): """ Test that gen_filenames also yields from locale dirs in installed apps. """ filenames = list(gen_filenames()) self.assertIn( os.path.join(os.path.dirname(upath(admin.__file__)), "locale", "nl", "LC_MESSAGES", "django.mo"), filenames )
def test_django_locales(self): """ Test that gen_filenames() also yields the built-in django locale files. """ filenames = list(gen_filenames()) self.assertIn( os.path.join(os.path.dirname(conf.__file__), "locale", "nl", "LC_MESSAGES", "django.mo"), filenames )
def test_django_locales(self): """ Test that gen_filenames() also yields the built-in django locale files. """ filenames = list(gen_filenames()) self.assertIn( os.path.join(os.path.dirname(conf.__file__), 'locale', 'nl', 'LC_MESSAGES', 'django.mo'), filenames)
def test_app_locales(self): """ Test that gen_filenames also yields from locale dirs in installed apps. """ filenames = list(gen_filenames()) self.assertIn(os.path.join(os.path.dirname(admin.__file__), 'locale', 'nl', 'LC_MESSAGES', 'django.mo'), filenames)
def test_django_locales(self): """ Test that gen_filenames() also yields the built-in django locale files. """ filenames = list(gen_filenames()) self.assertIn(os.path.join(os.path.dirname(conf.__file__), 'locale', 'nl', 'LC_MESSAGES', 'django.mo'), filenames)
def test_app_locales(self): """ Test that gen_filenames also yields from locale dirs in installed apps. """ filenames = list(gen_filenames()) self.assertIn( os.path.join(os.path.dirname(admin.__file__), 'locale', 'nl', 'LC_MESSAGES', 'django.mo'), filenames)
def test_app_locales(self): """ Test that gen_filenames also yields from locale dirs in installed apps. """ with app_cache._empty(), app_cache._with_app('django.contrib.admin'): filenames = list(gen_filenames()) self.assertIn(os.path.join(os.path.dirname(admin.__file__), 'locale', 'nl', 'LC_MESSAGES', 'django.mo'), filenames)
def test_no_i18n(self): """ If i18n machinery is disabled, there is no need for watching the locale files. """ filenames = list(gen_filenames()) self.assertNotIn( os.path.join(os.path.dirname(conf.__file__), 'locale', 'nl', 'LC_MESSAGES', 'django.mo'), filenames)
def test_no_i18n(self): """ If i18n machinery is disabled, there is no need for watching the locale files. """ filenames = list(gen_filenames()) self.assertNotIn( os.path.join(os.path.dirname(upath(conf.__file__)), "locale", "nl", "LC_MESSAGES", "django.mo"), filenames )
def test_project_root_locale(self): """ Test that gen_filenames also yields from the current directory (project root). """ old_cwd = os.getcwd() os.chdir(os.path.dirname(__file__)) try: filenames = list(gen_filenames()) self.assertIn(os.path.join(LOCALE_PATH, "nl", "LC_MESSAGES", "django.mo"), filenames) finally: os.chdir(old_cwd)
def test_project_root_locale(self): """ Test that gen_filenames also yields from the current directory (project root). """ old_cwd = os.getcwd() os.chdir(os.path.dirname(__file__)) try: filenames = list(gen_filenames()) self.assertIn( os.path.join(LOCALE_PATH, 'nl', 'LC_MESSAGES', 'django.mo'), filenames) finally: os.chdir(old_cwd)
def test_django_locales(self): """ Test that gen_filenames() also yields the built-in django locale files. """ filenames = list(gen_filenames()) locales = [] basedir = os.path.join(os.path.dirname(conf.__file__), 'locale') for dirpath, dirnames, locale_filenames in os.walk(basedir): for filename in locale_filenames: if filename.endswith('.mo'): locales.append(os.path.join(dirpath, filename)) self.assertTrue(len(locales) > 10) # assume a few available locales for filename in locales: self.assertIn(filename, filenames)
def test_project_root_locale(self): """ Test that gen_filenames also yields from the current directory (project root). """ old_cwd = os.getcwd() os.chdir(os.path.dirname(__file__)) try: # Remove the current app from the app cache to guarantee that the # files will be found thanks to the current working directory. with app_cache._empty(): filenames = list(gen_filenames()) self.assertIn( os.path.join(LOCALE_PATH, 'nl', 'LC_MESSAGES', 'django.mo'), filenames) finally: os.chdir(old_cwd)
def code_changed(): global _mtimes, _win file_list = get_files_list(STATIC_DIRS, FILE_SUFFIX) for filename in chain(gen_filenames(), file_list): stat = os.stat(filename) mtime = stat.st_mtime if _win: mtime -= stat.st_ctime if filename not in _mtimes: _mtimes[filename] = mtime continue if mtime != _mtimes[filename]: _mtimes = {} try: del _error_files[_error_files.index(filename)] except ValueError: pass return I18N_MODIFIED if filename.endswith(".mo") else FILE_MODIFIED return False
def inner_run(): if self.show_startup_messages: print("Performing system checks...\n") if hasattr(self, "check"): self.check(display_num_errors=self.show_startup_messages) else: self.validate(display_num_errors=self.show_startup_messages) if HAS_MIGRATIONS: try: self.check_migrations() except ImproperlyConfigured: pass if self.show_startup_messages: print("\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)) print("Development server is running at %s" % (bind_url,)) print("Using the Werkzeug debugger (http://werkzeug.pocoo.org/)") print("Quit the server with %s." % quit_command) path = options.get("admin_media_path", "") if not path: admin_media_path = os.path.join(django.__path__[0], "contrib/admin/static/admin") if os.path.isdir(admin_media_path): path = admin_media_path else: path = os.path.join(django.__path__[0], "contrib/admin/media") handler = WSGIHandler() if USE_ADMINMEDIAHANDLER: handler = AdminMediaHandler(handler, path) if USE_STATICFILES: use_static_handler = options.get("use_static_handler", True) insecure_serving = options.get("insecure_serving", False) if use_static_handler and (settings.DEBUG or insecure_serving): handler = StaticFilesHandler(handler) if open_browser: import webbrowser webbrowser.open(bind_url) if cert_path: """ OpenSSL is needed for SSL support. This will make flakes8 throw warning since OpenSSL is not used directly, alas, this is the only way to show meaningful error messages. See: http://lucumr.pocoo.org/2011/9/21/python-import-blackbox/ for more information on python imports. """ try: import OpenSSL # NOQA except ImportError: raise CommandError( "Python OpenSSL Library is " "required to use runserver_plus with ssl support. " "Install via pip (pip install pyOpenSSL)." ) dir_path, cert_file = os.path.split(cert_path) if not dir_path: dir_path = os.getcwd() root, ext = os.path.splitext(cert_file) certfile = os.path.join(dir_path, root + ".crt") keyfile = os.path.join(dir_path, root + ".key") try: from werkzeug.serving import make_ssl_devcert if os.path.exists(certfile) and os.path.exists(keyfile): ssl_context = (certfile, keyfile) else: # Create cert, key files ourselves. ssl_context = make_ssl_devcert(os.path.join(dir_path, root), host="localhost") except ImportError: if self.show_startup_messages: print("Werkzeug version is less than 0.9, trying adhoc certificate.") ssl_context = "adhoc" else: ssl_context = None if use_reloader and settings.USE_I18N: try: from django.utils.autoreload import gen_filenames except ImportError: pass else: extra_files.extend(filter(lambda filename: filename.endswith(".mo"), gen_filenames())) run_simple( self.addr, int(self.port), DebuggedApplication(handler, True), use_reloader=use_reloader, use_debugger=True, extra_files=extra_files, reloader_interval=reloader_interval, threaded=threaded, ssl_context=ssl_context, )
def inner_run(): print("Validating models...") self.validate(display_num_errors=True) print("\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)) print("Development server is running at %s" % (bind_url, )) print("Using the Werkzeug debugger (http://werkzeug.pocoo.org/)") print("Quit the server with %s." % quit_command) path = options.get('admin_media_path', '') if not path: admin_media_path = os.path.join(django.__path__[0], 'contrib/admin/static/admin') if os.path.isdir(admin_media_path): path = admin_media_path else: path = os.path.join(django.__path__[0], 'contrib/admin/media') handler = WSGIHandler() if USE_ADMINMEDIAHANDLER: handler = AdminMediaHandler(handler, path) if USE_STATICFILES: use_static_handler = options.get('use_static_handler', True) insecure_serving = options.get('insecure_serving', False) if use_static_handler and (settings.DEBUG or insecure_serving): handler = StaticFilesHandler(handler) if open_browser: import webbrowser webbrowser.open(bind_url) if cert_path: """ OpenSSL is needed for SSL support. This will make flakes8 throw warning since OpenSSL is not used directly, alas, this is the only way to show meaningful error messages. See: http://lucumr.pocoo.org/2011/9/21/python-import-blackbox/ for more information on python imports. """ try: import OpenSSL # NOQA except ImportError: raise CommandError( "Python OpenSSL Library is " "required to use runserver_plus with ssl support. " "Install via pip (pip install pyOpenSSL).") dir_path, cert_file = os.path.split(cert_path) if not dir_path: dir_path = os.getcwd() root, ext = os.path.splitext(cert_file) certfile = os.path.join(dir_path, root + ".crt") keyfile = os.path.join(dir_path, root + ".key") try: from werkzeug.serving import make_ssl_devcert if os.path.exists(certfile) and \ os.path.exists(keyfile): ssl_context = (certfile, keyfile) else: # Create cert, key files ourselves. ssl_context = make_ssl_devcert(os.path.join( dir_path, root), host='localhost') except ImportError: print( "Werkzeug version is less than 0.9, trying adhoc certificate." ) ssl_context = "adhoc" else: ssl_context = None if use_reloader and settings.USE_I18N: try: from django.utils.autoreload import gen_filenames except ImportError: pass else: extra_files.extend( filter(lambda filename: filename.endswith('.mo'), gen_filenames())) run_simple( self.addr, int(self.port), DebuggedApplication(handler, True), use_reloader=use_reloader, use_debugger=True, extra_files=extra_files, reloader_interval=reloader_interval, threaded=threaded, ssl_context=ssl_context, )
def inner_run(self, options): import django try: from werkzeug import run_simple, DebuggedApplication from werkzeug.serving import WSGIRequestHandler as _WSGIRequestHandler # Set colored output if settings.DEBUG: try: set_werkzeug_log_color() except Exception: # We are dealing with some internals, anything could go wrong if self.show_startup_messages: print("Wrapping internal werkzeug logger for color highlighting has failed!") pass except ImportError: raise CommandError("Werkzeug is required to use runserver_plus. Please visit http://werkzeug.pocoo.org/ or install via pip. (pip install Werkzeug)") class WSGIRequestHandler(_WSGIRequestHandler): def make_environ(self): environ = super(WSGIRequestHandler, self).make_environ() if not options.get('keep_meta_shutdown_func'): del environ['werkzeug.server.shutdown'] return environ threaded = options.get('threaded', True) use_reloader = options.get('use_reloader', True) open_browser = options.get('open_browser', False) cert_path = options.get("cert_path") quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C' extra_files = options.get('extra_files', None) or [] reloader_interval = options.get('reloader_interval', 1) reloader_type = options.get('reloader_type', 'auto') self.nopin = options.get('nopin', False) if self.show_startup_messages: print("Performing system checks...\n") if hasattr(self, 'check'): self.check(display_num_errors=self.show_startup_messages) else: self.validate(display_num_errors=self.show_startup_messages) try: self.check_migrations() except ImproperlyConfigured: pass handler = get_internal_wsgi_application() if USE_STATICFILES: use_static_handler = options.get('use_static_handler', True) insecure_serving = options.get('insecure_serving', False) if use_static_handler and (settings.DEBUG or insecure_serving): handler = StaticFilesHandler(handler) if cert_path: """ OpenSSL is needed for SSL support. This will make flakes8 throw warning since OpenSSL is not used directly, alas, this is the only way to show meaningful error messages. See: http://lucumr.pocoo.org/2011/9/21/python-import-blackbox/ for more information on python imports. """ try: import OpenSSL # NOQA except ImportError: raise CommandError("Python OpenSSL Library is " "required to use runserver_plus with ssl support. " "Install via pip (pip install pyOpenSSL).") dir_path, cert_file = os.path.split(cert_path) if not dir_path: dir_path = os.getcwd() root, ext = os.path.splitext(cert_file) certfile = os.path.join(dir_path, root + ".crt") keyfile = os.path.join(dir_path, root + ".key") try: from werkzeug.serving import make_ssl_devcert if os.path.exists(certfile) and \ os.path.exists(keyfile): ssl_context = (certfile, keyfile) else: # Create cert, key files ourselves. ssl_context = make_ssl_devcert( os.path.join(dir_path, root), host='localhost') except ImportError: if self.show_startup_messages: print("Werkzeug version is less than 0.9, trying adhoc certificate.") ssl_context = "adhoc" else: ssl_context = None bind_url = "%s://%s:%s/" % ( "https" if ssl_context else "http", self.addr if not self._raw_ipv6 else '[%s]' % self.addr, self.port) if self.show_startup_messages: print("\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)) print("Development server is running at %s" % (bind_url,)) print("Using the Werkzeug debugger (http://werkzeug.pocoo.org/)") print("Quit the server with %s." % quit_command) if open_browser: import webbrowser webbrowser.open(bind_url) if use_reloader and settings.USE_I18N: extra_files.extend(filter(lambda filename: filename.endswith('.mo'), gen_filenames())) # Werkzeug needs to be clued in its the main instance if running # without reloader or else it won't show key. # https://git.io/vVIgo if not use_reloader: os.environ['WERKZEUG_RUN_MAIN'] = 'true' # Don't run a second instance of the debugger / reloader # See also: https://github.com/django-extensions/django-extensions/issues/832 if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': if self.nopin: os.environ['WERKZEUG_DEBUG_PIN'] = 'off' handler = DebuggedApplication(handler, True) run_simple( self.addr, int(self.port), handler, use_reloader=use_reloader, use_debugger=True, extra_files=extra_files, reloader_interval=reloader_interval, reloader_type=reloader_type, threaded=threaded, request_handler=WSGIRequestHandler, ssl_context=ssl_context, )
def assertFileNotFound(self, filename): self.clear_autoreload_caches() # Test uncached access self.assertNotIn(filename, autoreload.gen_filenames()) # Test cached access self.assertNotIn(filename, autoreload.gen_filenames())
def assertFileFoundOnlyNew(self, filename): self.clear_autoreload_caches() # Test uncached access self.assertIn(filename, autoreload.gen_filenames(only_new=True)) # Test cached access self.assertNotIn(filename, autoreload.gen_filenames(only_new=True))
def test_paths_are_native_strings(self): for filename in autoreload.gen_filenames(): self.assertIsInstance(filename, str)