636
637
return from_location[sep+1:]
638
639
return from_location
641
def _is_absolute(url):
642
return (osutils.pathjoin('/foo', url) == url)
644
def rebase_url(url, old_base, new_base):
645
"""Convert a relative path from an old base URL to a new base URL.
647
The result will be a relative path.
648
Absolute paths and full URLs are returned unaltered.
650
scheme, separator = _find_scheme_and_separator(url)
651
if scheme is not None:
653
if _is_absolute(url):
655
old_parsed = urlparse.urlparse(old_base)
656
new_parsed = urlparse.urlparse(new_base)
657
if (old_parsed[:2]) != (new_parsed[:2]):
658
raise ValueError('URLs cannot have relative paths: %s %s' %
659
(old_base, new_base))
660
return determine_relative_path(new_parsed.path,
661
osutils.pathjoin(old_parsed.path, url))
664
def determine_relative_path(from_path, to_path):
665
"""Determine a relative path from from_path to to_path."""
666
from_segments = osutils.splitpath(from_path)
667
to_segments = osutils.splitpath(to_path)
669
for count, (from_element, to_element) in enumerate(zip(from_segments,
671
if from_element != to_element:
675
unique_from = from_segments[count:]
676
unique_to = to_segments[count:]
677
segments = (['..'] * len(unique_from) + unique_to)
678
if len(segments) == 0:
680
return osutils.pathjoin(*segments)