Commit 9256848c by Timothée Peignier

use posixpath rather than os dependant os.path to deal with asset urls

parent 8ccc3204
import base64 import base64
import os import os
import posixpath
import re import re
import subprocess import subprocess
...@@ -13,8 +14,8 @@ except ImportError: ...@@ -13,8 +14,8 @@ except ImportError:
from django.contrib.staticfiles import finders # noqa from django.contrib.staticfiles import finders # noqa
from pipeline.conf import settings from pipeline.conf import settings
from pipeline.utils import to_class, relpath
from pipeline.storage import default_storage from pipeline.storage import default_storage
from pipeline.utils import to_class, relpath
URL_DETECTOR = r'url\([\'"]?([^\s)]+\.[a-z]+[\?\#\d\w]*)[\'"]?\)' URL_DETECTOR = r'url\([\'"]?([^\s)]+\.[a-z]+[\?\#\d\w]*)[\'"]?\)'
URL_REPLACER = r'url\(__EMBED__(.+?)(\?\d+)?\)' URL_REPLACER = r'url\(__EMBED__(.+?)(\?\d+)?\)'
...@@ -144,16 +145,16 @@ class Compressor(object): ...@@ -144,16 +145,16 @@ class Compressor(object):
def construct_asset_path(self, asset_path, css_path, output_filename, variant=None): def construct_asset_path(self, asset_path, css_path, output_filename, variant=None):
"""Return a rewritten asset URL for a stylesheet""" """Return a rewritten asset URL for a stylesheet"""
public_path = self.absolute_path(asset_path, os.path.dirname(css_path)) public_path = self.absolute_path(asset_path, posixpath.dirname(css_path))
if self.embeddable(public_path, variant): if self.embeddable(public_path, variant):
return "__EMBED__%s" % public_path return "__EMBED__%s" % public_path
if not os.path.isabs(asset_path): if not posixpath.isabs(asset_path):
asset_path = self.relative_path(public_path, output_filename) asset_path = self.relative_path(public_path, output_filename)
return asset_path return asset_path
def embeddable(self, path, variant): def embeddable(self, path, variant):
"""Is the asset embeddable ?""" """Is the asset embeddable ?"""
name, ext = os.path.splitext(path) name, ext = posixpath.splitext(path)
font = ext in FONT_EXTS font = ext in FONT_EXTS
if not variant: if not variant:
return False return False
...@@ -191,16 +192,16 @@ class Compressor(object): ...@@ -191,16 +192,16 @@ class Compressor(object):
Return the absolute public path for an asset, Return the absolute public path for an asset,
given the path of the stylesheet that contains it. given the path of the stylesheet that contains it.
""" """
if os.path.isabs(path): if posixpath.isabs(path):
path = os.path.join(default_storage.location, path) path = posixpath.join(default_storage.location, path)
else: else:
path = os.path.join(start, path) path = posixpath.join(start, path)
return os.path.normpath(path) return posixpath.normpath(path)
def relative_path(self, absolute_path, output_filename): def relative_path(self, absolute_path, output_filename):
"""Rewrite paths relative to the output stylesheet path""" """Rewrite paths relative to the output stylesheet path"""
absolute_path = os.path.join(settings.PIPELINE_ROOT, absolute_path) absolute_path = posixpath.join(settings.PIPELINE_ROOT, absolute_path)
output_path = os.path.join(settings.PIPELINE_ROOT, os.path.dirname(output_filename)) output_path = posixpath.join(settings.PIPELINE_ROOT, posixpath.dirname(output_filename))
return relpath(absolute_path, output_path) return relpath(absolute_path, output_path)
def read_file(self, path): def read_file(self, path):
......
import mimetypes import mimetypes
import os import posixpath
import sys
import urllib import urllib
from django.utils import importlib from django.utils import importlib
...@@ -34,52 +33,18 @@ def guess_type(path, default=None): ...@@ -34,52 +33,18 @@ def guess_type(path, default=None):
return mimetype return mimetype
def _relpath_nt(path, start=os.path.curdir): def relpath(path, start=posixpath.curdir):
"""Return a relative version of a path"""
if not path:
raise ValueError("no path specified")
start_list = os.path.abspath(start).split(os.path.sep)
path_list = os.path.abspath(path).split(os.path.sep)
if start_list[0].lower() != path_list[0].lower():
unc_path, rest = os.path.splitunc(path)
unc_start, rest = os.path.splitunc(start)
if bool(unc_path) ^ bool(unc_start):
raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
% (path, start))
else:
raise ValueError("path is on drive %s, start on drive %s"
% (path_list[0], start_list[0]))
# Work out how much of the filepath is shared by start and path.
for i in range(min(len(start_list), len(path_list))):
if start_list[i].lower() != path_list[i].lower():
break
else:
i += 1
rel_list = [os.path.pardir] * (len(start_list) - i) + path_list[i:]
if not rel_list:
return os.path.curdir
return os.path.join(*rel_list)
def _relpath_posix(path, start=os.path.curdir):
"""Return a relative version of a path""" """Return a relative version of a path"""
if not path: if not path:
raise ValueError("no path specified") raise ValueError("no path specified")
start_list = os.path.abspath(start).split(os.path.sep) start_list = posixpath.abspath(start).split(posixpath.sep)
path_list = os.path.abspath(path).split(os.path.sep) path_list = posixpath.abspath(path).split(posixpath.sep)
# Work out how much of the filepath is shared by start and path. # Work out how much of the filepath is shared by start and path.
i = len(os.path.commonprefix([start_list, path_list])) i = len(posixpath.commonprefix([start_list, path_list]))
rel_list = [os.path.pardir] * (len(start_list) - i) + path_list[i:] rel_list = [posixpath.pardir] * (len(start_list) - i) + path_list[i:]
if not rel_list: if not rel_list:
return os.path.curdir return posixpath.curdir
return os.path.join(*rel_list) return posixpath.join(*rel_list)
if os.path is sys.modules.get('ntpath'):
relpath = _relpath_nt
else:
relpath = _relpath_posix
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment