def remove_old_versions(self, path, prefixed_path, source_storage, **kwargs):
        # For iglob matching to work, we actually need the full path
        # of the output file.
        outpath = os.path.join(settings.VERSIONED_STATIC_ROOT, prefixed_path)

        current_version = get_file_version(path, source_storage)
        current_outpath = get_versioned_path(outpath, current_version)

        # Match any possible version of the output file.
        for matching_path in iglob(get_versioned_path(outpath, "*")):
            # Remove VERSIONED_STATIC_ROOT just for output log, etc.
            rel_matching_path = matching_path.replace(
                settings.VERSIONED_STATIC_ROOT, "", 1
            )[1:]
            if kwargs['all'] or (matching_path != current_outpath):
                self.deleted_files.append(matching_path)
                if kwargs['dry_run']:
                    self.log(u"Pretending to delete '%s' (Current is '%s')" % (
                        rel_matching_path, current_version
                    ))
                else:
                    self.log(u"Deleting '%s' (Current is '%s')" % (
                        rel_matching_path, current_version
                    ))
                    os.unlink(matching_path)
                    self.versioned_storage.delete(matching_path)
            else:
                self.unmodified_files.append(matching_path)
                self.log(u"Skipping '%s'. (Current version.)" % rel_matching_path)
Exemplo n.º 2
0
def versionedstatic(path):
    versioned_path = path
    if not settings.DEBUG:
        version = asset_versions.get(path)
        if version:
            versioned_path = get_versioned_path(path, version)

    return ''.join((settings.STATIC_URL, versioned_path))
    def handle_noargs(self, **options):
        self.verbosity = int(options.get('verbosity', 1))

        ignore_patterns = options['ignore_patterns']
        if options['use_default_ignore_patterns']:
            ignore_patterns += ['CVS', '.*', '*~']
        ignore_patterns = list(set(ignore_patterns))

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

This will remove 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.")

        # Get output filenames for up-to-date static files.
        self.valid_outpaths = set()
        if not options['all']:
            for finder in finders.get_finders():
                for path, storage in finder.list(ignore_patterns):
                    # Prefix the relative path if source storage contains it
                    if getattr(storage, 'prefix', None):
                        prefixed_path = os.path.join(storage.prefix, path)
                    else:
                        prefixed_path = path

                    current_version = get_file_version(path, storage)
                    current_outpath = get_versioned_path(
                        prefixed_path, current_version)

                    self.valid_outpaths.add(current_outpath)

        # Find everything currently in VERSIONED_STATIC_ROOT and remove
        # anything not in `self.valid_outpaths`
        def cleardirs(cd, subdirs, files):
            for d in subdirs:
                new_cd = os.path.join(cd, d)
                new_subdirs, new_files = self.versioned_storage.listdir(
                    os.path.join(cd, d)
                )
                cleardirs(new_cd, new_subdirs, new_files)
            for f in files:
                filepath = os.path.join(cd, f)
                if filepath not in self.valid_outpaths:
                    if options['dry_run']:
                        self.log(u"Pretending to delete '%s'" % filepath)
                    else:
                        self.log(u"Deleting '%s'" % filepath)
                        self.versioned_storage.delete(filepath)
                    self.deleted_files.append(filepath)
                else:
                    self.log(u"Skipping up-to-date file '%s'" % filepath)
                    self.unmodified_files.append(filepath)

        subdirs, files = self.versioned_storage.listdir("")
        cleardirs("", subdirs, files)

        actual_count = len(self.deleted_files)
        unmodified_count = len(self.unmodified_files)
        if self.verbosity >= 1:
            self.stdout.write(smart_str(u"\n%s versioned file%s %s%s.\n"
                              % (actual_count, actual_count != 1 and 's' or '',
                                 'deleted',
                                 unmodified_count and ' (%s unmodified)'
                                 % unmodified_count or '')))
Exemplo n.º 4
0
 def link_file(self, path, prefixed_path, source_storage, **kwargs):
     version = get_file_version(path, source_storage)
     prefixed_path = get_versioned_path(prefixed_path, version)
     return super(Command, self).link_file(path, prefixed_path,
         source_storage, **kwargs)