示例#1
0
 def test_override_staticfiles_finders(self):
     """
     Overriding the STATICFILES_FINDERS setting should be reflected in
     the return value of django.contrib.staticfiles.finders.get_finders.
     """
     current = get_finders()
     self.assertGreater(len(list(current)), 1)
     finders = ['django.contrib.staticfiles.finders.FileSystemFinder']
     with self.settings(STATICFILES_FINDERS=finders):
         self.assertEqual(len(list(get_finders())), len(finders))
示例#2
0
    def handle_noargs(self, **options):
        self.set_options(**options)

        found_files = SortedDict()
        manifest = ConfiguredStaticFilesManifest()
        manifest.clear()

        ignore_patterns = getattr(settings, 'ECSTATIC_MANIFEST_EXCLUDES', [])

        for finder in finders.get_finders():
            for path, storage in finder.list(ignore_patterns):
                # Prefix the relative path if the source storage contains it
                if getattr(storage, 'prefix', None):
                    prefixed_path = os.path.join(storage.prefix, path)
                else:
                    prefixed_path = path

                if prefixed_path not in found_files:
                    found_files[prefixed_path] = path

        for path in found_files.values() + settings.ECSTATIC_MANIFEST_EXTRAS:
            try:
                generate_url = self.storage.generate_url
            except AttributeError:
                raise AttributeError('%s doesn\'t define a generate_url method.'
                        ' Did you remember to extend StaticManifestMixin?' %
                        self.storage)
            hashed_name = generate_url(path)
            manifest.add(path, hashed_name)

        manifest.flush()
示例#3
0
    def collect(self):
        """
            Copied from collectstatic's Command.collect() with a tiny
            storage-layer check ..
        """
        if self.symlink:
            if sys.platform == 'win32':
                raise CommandError("Symlinking is not supported by this "
                                   "platform (%s)." % sys.platform)
            if not self.local:
                raise CommandError("Can't symlink to a remote destination.")

        if self.clear:
            self.clear_dir('')

        if self.symlink:
            handler = self.link_file
        else:
            handler = self.copy_file

        found_files = SortedDict()
        for finder in finders.get_finders():
            if isinstance(finder, AppLayerFinder):
                lister = lambda: finder.list(self.ignore_patterns, self.layer)
            else:
                lister = lambda: finder.list(self.ignore_patterns)

            for path, storage in lister():
                ## .. is the storage part of the current layer?
                if hasattr(storage, 'layer') and storage.layer != self.layer:
                    continue

                # Prefix the relative path if the source storage contains it
                if getattr(storage, 'prefix', None):
                    prefixed_path = os.path.join(storage.prefix, path)
                else:
                    prefixed_path = path

                if prefixed_path not in found_files:
                    found_files[prefixed_path] = (storage, path)
                    handler(path, prefixed_path, storage)

        # Here we check if the storage backend has a post_process
        # method and pass it the list of modified files.
        if self.post_process and hasattr(self.storage, 'post_process'):
            processor = self.storage.post_process(found_files,
                                                  dry_run=self.dry_run)
            for original_path, processed_path, processed in processor:
                if processed:
                    self.log(u"Post-processed '%s' as '%s" %
                             (original_path, processed_path), level=1)
                    self.post_processed_files.append(original_path)
                else:
                    self.log(u"Skipped post-processing '%s'" % original_path)

        return {
            'modified': self.copied_files + self.symlinked_files,
            'unmodified': self.unmodified_files,
            'post_processed': self.post_processed_files,
        }
示例#4
0
def get_base_finders():
    """
    List all base finders
    """
    for finder in get_finders():
        if not isinstance(finder, FacetsFinder):
            yield finder
