Ejemplo n.º 1
0
 def test_cache(self):
     finders.get_finder.cache_clear()
     for n in range(10):
         finders.get_finder('django.contrib.staticfiles.finders.FileSystemFinder')
     cache_info = finders.get_finder.cache_info()
     self.assertEqual(cache_info.hits, 9)
     self.assertEqual(cache_info.currsize, 1)
Ejemplo n.º 2
0
 def test_cache(self):
     finders.get_finder.cache_clear()
     for n in range(10):
         finders.get_finder("django.contrib.staticfiles.finders.FileSystemFinder")
     cache_info = finders.get_finder.cache_info()
     self.assertEqual(cache_info.hits, 9)
     self.assertEqual(cache_info.currsize, 1)
Ejemplo n.º 3
0
 def test_override_staticfiles_dirs(self):
     """
     Overriding the STATICFILES_DIRS setting should be reflected in
     the locations attribute of the
     django.contrib.staticfiles.finders.FileSystemFinder instance.
     """
     finder = get_finder('django.contrib.staticfiles.finders.FileSystemFinder')
     test_path = '/tmp/test'
     expected_location = ('', test_path)
     self.assertNotIn(expected_location, finder.locations)
     with self.settings(STATICFILES_DIRS=[test_path]):
         finder = get_finder('django.contrib.staticfiles.finders.FileSystemFinder')
         self.assertIn(expected_location, finder.locations)
Ejemplo n.º 4
0
 def test_override_staticfiles_dirs(self):
     """
     Overriding the STATICFILES_DIRS setting should be reflected in
     the locations attribute of the
     django.contrib.staticfiles.finders.FileSystemFinder instance.
     """
     finder = get_finder('django.contrib.staticfiles.finders.FileSystemFinder')
     test_path = '/tmp/test'
     expected_location = ('', test_path)
     self.assertNotIn(expected_location, finder.locations)
     with self.settings(STATICFILES_DIRS=[test_path]):
         finder = get_finder('django.contrib.staticfiles.finders.FileSystemFinder')
         self.assertIn(expected_location, finder.locations)
Ejemplo n.º 5
0
 def test_nonexistent_directories(self):
     with self.settings(
         STATICFILES_DIRS=[
             "/fake/path",
             ("prefix", "/fake/prefixed/path"),
         ]
     ):
         self.assertEqual(
             check_finders(None),
             [
                 Warning(
                     "The directory '/fake/path' in the STATICFILES_DIRS "
                     "setting does not exist.",
                     id="staticfiles.W004",
                 ),
                 Warning(
                     "The directory '/fake/prefixed/path' in the "
                     "STATICFILES_DIRS setting does not exist.",
                     id="staticfiles.W004",
                 ),
             ],
         )
         # Nonexistent directories are skipped.
         finder = get_finder("django.contrib.staticfiles.finders.FileSystemFinder")
         self.assertEqual(list(finder.list(None)), [])
Ejemplo n.º 6
0
    def watched_paths(self):
        """
        list of paths to be watched for modifications on static files.

        Returns:
        all static and template locations for all apps included in DJANGO_LIVESYNC.INCLUDED_APPS.
        """
        tmpls = [
            os.path.join(d, 'templates') for d in get_app_template_dirs('')
            if d.startswith(settings.BASE_DIR) and os.path.basename(
                d) in settings.DJANGO_LIVESYNC['INCLUDED_APPS']
            and os.path.exists(os.path.join(d, 'templates'))
        ]

        paths = set(
            itertools.chain(
                getattr(settings, 'STATICFILES_DIRS', []),
                getattr(settings, 'TEMPLATE_DIRS',
                        []),  # django backward compatibility.
                tmpls,
            ))

        paths.update(*[tmpl['DIRS'] for tmpl in settings.TEMPLATES])

        if apps.is_installed('django.contrib.staticfiles'):
            from django.contrib.staticfiles.finders import get_finder
            finder = get_finder(
                'django.contrib.staticfiles.finders.AppDirectoriesFinder')
            paths.update([
                st.location for app, st in finder.storages.items()
                if app in settings.DJANGO_LIVESYNC['INCLUDED_APPS']
            ])

        return paths
