Пример #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 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
Пример #3
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)
Пример #4
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)
Пример #5
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
Пример #6
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,
        }
Пример #7
0
    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
Пример #8
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)
Пример #9
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)
Пример #10
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,
        }
Пример #11
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]
Пример #12
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)
Пример #13
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
Пример #14
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)
Пример #15
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)
Пример #16
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
Пример #17
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()
Пример #18
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)
Пример #19
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
Пример #20
0
def get_base_finders():
    """
    List all base finders
    """
    for finder in get_finders():
        if not isinstance(finder, FacetsFinder):
            yield finder
Пример #21
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)
Пример #22
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)
Пример #23
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
Пример #24
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
Пример #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 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()
Пример #27
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
Пример #28
0
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 _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
Пример #30
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
Пример #31
0
def grab_static_files():

    f = TemporaryFile()
    tar = tarfile.open(fileobj=f, mode="w")

    found_files = set()
    for finder in get_finders():
        for path, storage in finder.list(['CVS', '.*', '*~']):
            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.add(prefixed_path)
                tar.add(storage.path(path), prefixed_path)
    tar.close()

    return f
Пример #32
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.

        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)
Пример #33
0
 def _check_sprite_attribute(self, **kwargs):
     if not self.sprite:
         return [
             checks.Error(
                 'sprite file path required',
                 obj=self
             )
         ]
     else:
         for finder in get_finders():
             result = finder.find(self.sprite, all=True)
             if result:
                 return []
         else:
             return [
                 checks.Error(
                     'sprite file not found: %s' % self.sprite,
                     obj=self
                 )
             ]
Пример #34
0
    def handle(self, *args, **options):
        venv = options.get('venv')
        base = settings.BASE_DIR
        webpack_loader_stats_path = os.path.dirname(
            settings.WEBPACK_LOADER_STATS_FILE)
        webpack_loader_stats_filename = os.path.basename(
            settings.WEBPACK_LOADER_STATS_FILE)
        output = options['PATH'][0]
        if venv and venv[0] != '/':
            venv = os.path.join(base, venv)
        node_modules = os.path.join(venv, 'node_modules')
        djaoapp = os.path.join(base, 'djaoapp', 'static')
        assets_cache_path = os.path.normpath(
            settings.ASSETS_ROOT + settings.STATIC_URL +
            settings.WEBPACK_LOADER['DEFAULT']['BUNDLE_DIR_NAME'])
        dirs = {
            'assets_cache_path': assets_cache_path,
            'webpack_loader_stats_path': webpack_loader_stats_path,
            'webpack_loader_stats_filename': webpack_loader_stats_filename,
            'node_modules': [node_modules],
        }
        ign = apps.get_app_config('staticfiles').ignore_patterns
        djaodjin_apps = ['djaoapp', 'saas', 'signup', 'rules', 'pages']
        djaodjin_mods = [djaoapp]
        for finder in get_finders():
            for path, storage in finder.list(ign):
                pth = storage.path(path)

                for app in djaodjin_apps:
                    subs = '/%s/static/' % app
                    if subs in pth:
                        static_dir = pth[0:pth.find(subs) + len(subs)][0:-1]
                        if static_dir not in djaodjin_mods:
                            djaodjin_mods.append(static_dir)
        dirs['djaodjin_modules'] = djaodjin_mods + [
            os.path.dirname(assets_cache_path), node_modules
        ]
        with open(output, 'w') as file_d:
            file_d.write(json.dumps(dirs))

        self.stdout.write('dumped djaodjin-webpack config to %s' % output)