示例#5
0
 def _load_namespaces():
     namespaces = getattr(StaticAsset, "_namespace_cache", None)
     if namespaces is None:
         namespaces = {}
         # Find all the assets.
         all_asset_names = []
         for finder in get_finders():
             for path, storage in finder.list(()):
                 if getattr(storage, "prefix", None):
                     path = os.path.join(storage.prefix, path)
                 all_asset_names.append(path)
         all_asset_names.sort()
         # Loads the assets.
         def do_load(type, include=(), exclude=()):
             include = [re.compile(fnmatch.translate(pattern)) for pattern in include]
             exclude = [re.compile(fnmatch.translate(pattern)) for pattern in exclude]
             # Create the loaded list of assets.
             asset_names = []
             seen_asset_names = set()
             for pattern in include:
                 new_asset_names = [a for a in all_asset_names if pattern.match(a) and not a in seen_asset_names]
                 asset_names.extend(new_asset_names)
                 seen_asset_names.update(new_asset_names)
             for pattern in exclude:
                 asset_names = [a for a in asset_names if not pattern.match(a)]
             # Create the assets.
             return [StaticAsset(asset_name) for asset_name in asset_names]
         # Load in all namespaces.
         for namespace, types in getattr(settings, "STATIC_ASSETS", {}).iteritems():
             type_cache = namespaces[namespace] = {}
             for type, config in types.iteritems():
                 type_cache[type] = do_load(type, **config)
         # Save in the cache.
         StaticAsset._namespace_cache = namespaces
     return namespaces
示例#6
0
def get_staticfiles_dirs():
    dirs = []
    for finder in get_finders():
        if isinstance(finder, (AppDirectoriesFinder, FileSystemFinder)):
            for storage in finder.storages.values():
                dirs.append(storage.location)
    return dirs
示例#7
0
 def listdir(self, path):
     for finder in finders.get_finders():
         for storage in finder.storages.values():
             try:
                 return storage.listdir(path)
             except OSError:
                 pass
示例#8
0
    def static_files_iterator(self):
        locations = get_apps_locations(self.test_labels, self.test_all)

        def in_tested_locations(path):
            for location in locations:
                if path.startswith(location):
                    return True
            return False
        
        if hasattr(settings, 'JSLINT_CHECKED_FILES'):
            for path in settings.JSLINT_CHECKED_FILES:
                yield path
                    
        if 'django.contrib.staticfiles' in settings.INSTALLED_APPS:
            # use django.contrib.staticfiles
            from django.contrib.staticfiles import finders

            for finder in finders.get_finders():
                for path, storage in finder.list(self.exclude):
                    path = os.path.join(storage.location, path)                
                    if path.endswith('.js') and in_tested_locations(path):
                        yield path
        else:
            # scan apps directories for static folders
            for location in locations:
                for dirpath, dirnames, filenames in os.walk(os.path.join(location, 'static')):
                    for filename in filenames:
                        if filename.endswith('.js') and in_tested_locations(os.path.join(dirpath, filename)):
                            yield os.path.join(dirpath, filename)
示例#9
0
文件: static.py 项目: ckaye89/hendrix
    def get_resources():

        ignore_patterns = [
            '*.less',
            '*.scss',
            '*.styl',
            '*.json',
        ]

        existing = []
        for finder in finders.get_finders():
            for staticfile, storage in finder.list([]):
                dirname = os.path.dirname(staticfile)
                path = os.path.join(storage.base_location, dirname)
                if not path in existing and dirname:
                    yield DjangoStaticResource(
                        path,
                        settings.STATIC_URL + '%s/' % dirname
                    )

                    existing.append(path)

        # add a handler for MEDIA files if configured
        if settings.MEDIA_ROOT and settings.MEDIA_URL:
            for resource in generate_resources_for_location(settings.MEDIA_ROOT, settings.MEDIA_URL):
                yield resource

        if not settings.DEBUG:
            for resource in generate_resources_for_location(settings.STATIC_ROOT, settings.STATIC_URL):
                yield resource
示例#10
0
    def _staticfiles(self):
        logging.info("htmldjango: _tags")
        """ matching any completions {% static '<here>' %}"""

        line = self.get_line()

        # checking for <img <style <script tags to further filter results
        if 'script' in line:
            ext = r".*\.(js|jsx)$"
        elif 'style' in line:
            ext = r".*\.(css|sass|scss|less)$"
        elif 'img' in line:
            ext = r".*\.(gif|jpg|jpeg|png)$"
        elif 'coffee' in line:
            ext = r".*\.coffee$"
        else:
            ext = r'.*'

        matches = []

        for finder in finders.get_finders():
            for path, _ in finder.list([]):
                if re.compile(ext, re.IGNORECASE).match(path) \
                        and path.startswith(self.pattern):
                    matches.append(
                        dict(insertion_text=path, extra_menu_info=''))

        return matches