Ejemplo n.º 7
0
def all_scss_files():
    '''
    Finds all .scss files that are in a scss folder in all staticfiles search
    locations, and returns a list of HTML <link> tags that contain references
    to those .scss files ready for consumption by e.g. django-compressor.
    '''

    # First, we need to get all the django.contrib.staticfiles
    # finders that are loaded for this project, since the common
    # finders interface does not support generic file listings.
    all_finders = [finders.get_finder(finder)
                   for finder in settings.STATICFILES_FINDERS]

    # For each of the finders, we get a get a list of all the
    # the static files available, and filter out all files
    # that are not .scss files.
    all_found_scss_files = reduce(operator.add, [
        filter(lambda filename: re.match('scss/[^/]*.scss$', filename),
               [filename for filename, _ in finder.list('')])
        for finder in all_finders])

    # Finally, we wrap in HTML <link> tags and return
    return {
        'all_found_scss_files': all_found_scss_files,
        'STATIC_URL': settings.STATIC_URL,
    }
Ejemplo n.º 8
0
 def test_get_finder(self):
     self.assertTrue(isinstance(finders.get_finder(
         "django.contrib.staticfiles.finders.FileSystemFinder"),
         finders.FileSystemFinder))
     self.assertRaises(ImproperlyConfigured,
         finders.get_finder, "django.contrib.staticfiles.finders.FooBarFinder")
     self.assertRaises(ImproperlyConfigured,
         finders.get_finder, "foo.bar.FooBarFinder")
Ejemplo n.º 9
0
 def test_get_finder(self):
     self.assertTrue(isinstance(finders.get_finder(
         'django.contrib.staticfiles.finders.FileSystemFinder'),
         finders.FileSystemFinder))
     self.assertRaises(ImproperlyConfigured,
         finders.get_finder, 'django.contrib.staticfiles.finders.FooBarFinder')
     self.assertRaises(ImproperlyConfigured,
         finders.get_finder, 'foo.bar.FooBarFinder')
Ejemplo n.º 10
0
def use_static_root(settings):
    from django.contrib.staticfiles.finders import get_finder

    static_root = ('', settings.STATIC_ROOT)
    finder = get_finder('django.contrib.staticfiles.finders.FileSystemFinder')
    finder.locations.append(static_root)
    yield

    try:
        finder.locations.remove(static_root)
    except ValueError:
        pass
def get_all_static():
    """
    Get all the static files directories found by ``STATICFILES_FINDERS``

    :return: set of paths (top-level folders only)
    """
    static_dirs = set()

    for finder in settings.STATICFILES_FINDERS:
        finder = finders.get_finder(finder)

        if hasattr(finder, "storages"):
            for storage in finder.storages.values():
                static_dirs.add(storage.location)

        if hasattr(finder, "storage"):
            static_dirs.add(finder.storage.location)

    return static_dirs
Ejemplo n.º 12
0
    def __init__(self):
        """Initialize the finder with user settings and paths."""
        # Try to get the Bulma settings. The user may not have created this dict.
        try:
            self.bulma_settings = settings.BULMA_SETTINGS
        except AttributeError:
            self.bulma_settings = {}

        self.bulma_submodule_path = simple_bulma_path / "bulma" / "sass"
        self.custom_scss = self.bulma_settings.get("custom_scss", [])
        self.variables = self.bulma_settings.get("variables", {})
        self.output_style = self.bulma_settings.get("output_style", "nested")
        self.storage = FileSystemStorage(simple_bulma_path)

        # Make a list of all the finders except this one.
        # We use this in the custom SCSS handler.
        other_finders = settings.STATICFILES_FINDERS.copy()
        other_finders.remove("django_simple_bulma.finders.SimpleBulmaFinder")
        self.other_finders = [get_finder(finder) for finder in other_finders]
Ejemplo n.º 13
0
def get_all_static():
    """
    Get all the static files directories found by ``STATICFILES_FINDERS``

    :return: set of paths (top-level folders only)
    """
    static_dirs = set()

    for finder in settings.STATICFILES_FINDERS:
        finder = finders.get_finder(finder)

        if hasattr(finder, 'storages'):
            for storage in finder.storages.values():
                static_dirs.add(storage.location)

        if hasattr(finder, 'storage'):
            static_dirs.add(finder.storage.location)

    return static_dirs
Ejemplo n.º 14
0
 def test_get_finder(self):
     self.assertIsInstance(finders.get_finder(
         'django.contrib.staticfiles.finders.FileSystemFinder'),
         finders.FileSystemFinder)
Ejemplo n.º 15
0
        def test_app_directory_finder(self):
            finder = get_finder('django.contrib.staticfiles.finders.AppDirectoriesFinder')
            dirs = AppDirectoriesFinderDirs(finder)

            # This ain't no real test, whatever
            self.assertTrue(len(dirs.list_paths()) > 0)