Пример #35
0
    def handle_noargs(self, **options):
        symlink = options['link']
        ignore_patterns = options['ignore_patterns']
        if options['use_default_ignore_patterns']:
            ignore_patterns += ['CVS', '.*', '*~']
        ignore_patterns = list(set(ignore_patterns))
        self.verbosity = int(options.get('verbosity', 1))

        if symlink:
            if sys.platform == 'win32':
                raise CommandError("Symlinking is not supported by this "
                                   "platform (%s)." % sys.platform)
            if not self.destination_local:
                raise CommandError("Can't symlink to a remote destination.")

        # Warn before doing anything more.
        if options.get('interactive'):
            confirm = raw_input("""
You have requested to collate static files and collect them at the destination
location as specified in your settings file.

This will overwrite existing files.
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: """)
            if confirm != 'yes':
                raise CommandError("Collecting static files cancelled.")

        for finder in finders.get_finders():
            for source, storage in finder.list(ignore_patterns):
                self.copy_file(source, storage, **options)

        actual_count = len(self.copied_files) + len(self.symlinked_files)
        unmodified_count = len(self.unmodified_files)
        if self.verbosity >= 1:
            self.stdout.write(
                "\n%s static file%s %s to '%s'%s.\n" %
                (actual_count, actual_count != 1 and 's' or '',
                 symlink and 'symlinked' or 'copied', settings.STATIC_ROOT,
                 unmodified_count and ' (%s unmodified)' % unmodified_count
                 or ''))
Пример #36
0
 def get_staticfiles_finders(self):
     """
     Returns a sorted mapping between the finder path and the list
     of relative and file system paths which that finder was able
     to find.
     """
     finders_mapping = OrderedDict()
     for finder in finders.get_finders():
         for path, finder_storage in finder.list([]):
             if getattr(finder_storage, 'prefix', None):
                 prefixed_path = join(finder_storage.prefix, path)
             else:
                 prefixed_path = path
             finder_cls = finder.__class__
             finder_path = '.'.join(
                 [finder_cls.__module__, finder_cls.__name__])
             real_path = finder_storage.path(path)
             payload = (prefixed_path, real_path)
             finders_mapping.setdefault(finder_path, []).append(payload)
             self.num_found += 1
     return finders_mapping
Пример #37
0
def collect_files(compilers):
    """Collect files for given compilers across the project.

    Given list of compilers to collect files for, returns a dictionary mapping
    compiler to list of tuples (src_path, dest_path).

    This is a mini implementation of the "collectstatic" management command.
    """

    # This common ignore pattern is defined inline in
    # django.contrib.staticfiles.management.commands.collectstatic, and we
    # just repeat it here verbatim
    ignore_patterns = ['CVS', '.*', '*~']

    if additional_ignore_patterns:
        ignore_patterns.extend(additional_ignore_patterns)

    output = defaultdict(list)

    # staticfiles has two default finders, one for the STATICFILES_DIRS and
    # one for the /static directories of the apps listed in INSTALLED_APPS.
    # This allows us to discover all the files we are interested in across
    # the entire project, including the libraries it uses.

    for finder in finders.get_finders():
        for partial_path, storage in finder.list(ignore_patterns):
            # Get the actual path of the asset
            full_path = storage.path(partial_path)

            # Resolve symbolic links
            src_path = os.path.realpath(full_path)

            base, ext = os.path.splitext(partial_path)

            if not any(dirs in full_path for dirs in ignore_dirs):
                for compiler in compilers:
                    if compiler.matches(base, ext):
                        output[compiler].append(
                            (src_path, compiler.get_dest_path(base, ext)))
    return output
Пример #38
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)
Пример #39
0
    def run_checks(cls):
        """
        Check that the integration is configured correctly for the panel.

        Specifically look for static files that haven't been collected yet.

        Return a list of :class: `django.core.checks.CheckMessage` instances.
        """
        errors = []
        for finder in finders.get_finders():
            try:
                for path, finder_storage in finder.list([]):
                    finder_storage.path(path)
            except OSError:
                errors.append(
                    Warning(
                        "debug_toolbar requires the STATICFILES_DIRS directories to exist.",
                        hint="Running manage.py collectstatic may help uncover the issue.",
                        id="debug_toolbar.staticfiles.W001",
                    )
                )
        return errors
