Ticket #596: django_as_egg_2.patch
File django_as_egg_2.patch, 8.9 KB (added by , 19 years ago) |
---|
-
django/utils/autoreload.py
37 37 mtimes = {} 38 38 while RUN_RELOADER: 39 39 for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())) + reloadFiles: 40 if not os.path.exists(filename): continue # file might be in an egg, so it can't be reloaded 40 41 if filename.endswith(".pyc"): 41 42 filename = filename[:-1] 42 43 mtime = os.stat(filename).st_mtime -
django/conf/global_settings.py
60 60 # Extension on all templates. 61 61 TEMPLATE_FILE_EXTENSION = '.html' 62 62 63 # Callables which know how to import template sources from various 64 # sources; the expected interface is callable(name, [dirs]), where dirs 65 # is a list of directories to search instead of TEMPLATE_DIRS. 66 TEMPLATE_SOURCE_LOADERS = ( 67 'django.core.template_file.load_template_source', 68 'django.core.template_eggs.load_template_source', 69 ) 70 71 63 72 # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a 64 73 # trailing slash. 65 74 # Examples: "http://foo.com/media/", "/media/". -
django/core/template_loader.py
1 1 "Wrapper for loading templates from storage of some sort (e.g. files or db)" 2 2 import template 3 from template_file import load_template_source4 3 4 from django.conf.settings import TEMPLATE_SOURCE_LOADERS 5 from django.core import exceptions 6 7 template_source_loaders = [] 8 for path in TEMPLATE_SOURCE_LOADERS: 9 i = path.rfind('.') 10 module, attr = path[:i], path[i+1:] 11 try: 12 mod = __import__(module, globals(), locals(), [attr]) 13 except ImportError, e: 14 raise exceptions.ImproperlyConfigured, 'Error importing template_source_loader %s: "%s"' % (module, e) 15 try: 16 template_source_loaders.append(getattr(mod, attr)) 17 except AttributeError: 18 raise exceptions.ImproperlyConfigured, 'Module "%s" does not define a "%s" callable template_source_loader' % (module, attr) 19 20 def load_template_source(name, dirs=None): 21 for loader in template_source_loaders: 22 try: 23 return loader(name, dirs) 24 except template.TemplateDoesNotExist: 25 pass 26 raise template.TemplateDoesNotExist, name 27 28 5 29 class ExtendsError(Exception): 6 30 pass 7 31 … … 24 48 Loads the given template_name and renders it with the given dictionary as 25 49 context. The template_name may be a string to load a single template using 26 50 get_template, or it may be a tuple to use select_template to find one of 27 the templates in the list. Returns a string. 51 the templates in the list. Returns a string. 28 52 """ 29 53 dictionary = dictionary or {} 30 54 if isinstance(template_name, (list, tuple)): -
django/core/servers/basehttp.py
616 616 617 617 # Find the admin file and serve it up, if it exists and is readable. 618 618 relative_url = environ['PATH_INFO'][len(self.media_url):] 619 file_path = os.path.join(self.media_dir, relative_url) 620 if not os.path.exists(file_path): 619 620 # Nasty hack to see if we're running in an egg 621 # We can't just use resource_* as we do when setting up, because 622 # the user might have set the media_dir to be something outside 623 # the egg, and so resource_* won't find it. So, check for .egg in media_dir, 624 # and if it's there then use resource_* to get the resources. 625 if self.media_dir.find('.egg') == -1: 626 MEDIA_PATH_IS_REAL = True 627 file_path = os.path.join(self.media_dir, relative_url) 628 path_exists = os.path.exists(file_path) 629 else: 630 from pkg_resources import resource_exists, resource_stream 631 import django 632 MEDIA_PATH_IS_REAL = False 633 file_path = self.media_dir[len(django.__path__[0]):] + '/' + relative_url 634 path_exists = resource_exists('django',file_path) 635 636 if not path_exists: 621 637 status = '404 NOT FOUND' 622 638 headers = {'Content-type': 'text/plain'} 623 639 output = ['Page not found: %s' % file_path] 624 640 else: 625 641 try: 626 fp = open(file_path, 'rb') 642 if MEDIA_PATH_IS_REAL: 643 fp = open(file_path, 'rb') 644 else: 645 fp = resource_stream('django',file_path) 627 646 except IOError: 628 647 status = '401 UNAUTHORIZED' 629 648 headers = {'Content-type': 'text/plain'} -
django/core/management.py
3 3 4 4 import django 5 5 import os, re, sys 6 try: 7 from pkg_resources import resource_listdir, resource_isdir, resource_stream 8 OK_IN_EGG = True 9 except: 10 OK_IN_EGG = False 6 11 7 12 MODULE_TEMPLATE = ''' {%% if perms.%(app)s.%(addperm)s or perms.%(app)s.%(changeperm)s %%} 8 13 <tr> … … 353 358 except OSError, e: 354 359 sys.stderr.write("Error: %s\n" % e) 355 360 sys.exit(1) 356 template_dir = PROJECT_TEMPLATE_DIR % app_or_project 357 for d, subdirs, files in os.walk(template_dir): 361 if OK_IN_EGG: 362 # Use setuptools.resource_* to get the resources, which should work whether 363 # django is installed as a directory or as an egg 364 template_dir = (PROJECT_TEMPLATE_DIR % app_or_project)[len(django.__path__[0])+1:] 365 print template_dir 366 walklist = _walk_pkgresources_helper(template_dir) 367 else: 368 # Use django.__path__, which is the installation directory 369 template_dir = PROJECT_TEMPLATE_DIR % app_or_project 370 walklist = os.walk(template_dir) 371 for d, subdirs, files in walklist: 358 372 relative_dir = d[len(template_dir)+1:].replace('%s_name' % app_or_project, name) 359 373 if relative_dir: 360 374 os.mkdir(os.path.join(top_dir, relative_dir)) … … 364 378 for f in files: 365 379 if f.endswith('.pyc'): 366 380 continue 367 fp_old = open(os.path.join(d, f), 'r') 381 if OK_IN_EGG: 382 fp_old = resource_stream('django',d + '/' + f) 383 else: 384 fp_old = open(os.path.join(d, f), 'r') 368 385 fp_new = open(os.path.join(top_dir, relative_dir, f.replace('%s_name' % app_or_project, name)), 'w') 369 386 fp_new.write(fp_old.read().replace('{{ %s_name }}' % app_or_project, name).replace('{{ %s_name }}' % other, other_name)) 370 387 fp_old.close() 371 388 fp_new.close() 372 389 390 def _walk_pkgresources_helper(top, topdown=True, onerror=None): 391 """An equivalent to os.walk but using the setuptools resource_* APIs, in order 392 to allow walking over directories inside egg files.""" 393 try: 394 names = resource_listdir('django',top) 395 except: 396 # Should handle an error callback here, but we don't. 397 return 398 dirs, nondirs = [], [] 399 for name in names: 400 if resource_isdir('django',top + '/' + name): # note, / separate, not os.path.join 401 dirs.append(name) 402 else: 403 nondirs.append(name) 404 405 if topdown: 406 yield top, dirs, nondirs 407 for name in dirs: 408 path = top + '/' + name 409 for x in _walk_pkgresources_helper(path, topdown, onerror): 410 yield x 411 if not topdown: 412 yield top, dirs, nondirs 413 373 414 def startproject(project_name, directory): 374 415 "Creates a Django project for the given project_name in the given directory." 375 416 from random import choice … … 379 420 admin_settings_file = os.path.join(directory, project_name, 'settings/admin.py') 380 421 settings_contents = open(admin_settings_file, 'r').read() 381 422 fp = open(admin_settings_file, 'w') 423 # Nasty hack to see if we're running in an egg 424 # If we're running as an egg, then the admin template path must be 425 # a path that's *relative* to the Django install dir. If we're not, 426 # then it must be an *absolute* path. So if .egg is in django.__path__[0], 427 # make the path relative. 428 admin_template_dir_to_use = ADMIN_TEMPLATE_DIR 429 if admin_template_dir_to_use.find('.egg') == -1: 430 admin_template_dir_to_use = admin_template_dir_to_use[len(django.__path__[0]):] 382 431 settings_contents = re.sub(r'(?s)\b(TEMPLATE_DIRS\s*=\s*\()(.*?)\)', "\\1\n r%r,\\2)" % ADMIN_TEMPLATE_DIR, settings_contents) 383 432 fp.write(settings_contents) 384 433 fp.close()