Ejemplo n.º 16
0
    def handle(self, **options):
        commit = options.get('commit', None)
        filelist = options.get('filelist', [])
        dry_run = options.get('dry_run', False)
        path = options.get('path', None)
        verbosity = int(options.get('verbosity', 1))
        interactive = options.get('interactive', True)

        verbose_output = False
        if verbosity > 1:
            verbose_output = True

        vcs = False
        if len(filelist) == 0:
            vcs = True

        get_changed_files = get_changed_files_git

        if path == None:
            # Look in the parent directory
            path = '..'

        # check staticfiles and pipeline is actually being used
        if settings.STATICFILES_STORAGE not in (
            'deploy_utils.storage.S3StaticStorage',
            'pipeline.storage.PipelineCachedStorage',
            'pipeline.storage.PipelineStorage'):

            self.stdout.write("Looks like you are not using S3 storage or " \
                "pipeline for static files - as such there is no need to " \
                "deploy any static files.")
            return

        if vcs:
            if not commit:
                commit = prompt("What commit do you want to deploy?")
            message, files_changed = get_changed_files(commit, path)
        else:
            message, files_changed = get_changed_files_local(filelist)

        if not files_changed:
            if vcs:
                self.stdout.write("No files were changed in revision %s " \
                    "\n\n COMMIT MESSAGE: '%s'" % (commit, message))
            else:
                self.stdout.write("You must specify files to deploy")
            return

        # show message from revision and prompt to proceed
        if vcs:
            prompt_message = "Are you sure you want to deploy commit" \
                " %s\nCOMMIT MESSAGE: '%s' \nFILES CHANGED:" % (
                commit, message)
        else:
            prompt_message = "Are you sure you want to " \
                "deploy these files:"

        for file_changed in files_changed:
            prompt_message = prompt_message + '\n\t' + file_changed

        if interactive and not prompt_bool(prompt_message + '\n'):
            self.stdout.write('Deployment aborted')
            return

        npm_collect_required = False
        npm_root_path = getattr(settings, "NPM_ROOT_PATH", "")
        if npm_root_path:
            npm_root_path = os.path.normpath(npm_root_path)

        # loop through all files and save each one using the default storage
        # (s3 is this case) if it is a static file
        for file_changed in files_changed:
            abs_path = os.path.join(os.path.abspath(path),
                                    file_changed)

            relative_path = get_static_file_path(abs_path)

            if verbose_output:
                self.stdout.write('file_changed = %s ' % file_changed)
                self.stdout.write('path = %s ' % path)
                self.stdout.write('abs_path = %s ' % abs_path)
                self.stdout.write('relative_path = %s ' % relative_path)

            if not relative_path:
                if npm_root_path and npm_root_path in abs_path:
                    # Front-end source file changed; we'll collect the
                    # NPM-built distribution files later
                    npm_collect_required = True
                else:
                    self.stdout.write('%s is _NOT_ a media/static file ' \
                        'and will not be deployed' % file_changed)
            elif not os.path.isfile(abs_path):  # check that the file exists
                self.stdout.write("%s doesn't exist locally so can't be " \
                                  "deployed" % abs_path)
            else:
                # this is a valid static file and we'll
                # post-process it now
                if not dry_run:
                    copy_static_file(abs_path, relative_path)
                    self.stdout.write('\tcopied %s ' % relative_path)
                    post_process_static_file(abs_path, relative_path)
                    self.stdout.write('\tprocessed %s ' % relative_path)

        if npm_collect_required:
            self.stdout.write("Collecting NPM-built assets...")
            finder = get_finder(
                    "django.contrib.staticfiles.finders.FileSystemFinder")
            ignore_patterns = ['CVS', '.*', '*~']
            for relative_path, storage in finder.list(ignore_patterns):
                location = storage.location
                if npm_root_path in location and not dry_run:
                    abs_path = os.path.join(storage.location, relative_path)
                    copy_static_file(abs_path, relative_path)
                    self.stdout.write('\tcopied %s ' % relative_path)
Ejemplo n.º 17
0
 def test_get_finder_bad_module(self):
     with self.assertRaises(ImportError):
         finders.get_finder('foo.bar.FooBarFinder')
Ejemplo n.º 18
0
 def test_get_finder_bad_classname(self):
     with self.assertRaises(ImportError):
         finders.get_finder('django.contrib.staticfiles.finders.FooBarFinder')
Ejemplo n.º 19
0
 def test_get_finder_bad_module(self):
     with self.assertRaises(ImportError):
         finders.get_finder("foo.bar.FooBarFinder")
Ejemplo n.º 20
0
 def test_get_finder_bad_classname(self):
     with self.assertRaises(ImportError):
         finders.get_finder("django.contrib.staticfiles.finders.FooBarFinder")