Пример #40
0
    def static_files_iterator(self):
        locations = get_apps_locations(self.test_labels, self.test_all)

        def in_tested_locations(path):
            if not self.jslint_with_minjs and path.endswith('.min.js'):
                return False

            for location in list(locations):
                if path.startswith(location):
                    return True
            if self.with_static_dirs:
                for location in list(settings.STATICFILES_DIRS):
                    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)
def get_include_paths():
    """
    Generate a list of include paths that libsass should use to find files
    mentioned in @import lines.
    """
    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

    return include_paths
    def collect(self):

        results = super(Command, self).collect()

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

        # Now check for static assets for each client.
        # If the asset is located in:
        #  - clients/gent/assets/images..
        # then the asset will be collected to
        #  - static/assets/gent/images
        found_files = SortedDict()

        # TenantStaticFilesFinder
        tenant_dir = getattr(settings, 'MULTI_TENANT_DIR', None)

        for finder in finders.get_finders():
            if isinstance(finder, TenantStaticFilesFinder):
                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)

        return {
            'modified': self.copied_files + self.symlinked_files,
            'unmodified': self.unmodified_files,
            'post_processed': self.post_processed_files,
        }
Пример #43
0
def get_static_file_path(abs_path):
    """
    Check if the given file path represents a static file, and if so return
    the corresponding relative path (i.e. relative to the static folder it
    belongs to); otherwise return None.
    """
    rel_path = None

    # First check if the file is in one of our STATICFILES_DIRS
    for static_dir in settings.STATICFILES_DIRS:
        static_dir = os.path.abspath(os.path.normpath(static_dir))
        if static_dir in abs_path:
            rel_path = abs_path[len(static_dir) + 1:]
            break

    if not rel_path:
        # Check if the file appears to be in one of the installed apps'
        # 'static' subdirectories
        static_dir = "{}static{}".format(os.path.sep, os.path.sep)
        index = abs_path.rfind(static_dir)
        if index != -1:
            # Path contains '/static/'; take the part after this
            rel_path = abs_path[index + len(static_dir):]

    if rel_path:
        # Have a relative path; now check if it is actually is a static
        # file by seeing if any of our static file finders returns it
        for finder in get_finders():
            # If the given relative path is a static file, then one of our
            # finders should return the corresponding absolute path
            found_paths = finder.find(rel_path, all=True)
            if abs_path in found_paths:
                # Return the relative static path (e.g. 'img/icons/arrow.png')
                return rel_path

    # Return None to indicate that this is not a static file
    return None
    def _find_in_other_finders(self, path, all=False):
        """Look for one or more matches in finders besides this one.

        Unfortunately much of this logic has to be duplicated from
        django.contrib.staticfiles.finders.find, because there's no easy way
        to eliminate this finder from the search and avoid infinite recusion.
        """
        matches = []

        for finder in get_finders():
            if isinstance(finder, self.__class__):
                continue

            result = finder.find(path, all=all)

            if not all and result:
                return result

            if not isinstance(result, (list, tuple)):
                result = [result]

            matches.extend(result)

        return matches
Пример #45
0
def cached_angular_template(path):
    """Load a template from cache or from a file.

    Cache the file if not in cache.

    :param path: Relative path to angular template
    :type path: str
    :returns: Rendering of script type="text/ng-template" element
    :rtype: str
    """
    # Check from cache first
    key = '_angular_template_{}'.format(path)
    html = cache.get(key)
    # If not found, read from file then cache
    if not html:
        paths = []
        for finder in finders.get_finders():
            paths += finder.find(path, True)
        with open(paths[-1], 'r') as file:
            html = file.read()
        html = _T.format(path, html)
        cache.set(key, html)
    # Mark value as safe
    return mark_safe(html)