示例#11
0
    def collect(self):
        """
        Perform the bulk of the work of collectstatic.

        Split off from handle() to facilitate testing.
        """
        if self.symlink and not self.local:
            raise CommandError("Can't symlink to a remote destination.")

        if self.clear:
            self.clear_dir("")

        if self.symlink:
            handler = self.link_file
        else:
            handler = self.copy_file

        found_files = OrderedDict()
        for finder in get_finders():
            for path, storage in finder.list(self.ignore_patterns):
                # Prefix the relative path if the source storage contains it
                if getattr(storage, "prefix", None):
                    prefixed_path = os.path.join(storage.prefix, path)
                else:
                    prefixed_path = path

                if prefixed_path not in found_files:
                    found_files[prefixed_path] = (storage, path)
                    handler(path, prefixed_path, storage)
                else:
                    self.log(
                        "Found another file with the destination path '%s'. It "
                        "will be ignored since only the first encountered file "
                        "is collected. If this is not what you want, make sure "
                        "every static file has a unique path." % prefixed_path,
                        level=1,
                    )

        # Here we check if the storage backend has a post_process
        # method and pass it the list of modified files.
        if self.post_process and hasattr(self.storage, "post_process"):
            processor = self.storage.post_process(found_files, dry_run=self.dry_run)
            for original_path, processed_path, processed in processor:
                if isinstance(processed, Exception):
                    self.stderr.write("Post-processing '%s' failed!" % original_path)
                    # Add a blank line before the traceback, otherwise it's
                    # too easy to miss the relevant part of the error message.
                    self.stderr.write("")
                    raise processed
                if processed:
                    self.log("Post-processed '%s' as '%s'" % (original_path, processed_path), level=1)
                    self.post_processed_files.append(original_path)
                else:
                    self.log("Skipped post-processing '%s'" % original_path)

        return {
            "modified": self.copied_files + self.symlinked_files,
            "unmodified": self.unmodified_files,
            "post_processed": self.post_processed_files,
        }
示例#12
0
def get_configs(names=('tests.json',), silent=False):
    """
    Find all configuration files throughout the project, parsing each and
    returning them as a list of dictionaries.
    """
    configs = {}
    for finder in get_finders():
        for path, storage in finder.list(ignore_patterns=None):
            if posixpath.basename(path) not in names:
                continue
            if path in configs:
                continue

            contents = storage.open(path).read()
            try:
                data = json.loads(contents)
                if not isinstance(data, dict):
                    raise ValueError("Expected a configuration dictionary")
                configs[path] = data
            except ValueError:
                if not silent:
                    raise ValueError("Invalid JSON config file: %s" % path)
                logging.error("Skipping invalid JSON config file: %s" % path)

    sorted_configs = sorted([
        (not data.get('priority'), posixpath.dirname(path), data)
        for path, data in configs.iteritems()
    ])
    return [parts[-1] for parts in sorted_configs]
示例#13
0
    def collect(self):
        target = 'apps/nunjucks/static/nunjucks/js/templates.js'
        templates = []
        for finder in finders.get_finders():
            for path, storage in finder.list(['*zinnia*']):

                if getattr(storage, 'prefix', None):
                    prefixed_path = os.path.join(storage.prefix, path)
                else:
                    prefixed_path = path

                # TOTO: find a correct way to get nj-paths
                if '/nj/' in path:
                    templates.append( {
                        'path': path,
                        'inner': self.compiler.compile(storage.path(path))
                        }
                    )


        tpl = render_to_string('nunjucks/compile/templates.js', {'templates': templates})

        open(target, "w").write(tpl)

        return