Ejemplo n.º 21
0
        def test_file_system_finder(self):
            finder = get_finder('django.contrib.staticfiles.finders.FileSystemFinder')
            dirs = FileSystemFinderDirs(finder)

            self.assertEqual(set(str(x) for x in settings.STATICFILES_DIRS), set(dirs.list_paths()))
Ejemplo n.º 22
0
def get_src_finders():
    for finder_path in settings.STATICFILES_FINDERS:
        if not finder_path.endswith("TransformfilesFinder"):
            yield get_finder(finder_path)
Ejemplo n.º 23
0
def test_dvhr001_hot_reload(dash_duo):
    from django.contrib.staticfiles.finders import get_finder

    finder = get_finder('django.contrib.staticfiles.finders.FileSystemFinder')
    finder.locations.append(('', settings.STATIC_ROOT), )

    settings.DASH_HOT_RELOAD = True

    app = dash.Dash(__name__, assets_folder="hr_assets")
    app.layout = html.Div(
        [html.H3("Hot reload", id="text"),
         html.Button("Click", id="btn")],
        id="hot-reload-content",
    )

    @app.callback(Output("text", "children"), [Input("btn", "n_clicks")])
    def new_text(n):
        if not n:
            raise PreventUpdate
        return n

    hot_reload_settings = dict(
        dev_tools_hot_reload=True,
        dev_tools_ui=True,
        dev_tools_serve_dev_bundles=True,
        dev_tools_hot_reload_interval=0.1,
        dev_tools_hot_reload_max_retry=100,
    )

    class DashView(BaseDashView):
        dash = app

    dash_duo.start_server(DashView, **hot_reload_settings)

    # default overload color is blue
    dash_duo.wait_for_style_to_equal("#hot-reload-content", "background-color",
                                     "rgba(0, 0, 255, 1)")

    # set a global var - if we soft reload it should still be there,
    # hard reload will delete it
    dash_duo.driver.execute_script("window.someVar = 42;")
    assert dash_duo.driver.execute_script("return window.someVar") == 42

    soft_reload_file, old_soft = replace_file("hot_reload.css", RED_BG)

    try:
        # red is live changed during the test execution
        dash_duo.wait_for_style_to_equal("#hot-reload-content",
                                         "background-color",
                                         "rgba(255, 0, 0, 1)")
    finally:
        sleep(1)  # ensure a new mod time
        with open(soft_reload_file, "w") as f:
            f.write(old_soft)

    dash_duo.wait_for_style_to_equal("#hot-reload-content", "background-color",
                                     "rgba(0, 0, 255, 1)")

    # only soft reload, someVar is still there
    assert dash_duo.driver.execute_script("return window.someVar") == 42

    assert dash_duo.driver.execute_script(
        "return window.cheese") == "roquefort"

    hard_reload_file, old_hard = replace_file("hot_reload.js", GOUDA)

    try:
        until(
            lambda: dash_duo.driver.execute_script("return window.cheese") ==
            "gouda",
            timeout=3,
        )
    finally:
        sleep(1)  # ensure a new mod time
        with open(hard_reload_file, "w") as f:
            f.write(old_hard)

    until(
        lambda: dash_duo.driver.execute_script("return window.cheese") ==
        "roquefort",
        timeout=3,
    )

    # we've done a hard reload so someVar is gone
    assert dash_duo.driver.execute_script("return window.someVar") is None

    # Now check the server status indicator functionality

    dash_duo.find_element(".dash-debug-menu").click()
    dash_duo.find_element(".dash-debug-menu__button--available")
    sleep(1)  # wait for opening animation
    dash_duo.percy_snapshot(name="hot-reload-available")

    dash_duo.server.stop()
    sleep(
        1)  # make sure we would have requested the reload hash multiple times
    dash_duo.find_element(".dash-debug-menu__button--unavailable")
    dash_duo.wait_for_no_elements(".dash-fe-error__title")
    dash_duo.percy_snapshot(name="hot-reload-unavailable")

    dash_duo.find_element(".dash-debug-menu").click()
    sleep(1)  # wait for opening animation
    dash_duo.find_element(".dash-debug-disconnected")
    dash_duo.percy_snapshot(name="hot-reload-unavailable-small")

    dash_duo.find_element("#btn").click()
    dash_duo.wait_for_text_to_equal(
        ".dash-fe-error__title",
        "Callback failed: the server did not respond.")

    # start up the server again
    dash_duo.start_server(app, **hot_reload_settings)

    # rerenders with debug menu closed after reload
    # reopen and check that server is now available
    dash_duo.find_element(".dash-debug-menu--closed").click()
    dash_duo.find_element(".dash-debug-menu__button--available")