Пример #46
0
 def get_staticfiles_finders(self):
     """
     Returns a sorted mapping between the finder path and the list
     of relative and file system paths which that finder was able
     to find.
     """
     finders_mapping = OrderedDict()
     for finder in finders.get_finders():
         try:
             for path, finder_storage in finder.list([]):
                 if getattr(finder_storage, "prefix", None):
                     prefixed_path = join(finder_storage.prefix, path)
                 else:
                     prefixed_path = path
                 finder_cls = finder.__class__
                 finder_path = ".".join([finder_cls.__module__, finder_cls.__name__])
                 real_path = finder_storage.path(path)
                 payload = (prefixed_path, real_path)
                 finders_mapping.setdefault(finder_path, []).append(payload)
                 self.num_found += 1
         except OSError:
             # This error should be captured and presented as a part of run_checks.
             pass
     return finders_mapping
Пример #47
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,
                    )

        # Storage backends may define a post_process() method.
        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=2)
                    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,
        }
Пример #48
0
    def handle(self, **options):
        try:
            from resource_versions import resource_versions
        except (ImportError, SyntaxError):
            resource_versions = {}

        # Write build.js file to feed to r.js
        with open(
                os.path.join(self.root_dir, 'staticfiles', 'hqwebapp', 'yaml',
                             'requirejs.yaml'), 'r') as f:
            config = yaml.load(f)

            bundles = {}
            all_modules = []
            prefix = os.path.join(os.getcwd(), 'corehq')
            for finder in finders.get_finders():
                if isinstance(finder, finders.AppDirectoriesFinder):
                    for path, storage in finder.list(
                        ['.*', '*~', '* *', '*.*ss', '*.png']):
                        if not storage.location.startswith(prefix):
                            continue
                        if path.endswith(".js"):
                            directory = re.sub(r'/[^/]*$', '', path)
                            if directory not in bundles:
                                bundles[directory] = []
                            bundles[directory].append(path[:-3])
                            all_modules.append(path[:-3])

            customized = {
                re.sub(r'/[^/]*$', '', m['name']): True
                for m in config['modules']
            }
            for directory, inclusions in six.iteritems(bundles):
                if directory not in customized and not directory.startswith(
                        "app_manager/js/vellum"):
                    # Add this module's config to build config
                    config['modules'].append({
                        'name':
                        os.path.join(directory, 'bundle'),
                        'include':
                        inclusions,
                        'excludeShallow': [
                            name for name in all_modules
                            if name not in inclusions
                        ],
                        'exclude': ['hqwebapp/js/common'],
                    })

            # Write .js files to staticfiles
            for module in config['modules']:
                with open(
                        os.path.join(self.root_dir, 'staticfiles',
                                     module['name'] + ".js"), 'w') as fout:
                    fout.write("define([], function() {});")

            with open(os.path.join(self.root_dir, 'staticfiles', 'build.js'),
                      'w') as fout:
                fout.write("({});".format(json.dumps(config, indent=4)))

        call([
            "node", "bower_components/r.js/dist/r.js", "-o",
            "staticfiles/build.js"
        ])
        filename = os.path.join(self.root_dir, 'staticfiles', 'hqwebapp', 'js',
                                'requirejs_config.js')
        resource_versions["hqwebapp/js/requirejs_config.js"] = self.get_hash(
            filename)

        # Overwrite each bundle in resource_versions with the sha from the optimized version in staticfiles
        for module in config['modules']:
            filename = os.path.join(self.root_dir, 'staticfiles',
                                    module['name'] + ".js")
            resource_versions[module['name'] + ".js"] = self.get_hash(filename)

        # Write out resource_versions.js for all js files in resource_versions
        # Exclude formdesigner directory, which contains a ton of files, none of which are required by HQ
        if settings.STATIC_CDN:
            filename = os.path.join(self.root_dir, 'staticfiles', 'hqwebapp',
                                    'js', 'resource_versions.js')
            with open(filename, 'w') as fout:
                fout.write("requirejs.config({ paths: %s });" % json.dumps(
                    {
                        file[:-3]: "{}{}{}{}".format(
                            settings.STATIC_CDN, settings.STATIC_URL,
                            file[:-3],
                            ".js?version=%s" % version if version else "")
                        for file, version in six.iteritems(resource_versions)
                        if file.endswith(".js")
                        and not file.startswith("formdesigner")
                    },
                    indent=2))
            resource_versions[
                "hqwebapp/js/resource_versions.js"] = self.get_hash(filename)

        self.overwrite_resources(resource_versions)