示例#14
0
    def handle(self, *args, **kwargs):
        training_flatpages = [step for step in training_summary.steps
                              if isinstance(step, FlatPageTrainingStep)]

        flatpages = {}

        # exit early if anything exists or file
        # is not found
        for step in training_flatpages:
            name = step.name
            src_path = 'flatpages/%s.html' % name
            for finder in get_finders():
                full_src_path = finder.find(src_path)
                if full_src_path:
                    flatpages[full_src_path] = step
                    break
            else:
                raise CommandError("static file '%s' should always exist. "
                                   "Did you delete one manually?" % src_path)

            url = '/%s/' % name
            if FlatPage.objects.filter(url=url).exists():
                raise CommandError("FlatPage '%s' already exists." % url)

        for full_src_path, step in flatpages.items():
            url = '/%s/' % step.name
            with open(full_src_path, 'r') as f:
                fp = FlatPage.objects.create(
                    title=step.description,
                    url=url,
                    template_name=step.flatpage_template_name,
                    content=f.read())
                fp.sites.add(settings.SITE_ID)
示例#15
0
    def handle(self, *args, **options):
        self.root_dir = settings.FILEPATH

        prefix = os.getcwd()
        current_snapshot = gitinfo.get_project_snapshot(self.root_dir, submodules=False)
        current_sha = current_snapshot['commits'][0]['sha']
        print "Current commit SHA: %s" % current_sha

        if 'clear' in args:
            print "clearing resource cache"
            rcache.delete_pattern(RESOURCE_PREFIX % '*')

        existing_resource_str = rcache.get(RESOURCE_PREFIX % current_sha, None)
        if existing_resource_str:
            print "getting resource dict from cache"
            self.output_resources(existing_resource_str)
            return

        self.resources = {}
        for finder in finders.get_finders():
            for path, storage in finder.list(['.*', '*~', '* *']):
                if not storage.location.startswith(prefix):
                    continue
                url = os.path.join(storage.prefix, path) if storage.prefix else path
                parts = (storage.location + '/' + path).split('/')
                self.generate_output(url, parts)
        resource_str = simplejson.dumps(self.resources, indent=2)
        rcache.set(RESOURCE_PREFIX % current_sha, resource_str, 86400)
        self.output_resources(resource_str)
示例#16
0
    def handle(self, *args, **options):
        prefix = os.getcwd()
        current_snapshot = gitinfo.get_project_snapshot(self.root_dir, submodules=False)
        current_sha = current_snapshot['commits'][0]['sha']
        print "Current commit SHA: %s" % current_sha

        if 'clear' in args:
            print "clearing resource cache"
            rcache.delete_pattern(RESOURCE_PREFIX % '*')

        existing_resource_str = rcache.get(RESOURCE_PREFIX % current_sha, None)
        if existing_resource_str:
            print "getting resource dict from cache"
            self.output_resources(existing_resource_str)
            return

        resources = {}
        for finder in finders.get_finders():
            for path, storage in finder.list(['.*', '*~', '* *']):
                if not storage.location.startswith(prefix):
                    continue
                if not getattr(storage, 'prefix', None):
                    url = path
                else:
                    url = os.path.join(storage.prefix, path)
                filename = os.path.join(storage.location, path)
                resources[url] = self.get_hash(filename)
        resource_str = json.dumps(resources, indent=2)
        rcache.set(RESOURCE_PREFIX % current_sha, resource_str, 86400)
        self.output_resources(resource_str)
示例#17
0
def build_less(item):
    path_less = path(item)
    path_css = '%s.css' % path_less

    updated_less = os.path.getmtime(path(item))
    updated_css = 0  # If the file doesn't exist, force a refresh.
    if os.path.exists(path_css):
        updated_css = os.path.getmtime(path_css)

    # Is the uncompiled version newer?  Then recompile!
    if updated_less > updated_css:
        with open(path_css, 'w') as output:
            less_dirs = []
            for finder in finders.get_finders():
                for rel_path, storage in finder.list(''):
                    if rel_path.endswith('.less'):
                        abs_path = storage.path(rel_path)
                        # Compute relative path of less_dir due to lessc
                        # peculiarity
                        common_prefix = os.path.commonprefix([abs_path,
                                                              path_less])
                        base_dir = os.path.dirname(common_prefix)
                        less_dir = os.path.relpath(os.path.dirname(abs_path),
                                                   base_dir)
                        if not less_dir in less_dirs:
                            less_dirs.append(less_dir)
            subprocess.Popen([settings.LESS_BIN,
                              '--include-path=%s' % ':'.join(less_dirs),
                              path_less],
                             stdout=output)
