Ticket #20485: 20485-prewalk-fixtures-dirs.diff
File 20485-prewalk-fixtures-dirs.diff, 8.2 KB (added by , 11 years ago) |
---|
-
django/core/management/commands/loaddata.py
diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index ab9f746..cb4a61b 100644
a b from django.core.management.base import BaseCommand, CommandError 12 12 from django.core.management.color import no_style 13 13 from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS, 14 14 IntegrityError, DatabaseError) 15 from django.db.models import get_app s15 from django.db.models import get_app_paths 16 16 from django.utils.encoding import force_text 17 from django.utils.functional import cached_property 17 18 from django.utils._os import upath 19 from django.utils import six 18 20 from itertools import product 19 21 20 22 try: … … class Command(BaseCommand): 69 71 self.fixture_object_count = 0 70 72 self.models = set() 71 73 72 class SingleZipReader(zipfile.ZipFile):73 def __init__(self, *args, **kwargs):74 zipfile.ZipFile.__init__(self, *args, **kwargs)75 if settings.DEBUG:76 assert len(self.namelist()) == 1, "Zip-compressed fixtures must contain only one file."77 def read(self):78 return zipfile.ZipFile.read(self, self.namelist()[0])79 80 74 self.compression_types = { 81 75 None: open, 82 76 'gz': gzip.GzipFile, … … class Command(BaseCommand): 85 79 if has_bz2: 86 80 self.compression_types['bz2'] = bz2.BZ2File 87 81 88 app_module_paths = []89 for app in get_apps():90 if hasattr(app, '__path__'):91 # It's a 'models/' subpackage92 for path in app.__path__:93 app_module_paths.append(upath(path))94 else:95 # It's a models.py module96 app_module_paths.append(upath(app.__file__))97 98 app_fixtures = [os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths]99 100 82 with connection.constraint_checks_disabled(): 101 83 for fixture_label in fixture_labels: 102 self.load_label(fixture_label , app_fixtures)84 self.load_label(fixture_label) 103 85 104 86 # Since we disabled constraint checks, we must manually check for 105 87 # any invalid keys that might have been added … … class Command(BaseCommand): 130 112 self.stdout.write("Installed %d object(s) (of %d) from %d fixture(s)" % ( 131 113 self.loaded_object_count, self.fixture_object_count, self.fixture_count)) 132 114 133 def load_label(self, fixture_label , app_fixtures):115 def load_label(self, fixture_label): 134 116 135 117 parts = fixture_label.split('.') 136 118 … … class Command(BaseCommand): 159 141 (fixture_name, format)) 160 142 161 143 if os.path.isabs(fixture_name): 162 fixture_dirs = [fixture_name]144 dirs_candidates = {'': [fixture_name]} 163 145 else: 164 fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + ['']146 dirs_candidates = self.fixture_files_cache 165 147 166 148 label_found = False 167 for fixture_dir in fixture_dirs:168 found = self.process_dir(fixture_dir, fixture_name,149 for fixture_dir, candidates in six.iteritems(dirs_candidates): 150 found = self.process_dir(fixture_dir, candidates, fixture_name, 169 151 compression_formats, formats) 170 152 label_found = label_found or found 171 153 172 154 if fixture_name != 'initial_data' and not label_found: 173 155 warnings.warn("No fixture named '%s' found." % fixture_name) 174 156 175 def process_dir(self, fixture_dir, fixture_name, compression_formats,176 157 def process_dir(self, fixture_dir, candidates, fixture_name, 158 compression_formats, serialization_formats): 177 159 178 160 humanize = lambda dirname: "'%s'" % dirname if dirname else 'absolute path' 179 161 … … class Command(BaseCommand): 193 175 if self.verbosity >= 3: 194 176 self.stdout.write("Trying %s for %s fixture '%s'..." % \ 195 177 (humanize(fixture_dir), file_name, fixture_name)) 178 if file_name not in candidates: 179 continue 196 180 full_path = os.path.join(fixture_dir, file_name) 197 181 open_method = self.compression_types[compression_format] 198 182 try: … … class Command(BaseCommand): 250 234 (fixture_name)) 251 235 252 236 return label_found 237 238 @cached_property 239 def fixture_files_cache(self): 240 """ 241 Return a dict mapping fixture directories to a list of their files. 242 243 Fixtures directories includes the 'fixtures' subdirectories of 244 installed application and the directories in FIXTURE_DIRS. 245 246 File paths are relative to the fixture directory containing them. 247 """ 248 dirs = [] 249 for path in get_app_paths(): 250 d = os.path.join(os.path.dirname(path), 'fixtures') 251 if os.path.isdir(d): 252 dirs.append(d) 253 dirs.extend(list(settings.FIXTURE_DIRS)) 254 dirs = [os.path.abspath(os.path.realpath(d)) for d in dirs] 255 256 cache = {} 257 for d in dirs: 258 files = [] 259 for dirpath, dirnames, filenames in os.walk(d): 260 dirpath = os.path.relpath(dirpath, d) 261 if dirpath == '.': 262 dirpath = '' 263 for filename in filenames: 264 files.append(os.path.join(dirpath, filename)) 265 cache[d] = files 266 267 return cache 268 269 270 class SingleZipReader(zipfile.ZipFile): 271 272 def __init__(self, *args, **kwargs): 273 zipfile.ZipFile.__init__(self, *args, **kwargs) 274 if len(self.namelist()) != 1: 275 raise ValueError("Zip-compressed fixtures must contain one file.") 276 277 def read(self): 278 return zipfile.ZipFile.read(self, self.namelist()[0]) -
django/db/models/__init__.py
diff --git a/django/db/models/__init__.py b/django/db/models/__init__.py index 5f17229..3eac216 100644
a b 1 1 from functools import wraps 2 2 3 3 from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured 4 from django.db.models.loading import get_apps, get_app , get_models, get_model, register_models4 from django.db.models.loading import get_apps, get_app_paths, get_app, get_models, get_model, register_models 5 5 from django.db.models.query import Q 6 6 from django.db.models.expressions import F 7 7 from django.db.models.manager import Manager -
django/db/models/loading.py
diff --git a/django/db/models/loading.py b/django/db/models/loading.py index c027105..4372873 100644
a b class AppCache(object): 130 130 return self.loaded 131 131 132 132 def get_apps(self): 133 "Returns a list of all installed modules that contain models." 133 """ 134 Returns a list of all installed modules that contain models. 135 """ 134 136 self._populate() 135 137 136 138 # Ensure the returned list is always in the same order (with new apps … … class AppCache(object): 140 142 apps.sort() 141 143 return [elt[1] for elt in apps] 142 144 145 def get_app_paths(self): 146 """ 147 Returns a list of paths to all installed apps. 148 149 Useful for discovering files at conventional locations inside apps 150 (static files, templates, etc.) 151 """ 152 self._populate() 153 154 app_paths = [] 155 for app in self.get_apps(): 156 if hasattr(app, '__path__'): # models/__init__.py package 157 app_paths.extend([upath(path) for path in app.__path__]) 158 else: # models.py module 159 app_paths.append(upath(app.__file__)) 160 return app_paths 161 143 162 def get_app(self, app_label, emptyOK=False): 144 163 """ 145 164 Returns the module containing the models for the given app_label. If … … cache = AppCache() 260 279 # These methods were always module level, so are kept that way for backwards 261 280 # compatibility. 262 281 get_apps = cache.get_apps 282 get_app_paths = cache.get_app_paths 263 283 get_app = cache.get_app 264 284 get_app_errors = cache.get_app_errors 265 285 get_models = cache.get_models