Пример #49
0
def finder(glob):
    for finder in finders.get_finders():
        for path, storage in finder.list([]):
            if fnmatch.fnmatchcase(path, glob):
                yield path, storage
    def collect(self):
        """
        Load and save ``PageColorScheme`` for every ``PageTheme``

        .. code-block:: bash

            static/themes/bootswatch/united/variables.scss
            static/themes/bootswatch/united/styles.scss

        """

        self.ignore_patterns = [
            '*.png', '*.jpg', '*.js', '*.gif', '*.ttf', '*.md', '*.rst',
            '*.svg'
        ]
        page_themes = PageTheme.objects.all()

        for finder in get_finders():
            for path, storage in finder.list(self.ignore_patterns):
                for t in page_themes:
                    static_path = 'themes/{0}'.format(t.name.split('/')[-1])
                    if static_path in path:
                        try:
                            page_theme = PageTheme.objects.get(id=t.id)
                        except PageTheme.DoesNotExist:
                            raise Exception(
                                "Run sync_themes before this command")
                        except Exception as e:
                            self.stdout.write(
                                "Cannot load {} into database original error: {}"
                                .format(t, e))

                        # find and load skins
                        skins_path = os.path.join(
                            storage.path('/'.join(path.split('/')[0:-1])))
                        for dirpath, skins, filenames in os.walk(skins_path):
                            for skin in [
                                    s for s in skins if s not in ['fonts']
                            ]:
                                for skin_dirpath, skins, filenames in os.walk(
                                        os.path.join(dirpath, skin)):
                                    skin, created = PageColorScheme.objects.get_or_create(
                                        theme=page_theme,
                                        label=skin,
                                        name=skin.title())
                                    for f in filenames:
                                        if 'styles' in f:
                                            with codecs.open(
                                                    os.path.join(
                                                        skin_dirpath,
                                                        f)) as style_file:
                                                skin.styles = style_file.read()
                                        elif 'variables' in f:
                                            with codecs.open(
                                                    os.path.join(
                                                        skin_dirpath,
                                                        f)) as variables_file:
                                                skin.variables = variables_file.read(
                                                )
                                    skin.save()
                                    self.skins_updated += 1

        self.page_themes_updated += len(page_themes)
Пример #51
0
 def iter_static_paths(self, ignore_import_error=False):
     self.load_django_settings()
     from django.contrib.staticfiles import finders, storage
     for finder in finders.get_finders():
         for path, _storage in finder.list(ignore_patterns=[]):
             yield path