示例#18
0
def get_include_paths():
    """
    Generate a list of include paths that libsass should use to find files
    mentioned in @import lines.
    """
    global INCLUDE_PATHS
    if INCLUDE_PATHS is not None:
        return INCLUDE_PATHS

    include_paths = []

    # Look for staticfile finders that define 'storages'
    for finder in get_finders():
        try:
            storages = finder.storages
        except AttributeError:
            continue

        for storage in storages.values():
            try:
                include_paths.append(storage.path('.'))
            except NotImplementedError:
                # storages that do not implement 'path' do not store files locally,
                # and thus cannot provide an include path
                pass

    INCLUDE_PATHS = include_paths
    return include_paths
示例#19
0
    def handle(self, **options):
        prefix = os.getcwd()

        if options['clear']:
            print("clearing resource cache")
            rcache.delete_pattern(RESOURCE_PREFIX % '*')

        current_sha = self.current_sha()
        existing_resources = rcache.get(RESOURCE_PREFIX % current_sha, None)
        if existing_resources and not isinstance(existing_resources, six.string_types):
            print("getting resource dict from cache")
            self.output_resources(existing_resources)
            return
        if isinstance(existing_resources, six.string_types):
            soft_assert_type_text(existing_resources)

        resources = {}
        for finder in finders.get_finders():
            for path, storage in finder.list(['.*', '*~', '* *']):
                if not storage.location.startswith(prefix):
                    continue
                if not getattr(storage, 'prefix', None):
                    url = path
                else:
                    url = os.path.join(storage.prefix, path)
                filename = os.path.join(storage.location, path)
                resources[url] = self.get_hash(filename)
        self.overwrite_resources(resources, sha=current_sha)
示例#20
0
    def collect(self, request=None, files=[]):
        if self.request and self.request is request:
            return
        self.request = request
        found_files = OrderedDict()
        for finder in finders.get_finders():
            # Ignore our finder to avoid looping
            if isinstance(finder, PipelineFinder):
                continue
            for path, storage in finder.list(['CVS', '.*', '*~']):
                # Prefix the relative path if the source storage contains it
                if getattr(storage, 'prefix', None):
                    prefixed_path = os.path.join(storage.prefix, path)
                else:
                    prefixed_path = path

                if (prefixed_path not in found_files and
                    (not files or prefixed_path in files)):
                    found_files[prefixed_path] = (storage, path)
                    self.copy_file(path, prefixed_path, storage)

                if files and len(files) == len(found_files):
                    break

        return six.iterkeys(found_files)
示例#21
0
    def collect(self):
        target = 'apps/nunjucks/static/nunjucks/js/compiled-templates.js'
        templates = []
        for finder in finders.get_finders():
            for path, storage in finder.list([]):


                # TOTO: find a correct way to get nj-paths
                if '/nj/' in path:
                    print(path)
                    compiled_template = self.compiler.compile_template(storage.path(path))
                    compiled_template = re.sub('/Users/ohrstrom/Documents/Code/openbroadcast.org/website/apps/(\w*)/static/', '', compiled_template)
                    templates.append( {
                        'path': path,
                        'inner': compiled_template
                        }
                    )

        print(templates)

        tpl = render_to_string('nunjucks/compile/templates.js', {'templates': templates})

        file = codecs.open(target, "w", "utf-8")
        file.write(tpl)
        file.close()

        return