Ejemplo n.º 24
0
    def handle(self, **options):
        commit = options.get('commit', None)
        filelist = options.get('filelist', [])
        dry_run = options.get('dry_run', False)
        path = options.get('path', None)
        verbosity = int(options.get('verbosity', 1))
        interactive = options.get('interactive', True)

        verbose_output = False
        if verbosity > 1:
            verbose_output = True

        vcs = False
        if len(filelist) == 0:
            vcs = True

        get_changed_files = get_changed_files_git

        if path == None:
            # Look in the parent directory
            path = '..'

        # check staticfiles and pipeline is actually being used
        if settings.STATICFILES_STORAGE not in (
                'deploy_utils.storage.S3StaticStorage',
                'pipeline.storage.PipelineCachedStorage',
                'pipeline.storage.PipelineStorage'):

            self.stdout.write("Looks like you are not using S3 storage or " \
                "pipeline for static files - as such there is no need to " \
                "deploy any static files.")
            return

        if vcs:
            if not commit:
                commit = prompt("What commit do you want to deploy?")
            message, files_changed = get_changed_files(commit, path)
        else:
            message, files_changed = get_changed_files_local(filelist)

        if not files_changed:
            if vcs:
                self.stdout.write("No files were changed in revision %s " \
                    "\n\n COMMIT MESSAGE: '%s'" % (commit, message))
            else:
                self.stdout.write("You must specify files to deploy")
            return

        # show message from revision and prompt to proceed
        if vcs:
            prompt_message = "Are you sure you want to deploy commit" \
                " %s\nCOMMIT MESSAGE: '%s' \nFILES CHANGED:" % (
                commit, message)
        else:
            prompt_message = "Are you sure you want to " \
                "deploy these files:"

        for file_changed in files_changed:
            prompt_message = prompt_message + '\n\t' + file_changed

        if interactive and not prompt_bool(prompt_message + '\n'):
            self.stdout.write('Deployment aborted')
            return

        npm_collect_required = False
        npm_root_path = getattr(settings, "NPM_ROOT_PATH", "")
        if npm_root_path:
            npm_root_path = os.path.normpath(npm_root_path)

        # loop through all files and save each one using the default storage
        # (s3 is this case) if it is a static file
        for file_changed in files_changed:
            abs_path = os.path.join(os.path.abspath(path), file_changed)

            relative_path = get_static_file_path(abs_path)

            if verbose_output:
                self.stdout.write('file_changed = %s ' % file_changed)
                self.stdout.write('path = %s ' % path)
                self.stdout.write('abs_path = %s ' % abs_path)
                self.stdout.write('relative_path = %s ' % relative_path)

            if not relative_path:
                if npm_root_path and npm_root_path in abs_path:
                    # Front-end source file changed; we'll collect the
                    # NPM-built distribution files later
                    npm_collect_required = True
                else:
                    self.stdout.write('%s is _NOT_ a media/static file ' \
                        'and will not be deployed' % file_changed)
            elif not os.path.isfile(abs_path):  # check that the file exists
                self.stdout.write("%s doesn't exist locally so can't be " \
                                  "deployed" % abs_path)
            else:
                # this is a valid static file and we'll
                # post-process it now
                if not dry_run:
                    copy_static_file(abs_path, relative_path)
                    self.stdout.write('\tcopied %s ' % relative_path)
                    post_process_static_file(abs_path, relative_path)
                    self.stdout.write('\tprocessed %s ' % relative_path)

        if npm_collect_required:
            self.stdout.write("Collecting NPM-built assets...")
            finder = get_finder(
                "django.contrib.staticfiles.finders.FileSystemFinder")
            ignore_patterns = ['CVS', '.*', '*~']
            for relative_path, storage in finder.list(ignore_patterns):
                location = storage.location
                if npm_root_path in location and not dry_run:
                    abs_path = os.path.join(storage.location, relative_path)
                    copy_static_file(abs_path, relative_path)
                    self.stdout.write('\tcopied %s ' % relative_path)
Ejemplo n.º 25
0
 def test_get_finder(self):
     self.assertIsInstance(finders.get_finder(
         'django.contrib.staticfiles.finders.FileSystemFinder'),
         finders.FileSystemFinder)
Ejemplo n.º 26
0
 def get_finders(self):
     return (get_finder('lily.utils.staticfiles.finders.GeneratedMediaFinder'),)