Пример #52
0
    def handle_noargs(self, **options):
        self.clear = options['clear']
        self.dry_run = options['dry_run']
        ignore_patterns = options['ignore_patterns']
        if options['use_default_ignore_patterns']:
            ignore_patterns += ['CVS', '.*', '*~']
        self.ignore_patterns = list(set(ignore_patterns))
        self.interactive = options['interactive']
        self.symlink = options['link']
        self.verbosity = int(options.get('verbosity', 1))
        self.post_process = options['post_process']

        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.")

        # Warn before doing anything more.
        if (isinstance(self.storage, FileSystemStorage)
                and self.storage.location):
            destination_path = self.storage.location
            destination_display = ':\n\n    %s' % destination_path
        else:
            destination_path = None
            destination_display = '.'

        if self.clear:
            clear_display = 'This will DELETE EXISTING FILES!'
        else:
            clear_display = 'This will overwrite existing files!'

        if self.interactive:
            confirm = raw_input(u"""
You have requested to collect static files at the destination
location as specified in your settings%s

%s
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: """ %
                                (destination_display, clear_display))
            if confirm != 'yes':
                raise CommandError("Collecting static files cancelled.")

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

        handler = {
            True: self.link_file,
            False: self.copy_file,
        }[self.symlink]

        found_files = []
        for finder in finders.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
                found_files.append(prefixed_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'):
            post_processed = self.storage.post_process(found_files, **options)
            for path in post_processed:
                self.log(u"Post-processed '%s'" % path, level=1)
        else:
            post_processed = []

        modified_files = self.copied_files + self.symlinked_files
        actual_count = len(modified_files)
        unmodified_count = len(self.unmodified_files)

        if self.verbosity >= 1:
            template = ("\n%(actual_count)s %(identifier)s %(action)s"
                        "%(destination)s%(unmodified)s.\n")
            summary = template % {
                'actual_count':
                actual_count,
                'identifier':
                'static file' + (actual_count > 1 and 's' or ''),
                'action':
                self.symlink and 'symlinked' or 'copied',
                'destination':
                (destination_path and " to '%s'" % destination_path or ''),
                'unmodified':
                (self.unmodified_files and ', %s unmodified' % unmodified_count
                 or ''),
            }
            self.stdout.write(smart_str(summary))
Пример #53
0
 def __init__(self):
     self.packager = Packager()
     self.packages = self.collect_packages()
     self.finders = get_finders()
     self.package_files = []
Пример #54
0
def find_file(path):
    for finder in get_finders():
        result = finder.find(path)
        if result:
            return result
Пример #55
0
             (rel(root_ca_path), rel(child_ca_path), rel(child_cert_path),
              ocsp_url)))

elif args.command == "collectstatic":
    setup_django("ca.settings")

    # pylint: disable=ungrouped-imports; have to call setup_django() first
    from django.contrib.staticfiles.finders import get_finders
    from django.core.management import call_command

    # pylint: enable=ungrouped-imports

    call_command("collectstatic", interactive=False)

    locations = set()
    for finder in get_finders():
        for path, storage in finder.list([]):
            locations.add(storage.location)

    for location in locations:
        print('rm -r "%s"' % location)
        shutil.rmtree(location)
elif args.command == "clean":
    base = os.path.dirname(os.path.abspath(__file__))

    def rm(*paths):  # pylint: disable=invalid-name; rm() is just descriptive
        """Remove a file/dir if it exists."""
        rm_path = os.path.join(base, *paths)
        if not os.path.exists(rm_path):
            return
        if os.path.isdir(rm_path):
Пример #56
0
    def handle_noargs(self, **options):
        self.clear = options['clear']
        self.dry_run = options['dry_run']
        ignore_patterns = options['ignore_patterns']
        if options['use_default_ignore_patterns']:
            ignore_patterns += ['CVS', '.*', '*~']
        self.ignore_patterns = list(set(ignore_patterns))
        self.interactive = options['interactive']
        self.symlink = options['link']
        self.verbosity = int(options.get('verbosity', 1))

        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.")

        # Warn before doing anything more.
        if (isinstance(self.storage, FileSystemStorage)
                and self.storage.location):
            destination_path = self.storage.location
            destination_display = ':\n\n    %s' % destination_path
        else:
            destination_path = None
            destination_display = '.'

        if self.clear:
            clear_display = 'This will DELETE EXISTING FILES!'
        else:
            clear_display = 'This will overwrite existing files!'

        if self.interactive:
            confirm = raw_input(u"""
You have requested to collect static files at the destination
location as specified in your settings%s

%s
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: """ %
                                (destination_display, clear_display))
            if confirm != 'yes':
                raise CommandError("Collecting static files cancelled.")

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

        handler = {True: self.link_file, False: self.copy_file}[self.symlink]

        for finder in finders.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
                handler(path, prefixed_path, storage)

        actual_count = len(self.copied_files) + len(self.symlinked_files)
        unmodified_count = len(self.unmodified_files)
        if self.verbosity >= 1:
            self.stdout.write(
                smart_str(
                    u"\n%s static file%s %s %s%s.\n" %
                    (actual_count, actual_count != 1 and 's' or '',
                     self.symlink and 'symlinked' or 'copied', destination_path
                     and "to '%s'" % destination_path or '', unmodified_count
                     and ' (%s unmodified)' % unmodified_count or '')))