示例#22
0
    def get_resources():

        # add a handler for MEDIA files if configured
        if settings.MEDIA_ROOT and settings.MEDIA_URL:
            yield DjangoStaticResource(
                settings.MEDIA_ROOT, settings.MEDIA_URL
            )

        pipeline_finder = 'pipeline.finders.PipelineFinder'
        has_pipeline_finder = pipeline_finder in settings.STATICFILES_FINDERS
        if not settings.DEBUG or has_pipeline_finder:
            yield DjangoStaticResource(
                settings.STATIC_ROOT, settings.STATIC_URL
            )
        else:
            existing = []
            for finder in finders.get_finders():
                for staticfile, storage in finder.list([]):
                    dirname = os.path.dirname(staticfile)
                    path = os.path.join(storage.base_location, dirname)
                    if path not in existing and dirname:
                        yield DjangoStaticResource(
                            path,
                            settings.STATIC_URL + '%s/' % dirname
                        )

                        existing.append(path)
示例#23
0
    def glob_staticfiles(self, item):
        # The staticfiles finder system can't do globs, but we can
        # access the storages behind the finders, and glob those.

        # We can't import too early because of unit tests
        try:
            from django.contrib.staticfiles import finders
        except ImportError:
            # Support pre-1.3 versions.
            finders = None

        for finder in finders.get_finders():
            # Builtin finders use either one of those attributes,
            # though this does seem to be informal; custom finders
            # may well use neither. Nothing we can do about that.
            if hasattr(finder, 'storages'):
                storages = finder.storages.values()
            elif hasattr(finder, 'storage'):
                storages = [finder.storage]
            else:
                continue

            for storage in storages:
                globber = StorageGlobber(storage)
                for file in globber.glob(item):
                    yield storage.path(file)
示例#24
0
    def handle(self, *args, **options):

        app_paths = {}
        for app in settings.INSTALLED_APPS:
            app_paths[app] = flurry.utils.get_path_for_application(app) + "/static"

        found_files = SortedDict()
        fs = []
        for finder in finders.get_finders():
            for path, storage in finder.list(["CVS", ".*", "*~"]):
                # Prefix the relative path if the source storage contains it
                if getattr(storage, "prefix", None):
                    prefixed_path = os.path.join(storage.prefix, path)
                else:
                    prefixed_path = path

                if storage.source_dir == "static":
                    app = ""
                    for k, v in app_paths.iteritems():
                        if v in storage.location:
                            app = k
                    fs.append(app + "://" + path.replace(storage.location, ""))

        print "Building Flurry Resource Map..."

        mx = flurry.map_model.FlurryMapWritable(fs)
        mx.saveToFile(f_settings.flurry_map_file)
示例#25
0
 def find_storage(self, name):
     for finder in finders.get_finders():
         for path, storage in finder.list([]):
             prefix = getattr(storage, "prefix", None)
             matched_path = self.match_location(name, path, prefix)
             if matched_path:
                 return matched_path, storage
     raise ValueError("The file '%s' could not be found with %r." % (name, self))
 def _get_collectable_files(self):
     for finder in finders.get_finders():
         if not isinstance(finder, PipelineFinder):
             for path, storage in finder.list(["CVS", ".*", "*~"]):
                 if getattr(storage, "prefix", None):
                     yield os.path.join(storage.prefix, path)
                 else:
                     yield path
示例#27
0
 def find_source_storage(self, path):
     for finder in get_finders():
         for short_path, storage in finder.list(''):
             if short_path == path:
                 if self.verbose:
                     print("Found storage: %s" % str(self.storage))
                 return storage
     return None
示例#28
0
文件: dj.py 项目: noeldvictor/burlap
def iter_static_paths(ignore_import_error=False):

    load_django_settings()

    from django.contrib.staticfiles import finders, storage
    for finder in finders.get_finders():
        for _n,_s in finder.storages.iteritems():
            yield _s.location
 def find_storage_with_path(self, logical_path):
     for finder in finders.get_finders():
         for path, storage in finder.list([]):
             prefix = getattr(storage, 'prefix') or ''
             path = os.path.join(prefix, path)
             if path == logical_path:
                 return storage
     raise Exception()
示例#30
0
 def _get_collectable_files(self):
     for finder in finders.get_finders():
         if not isinstance(finder, PipelineFinder):
             for path, storage in finder.list(['CVS', '.*', '*~']):
                 if getattr(storage, 'prefix', None):
                     yield os.path.join(storage.prefix, path)
                 else:
                     yield path