/usr/lib/python2.7/dist-packages/pex/link.py is in python-pex 1.1.14-2ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | # Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import absolute_import
import os
import posixpath
from collections import Iterable
from .compatibility import string as compatible_string
from .compatibility import PY3, WINDOWS, pathname2url, url2pathname
from .util import Memoizer
if PY3:
import urllib.parse as urlparse
else:
import urlparse
class Link(object):
"""Wrapper around a URL."""
@classmethod
def wrap(cls, url):
"""Given a url that is either a string or :class:`Link`, return a :class:`Link`.
:param url: A string-like or :class:`Link` object to wrap.
:returns: A :class:`Link` object wrapping the url.
"""
if isinstance(url, cls):
return url
elif isinstance(url, compatible_string):
return cls(url)
else:
raise ValueError('url must be either a string or Link.')
@classmethod
def wrap_iterable(cls, url_or_urls):
"""Given a string or :class:`Link` or iterable, return an iterable of :class:`Link` objects.
:param url_or_urls: A string or :class:`Link` object, or iterable of string or :class:`Link`
objects.
:returns: A list of :class:`Link` objects.
"""
try:
return [cls.wrap(url_or_urls)]
except ValueError:
pass
if isinstance(url_or_urls, Iterable):
return [cls.wrap(url) for url in url_or_urls]
raise ValueError('url_or_urls must be string/Link or iterable of strings/Links')
@classmethod
def _normalize(cls, filename):
return urlparse.urljoin('file:', pathname2url(
os.path.realpath(os.path.expanduser(filename))))
# A cache for the result of from_filename
_FROM_FILENAME_CACHE = Memoizer()
@classmethod
def from_filename(cls, filename):
"""Return a :class:`Link` wrapping the local filename."""
result = cls._FROM_FILENAME_CACHE.get(filename)
if result is None:
result = cls(cls._normalize(filename))
cls._FROM_FILENAME_CACHE.store(filename, result)
return result
def __init__(self, url):
"""Construct a :class:`Link` from a url.
:param url: A string-like object representing a url.
"""
purl = urlparse.urlparse(url)
if purl.scheme == '' or (
WINDOWS and len(purl.scheme) == 1): # This is likely a drive letter.
purl = urlparse.urlparse(self._normalize(url))
self._url = purl
def __ne__(self, other):
return not self.__eq__(other)
def __eq__(self, link):
return self.__class__ == link.__class__ and self._url == link._url
def __hash__(self):
return hash(self._url)
def join(self, href):
"""Given a href relative to this link, return the :class:`Link` of the absolute url.
:param href: A string-like path relative to this link.
"""
return self.wrap(urlparse.urljoin(self.url, href))
@property
def filename(self):
"""The basename of this url."""
return urlparse.unquote(posixpath.basename(self._url.path))
@property
def path(self):
"""The full path of this url with any hostname and scheme components removed."""
return urlparse.unquote(self._url.path)
@property
def local_path(self):
"""Returns the local filesystem path (only works for file:// urls)."""
assert self.local, 'local_path called on a non-file:// url %s' % (self.url,)
return url2pathname(self.path)
@property
def url(self):
"""The url string to which this link points."""
return urlparse.urlunparse(self._url)
@property
def fragment(self):
"""The url fragment following '#' if any."""
return urlparse.unquote(self._url.fragment)
@property
def scheme(self):
"""The URI scheme used by this Link."""
return self._url.scheme
@property
def local(self):
"""Is the url a local file?"""
return self._url.scheme in ('', 'file')
@property
def remote(self):
"""Is the url a remote file?"""
return self._url.scheme in ('http', 'https')
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.url)
|