Пример #57
0
def get_buildable_file_finders():
    with patched_settings(STATICBUILDER_COLLECT_BUILT=False):
        for f in finders.get_finders():
            yield BuildableFileFinder(f)
Пример #58
0
def find_common_media():
    for finder in finders.get_finders():
        for path, _storage in finder.list([]):
            if path.startswith(settings.COMMON_MEDIA_PREFIX):
                yield path
Пример #59
0
    def edit_style(self, request, page_pk, pk):
        """
        Prompt edit style form for requested plugin
        :param request:
        :param pk:
        :return: HTML (edit_style_form) or a script that close popup window
        """
        page = get_object_or_404(HtmlPage, pk=page_pk)
        plugin = get_object_or_404(Plugin, pk=pk)

        # Create the modeladmin instance of the plugin
        plugin_modeladmin_cls = plugin_pool.plugins[plugin.type]
        plugin_model = plugin_modeladmin_cls.model
        plugin_modeladmin = plugin_modeladmin_cls(plugin_model,
                                                  self.admin_site)

        # Fetch the plugin manager that contain the appropriate context variables
        plugin_manager = get_object_or_404(plugin_model, pk=pk)

        if plugin_modeladmin.__class__.__name__ == 'RowPlugin':
            rendered_template = '<div class="row" id="cms_plugin_' + str(
                plugin_manager.pk) + '">'
        elif plugin_modeladmin.__class__.__name__ == 'ColumnPlugin':
            rendered_template = '<div class="col-lg-' + str(
                plugin_manager.width) + '" id="cms_plugin_' + str(
                    plugin_manager.pk) + '">'
        else:
            if plugin_modeladmin.template:
                # TODO: Find a way to render plugin template as will showed properly in the page
                request_context = RequestContext(request)
                request_context['page_slug'] = page.slug
                rendered_template = plugin_modeladmin.render_to_string(
                    request_context, plugin_manager)

        # Normalize template without whitespaces
        lines = rendered_template.split('\n')
        rendered_template = '\n'.join(
            [line for line in lines if not re.match(r'^\s*$', line)])

        if request.method == 'GET':
            # Find all html tags from the template
            # Exam if it is in cache else set it to the cache with timeout 15 * 60 = 15 minutes
            parser = cache.get(plugin.pk)
            if not parser:
                parser = MyHtmlParser()
                parser.feed(rendered_template)
                cache.set(plugin.pk, parser, 60 * 15)

                # Find all css of the project and pass it to the css list
                csss = []
                for finder in finders.get_finders():
                    for file_ in list(
                            finder.list([
                                '*.js', '*.min.css', '*.woff2', '*.svg',
                                '*.eot', '*.woff', '*.ttf', '*.png', '*.jpg',
                                '*.otf', '*.psd', '*.map', '*.txt', '*.gif',
                                '*.md', '*admin*', '*ckeditor*',
                                '*loosecms_style*'
                            ])):
                        csss.append(finders.find(file_[0]))

                # Call utils function which finds all css attributes given template
                populate_cssclasses_attrs(csss, parser.plugin_style)

            StyleFormSet = formset_factory(StyleForm,
                                           formset=BaseStyleFormSet,
                                           extra=0,
                                           can_delete=True)
            formset = StyleFormSet(initial=get_initial_values(
                parser.plugin_style, plugin),
                                   admin_site=self.admin_site)

            context = dict(
                # Include common variables for rendering the admin template.
                self.admin_site.each_context(request),
                current_app=self.admin_site.name,
                title=_('Edit stylesheet'),
                formset=formset,
                media=self.media + formset.media,
                is_popup=True,
                template=rendered_template,
                form_url=urlresolvers.reverse('admin:admin_edit_style',
                                              args=(page_pk, pk)))
            return render(request, 'admin/edit_style_form.html', context)

        if request.method == 'POST':
            StyleFormSet = formset_factory(StyleForm,
                                           formset=BaseStyleFormSet,
                                           can_delete=True,
                                           validate_min=True,
                                           min_num=1)
            formset = StyleFormSet(data=request.POST,
                                   files=request.FILES,
                                   admin_site=self.admin_site)

            if formset.is_valid():
                for form in formset:
                    if 'title' not in form.cleaned_data:
                        continue

                    pk = form.cleaned_data['pk']
                    title = form.cleaned_data['title']
                    original_html = form.cleaned_data['original_html']
                    html_tag = form.cleaned_data['html_tag']
                    html_id = form.cleaned_data['html_id']
                    source_css = form.cleaned_data['source_css']
                    css = form.cleaned_data['css']
                    styleclasses = form.cleaned_data['styleclasses']
                    source_styleclasses = form.cleaned_data[
                        'source_styleclasses']

                    if form in formset.deleted_forms:
                        try:
                            style = Style.objects.get(pk=pk)
                            style.delete()
                            continue
                        except Style.DoesNotExist:
                            pass

                    source_styleclasses = [
                        x for x in source_styleclasses.split(',')
                    ]
                    source_styleclasses_queryset = StyleClass.objects.filter(
                        title__in=source_styleclasses)

                    update_values = dict(title=title,
                                         plugin=plugin,
                                         original_html=original_html,
                                         html_tag=html_tag,
                                         html_id=html_id if html_id else None,
                                         source_css=source_css,
                                         css=css)

                    if plugin.type == 'RowPlugin' or plugin.type == 'ColumnPlugin':
                        update_values.update(element_is_grid=True)
                    else:
                        update_values.update(element_is_grid=False)

                    style, created = Style.objects.update_or_create(
                        pk=pk, defaults=update_values)

                    style.styleclasses.clear()
                    style.source_styleclasses.clear()
                    for styleclass in styleclasses:
                        if styleclass in source_styleclasses_queryset:
                            form.add_error(
                                'styleclasses',
                                '%s styleclass is arleady in source styleclasess'
                                % styleclass)
                            context = dict(
                                # Include common variables for rendering the admin template.
                                self.admin_site.each_context(request),
                                current_app=self.admin_site.name,
                                title=_('Edit stylesheet'),
                                formset=formset,
                                media=self.media + formset.media,
                                is_popup=True,
                                template=rendered_template,
                                form_url=urlresolvers.reverse(
                                    'admin:admin_edit_style',
                                    args=(page_pk, pk)))
                            return render(request,
                                          'admin/edit_style_form.html',
                                          context)
                        style.styleclasses.add(styleclass)

                    for source_styleclass in source_styleclasses_queryset:
                        style.source_styleclasses.add(source_styleclass)

                return HttpResponse(
                    '<script>window.parent.location.reload(true);self.close();</script>'
                )
            else:
                context = dict(
                    # Include common variables for rendering the admin template.
                    self.admin_site.each_context(request),
                    current_app=self.admin_site.name,
                    title=_('Edit stylesheet'),
                    formset=formset,
                    media=self.media + formset.media,
                    is_popup=True,
                    template=rendered_template,
                    form_url=urlresolvers.reverse('admin:admin_edit_style',
                                                  args=(page_pk, pk)))
                return render(request, 'admin/edit_style_form.html', context)
Пример #60
0
def find_static_files(ignore_patterns=()):
    found_files = SortedDict()
    for finder in finders.get_finders():
        for path, storage in finder.list(ignore_patterns):
            found_files[path] = storage.path(path)
    return found_files