def canonical_filename(self, filename):
"""Return a canonical filename for `filename`.
An absolute path with no redundant components and normalized case.
"""
if filename not in self.canonical_filename_cache:
if not os.path.isabs(filename):
for path in [os.curdir] + sys.path:
if path is None:
continue
f = os.path.join(path, filename)
if os.path.exists(f):
filename = f
break
cf = abs_file(filename)
self.canonical_filename_cache[filename] = cf
return self.canonical_filename_cache[filename]
python类isabs()的实例源码
def canonical_filename(self, filename):
"""Return a canonical filename for `filename`.
An absolute path with no redundant components and normalized case.
"""
if filename not in self.canonical_filename_cache:
if not os.path.isabs(filename):
for path in [os.curdir] + sys.path:
if path is None:
continue
f = os.path.join(path, filename)
if os.path.exists(f):
filename = f
break
cf = abs_file(filename)
self.canonical_filename_cache[filename] = cf
return self.canonical_filename_cache[filename]
def canonical_filename(self, filename):
"""Return a canonical filename for `filename`.
An absolute path with no redundant components and normalized case.
"""
if filename not in self.canonical_filename_cache:
if not os.path.isabs(filename):
for path in [os.curdir] + sys.path:
if path is None:
continue
f = os.path.join(path, filename)
if os.path.exists(f):
filename = f
break
cf = abs_file(filename)
self.canonical_filename_cache[filename] = cf
return self.canonical_filename_cache[filename]
def canonical_filename(self, filename):
"""Return a canonical filename for `filename`.
An absolute path with no redundant components and normalized case.
"""
if filename not in self.canonical_filename_cache:
if not os.path.isabs(filename):
for path in [os.curdir] + sys.path:
if path is None:
continue
f = os.path.join(path, filename)
if os.path.exists(f):
filename = f
break
cf = abs_file(filename)
self.canonical_filename_cache[filename] = cf
return self.canonical_filename_cache[filename]
def norm_path(path, cwd):
"""
Normalize a path using a cwd if needed and possible.
"""
if not posixpath.isabs(path):
has_abs_cwd = cwd and posixpath.isabs(cwd)
if has_abs_cwd:
if not posixpath.isabs(path):
path = posixpath.join(cwd, path)
else:
logger.warning('In norm_path: Unable to resolve path: '
'%(path)s: cwd %(cwd)r is invalid.' % locals())
return posixpath.normpath(path)
# An exec is loaded in a process: we keep the command, args and time stamp
def canonical_filename(self, filename):
"""Return a canonical filename for `filename`.
An absolute path with no redundant components and normalized case.
"""
if filename not in self.canonical_filename_cache:
if not os.path.isabs(filename):
for path in [os.curdir] + sys.path:
if path is None:
continue
f = os.path.join(path, filename)
if os.path.exists(f):
filename = f
break
cf = abs_file(filename)
self.canonical_filename_cache[filename] = cf
return self.canonical_filename_cache[filename]
def canonical_filename(filename):
"""Return a canonical file name for `filename`.
An absolute path with no redundant components and normalized case.
"""
if filename not in CANONICAL_FILENAME_CACHE:
if not os.path.isabs(filename):
for path in [os.curdir] + sys.path:
if path is None:
continue
f = os.path.join(path, filename)
if os.path.exists(f):
filename = f
break
cf = abs_file(filename)
CANONICAL_FILENAME_CACHE[filename] = cf
return CANONICAL_FILENAME_CACHE[filename]
def resolvepath(self, hdfs_path):
"""Return absolute, normalized path.
:param hdfs_path: Remote path.
"""
path = hdfs_path
if not psp.isabs(path):
if not self.root or not psp.isabs(self.root):
root = self._api_request(method='GET', hdfs_path='/', params={'op': 'GETHOMEDIRECTORY'}).json()['Path']
self.root = psp.join(root, self.root) if self.root else root
_logger.debug('Updated root to %r.', self.root)
path = psp.join(self.root, path)
path = psp.normpath(path)
_logger.debug('Resolved path %r to %r.', hdfs_path, path)
return path
def isabs_anywhere(filename):
"""Is `filename` an absolute path on any OS?"""
return ntpath.isabs(filename) or posixpath.isabs(filename)
def test_isabs(self):
self.assertIs(posixpath.isabs(""), False)
self.assertIs(posixpath.isabs("/"), True)
self.assertIs(posixpath.isabs("/foo"), True)
self.assertIs(posixpath.isabs("/foo/bar"), True)
self.assertIs(posixpath.isabs("foo/bar"), False)
self.assertIs(posixpath.isabs(b""), False)
self.assertIs(posixpath.isabs(b"/"), True)
self.assertIs(posixpath.isabs(b"/foo"), True)
self.assertIs(posixpath.isabs(b"/foo/bar"), True)
self.assertIs(posixpath.isabs(b"foo/bar"), False)
def test_isabs(self):
self.assertIs(posixpath.isabs(""), False)
self.assertIs(posixpath.isabs("/"), True)
self.assertIs(posixpath.isabs("/foo"), True)
self.assertIs(posixpath.isabs("/foo/bar"), True)
self.assertIs(posixpath.isabs("foo/bar"), False)
def test_isabs(self):
self.assertIs(posixpath.isabs(""), False)
self.assertIs(posixpath.isabs("/"), True)
self.assertIs(posixpath.isabs("/foo"), True)
self.assertIs(posixpath.isabs("/foo/bar"), True)
self.assertIs(posixpath.isabs("foo/bar"), False)
def test_isabs(self):
self.assertIs(posixpath.isabs(""), False)
self.assertIs(posixpath.isabs("/"), True)
self.assertIs(posixpath.isabs("/foo"), True)
self.assertIs(posixpath.isabs("/foo/bar"), True)
self.assertIs(posixpath.isabs("foo/bar"), False)
self.assertIs(posixpath.isabs(b""), False)
self.assertIs(posixpath.isabs(b"/"), True)
self.assertIs(posixpath.isabs(b"/foo"), True)
self.assertIs(posixpath.isabs(b"/foo/bar"), True)
self.assertIs(posixpath.isabs(b"foo/bar"), False)
def test_pkgserv(httpget, pypistage, testapp):
pypistage.mock_simple("package", '<a href="/package-1.0.zip" />')
pypistage.mock_extfile("/package-1.0.zip", b"123")
r = testapp.get("/root/pypi/+simple/package")
assert r.status_code == 200
href = getfirstlink(r.text).get("href")
assert not posixpath.isabs(href)
url = URL(r.request.url).joinpath(href).url
r = testapp.get(url)
assert r.body == b"123"
def isabs_anywhere(filename):
"""Is `filename` an absolute path on any OS?"""
return ntpath.isabs(filename) or posixpath.isabs(filename)
def test_isabs(self):
self.assertIs(posixpath.isabs(""), False)
self.assertIs(posixpath.isabs("/"), True)
self.assertIs(posixpath.isabs("/foo"), True)
self.assertIs(posixpath.isabs("/foo/bar"), True)
self.assertIs(posixpath.isabs("foo/bar"), False)
def test_isabs(self):
self.assertIs(posixpath.isabs(""), False)
self.assertIs(posixpath.isabs("/"), True)
self.assertIs(posixpath.isabs("/foo"), True)
self.assertIs(posixpath.isabs("/foo/bar"), True)
self.assertIs(posixpath.isabs("foo/bar"), False)
self.assertIs(posixpath.isabs(b""), False)
self.assertIs(posixpath.isabs(b"/"), True)
self.assertIs(posixpath.isabs(b"/foo"), True)
self.assertIs(posixpath.isabs(b"/foo/bar"), True)
self.assertIs(posixpath.isabs(b"foo/bar"), False)
def test_isabs(self):
self.assertIs(posixpath.isabs(""), False)
self.assertIs(posixpath.isabs("/"), True)
self.assertIs(posixpath.isabs("/foo"), True)
self.assertIs(posixpath.isabs("/foo/bar"), True)
self.assertIs(posixpath.isabs("foo/bar"), False)
def isabs_anywhere(filename):
"""Is `filename` an absolute path on any OS?"""
return ntpath.isabs(filename) or posixpath.isabs(filename)
def isabs_anywhere(filename):
"""Is `filename` an absolute path on any OS?"""
return ntpath.isabs(filename) or posixpath.isabs(filename)
def test_isabs(self):
self.assertIs(posixpath.isabs(""), False)
self.assertIs(posixpath.isabs("/"), True)
self.assertIs(posixpath.isabs("/foo"), True)
self.assertIs(posixpath.isabs("/foo/bar"), True)
self.assertIs(posixpath.isabs("foo/bar"), False)
self.assertIs(posixpath.isabs(b""), False)
self.assertIs(posixpath.isabs(b"/"), True)
self.assertIs(posixpath.isabs(b"/foo"), True)
self.assertIs(posixpath.isabs(b"/foo/bar"), True)
self.assertIs(posixpath.isabs(b"foo/bar"), False)
def isabs_anywhere(filename):
"""Is `filename` an absolute path on any OS?"""
return ntpath.isabs(filename) or posixpath.isabs(filename)
def isabs_anywhere(filename):
"""Is `filename` an absolute path on any OS?"""
return ntpath.isabs(filename) or posixpath.isabs(filename)
def _ProcessSymbolOutput(self, lines):
"""Parses an addr2line symbol output and triggers the client callback."""
(_, callback_arg, _) = self._request_queue.popleft()
self.queue_size -= 1
innermost_sym_info = None
sym_info = None
for (line1, line2) in lines:
prev_sym_info = sym_info
name = line1 if not line1.startswith('?') else None
source_path = None
source_line = None
m = ELFSymbolizer.Addr2Line.SYM_ADDR_RE.match(line2)
if m:
if not m.group(1).startswith('?'):
source_path = m.group(1)
if not m.group(2).startswith('?'):
source_line = int(m.group(2))
else:
logging.warning('Got invalid symbol path from addr2line: %s', line2)
# In case disambiguation is on, and needed
was_ambiguous = False
disambiguated = False
if self._symbolizer.disambiguate:
if source_path and not posixpath.isabs(source_path):
path = self._symbolizer.disambiguation_table.get(source_path)
was_ambiguous = True
disambiguated = path is not None
source_path = path if disambiguated else source_path
# Use absolute paths (so that paths are consistent, as disambiguation
# uses absolute paths)
if source_path and not was_ambiguous:
source_path = os.path.abspath(source_path)
if source_path and self._symbolizer.strip_base_path:
# Strip the base path
source_path = re.sub('^' + self._symbolizer.strip_base_path,
self._symbolizer.source_root_path or '', source_path)
sym_info = ELFSymbolInfo(name, source_path, source_line, was_ambiguous,
disambiguated)
if prev_sym_info:
prev_sym_info.inlined_by = sym_info
if not innermost_sym_info:
innermost_sym_info = sym_info
self._processed_symbols_count += 1
self._symbolizer.callback(innermost_sym_info, callback_arg)
def _ProcessSymbolOutput(self, lines):
"""Parses an addr2line symbol output and triggers the client callback."""
(_, callback_arg, _) = self._request_queue.popleft()
self.queue_size -= 1
innermost_sym_info = None
sym_info = None
for (line1, line2) in lines:
prev_sym_info = sym_info
name = line1 if not line1.startswith('?') else None
source_path = None
source_line = None
m = ELFSymbolizer.Addr2Line.SYM_ADDR_RE.match(line2)
if m:
if not m.group(1).startswith('?'):
source_path = m.group(1)
if not m.group(2).startswith('?'):
source_line = int(m.group(2))
else:
logging.warning('Got invalid symbol path from addr2line: %s', line2)
# In case disambiguation is on, and needed
was_ambiguous = False
disambiguated = False
if self._symbolizer.disambiguate:
if source_path and not posixpath.isabs(source_path):
path = self._symbolizer.disambiguation_table.get(source_path)
was_ambiguous = True
disambiguated = path is not None
source_path = path if disambiguated else source_path
# Use absolute paths (so that paths are consistent, as disambiguation
# uses absolute paths)
if source_path and not was_ambiguous:
source_path = os.path.abspath(source_path)
if source_path and self._symbolizer.strip_base_path:
# Strip the base path
source_path = re.sub('^' + self._symbolizer.strip_base_path,
self._symbolizer.source_root_path or '', source_path)
sym_info = ELFSymbolInfo(name, source_path, source_line, was_ambiguous,
disambiguated)
if prev_sym_info:
prev_sym_info.inlined_by = sym_info
if not innermost_sym_info:
innermost_sym_info = sym_info
self._processed_symbols_count += 1
self._symbolizer.callback(innermost_sym_info, callback_arg)
def _ProcessSymbolOutput(self, lines):
"""Parses an addr2line symbol output and triggers the client callback."""
(_, callback_arg, _) = self._request_queue.popleft()
self.queue_size -= 1
innermost_sym_info = None
sym_info = None
for (line1, line2) in lines:
prev_sym_info = sym_info
name = line1 if not line1.startswith('?') else None
source_path = None
source_line = None
m = ELFSymbolizer.Addr2Line.SYM_ADDR_RE.match(line2)
if m:
if not m.group(1).startswith('?'):
source_path = m.group(1)
if not m.group(2).startswith('?'):
source_line = int(m.group(2))
else:
logging.warning('Got invalid symbol path from addr2line: %s', line2)
# In case disambiguation is on, and needed
was_ambiguous = False
disambiguated = False
if self._symbolizer.disambiguate:
if source_path and not posixpath.isabs(source_path):
path = self._symbolizer.disambiguation_table.get(source_path)
was_ambiguous = True
disambiguated = path is not None
source_path = path if disambiguated else source_path
# Use absolute paths (so that paths are consistent, as disambiguation
# uses absolute paths)
if source_path and not was_ambiguous:
source_path = os.path.abspath(source_path)
if source_path and self._symbolizer.strip_base_path:
# Strip the base path
source_path = re.sub('^' + self._symbolizer.strip_base_path,
self._symbolizer.source_root_path or '', source_path)
sym_info = ELFSymbolInfo(name, source_path, source_line, was_ambiguous,
disambiguated)
if prev_sym_info:
prev_sym_info.inlined_by = sym_info
if not innermost_sym_info:
innermost_sym_info = sym_info
self._processed_symbols_count += 1
self._symbolizer.callback(innermost_sym_info, callback_arg)
def _ProcessSymbolOutput(self, lines):
"""Parses an addr2line symbol output and triggers the client callback."""
(_, callback_arg, _) = self._request_queue.popleft()
self.queue_size -= 1
innermost_sym_info = None
sym_info = None
for (line1, line2) in lines:
prev_sym_info = sym_info
name = line1 if not line1.startswith('?') else None
source_path = None
source_line = None
m = ELFSymbolizer.Addr2Line.SYM_ADDR_RE.match(line2)
if m:
if not m.group(1).startswith('?'):
source_path = m.group(1)
if not m.group(2).startswith('?'):
source_line = int(m.group(2))
else:
logging.warning('Got invalid symbol path from addr2line: %s' % line2)
# In case disambiguation is on, and needed
was_ambiguous = False
disambiguated = False
if self._symbolizer.disambiguate:
if source_path and not posixpath.isabs(source_path):
path = self._symbolizer.disambiguation_table.get(source_path)
was_ambiguous = True
disambiguated = path is not None
source_path = path if disambiguated else source_path
# Use absolute paths (so that paths are consistent, as disambiguation
# uses absolute paths)
if source_path and not was_ambiguous:
source_path = os.path.abspath(source_path)
if source_path and self._symbolizer.strip_base_path:
# Strip the base path
source_path = re.sub('^' + self._symbolizer.strip_base_path,
self._symbolizer.source_root_path or '', source_path)
sym_info = ELFSymbolInfo(name, source_path, source_line, was_ambiguous,
disambiguated)
if prev_sym_info:
prev_sym_info.inlined_by = sym_info
if not innermost_sym_info:
innermost_sym_info = sym_info
self._processed_symbols_count += 1
self._symbolizer.callback(innermost_sym_info, callback_arg)
def _ProcessSymbolOutput(self, lines):
"""Parses an addr2line symbol output and triggers the client callback."""
(_, callback_arg, _) = self._request_queue.popleft()
self.queue_size -= 1
innermost_sym_info = None
sym_info = None
for (line1, line2) in lines:
prev_sym_info = sym_info
name = line1 if not line1.startswith('?') else None
source_path = None
source_line = None
m = ELFSymbolizer.Addr2Line.SYM_ADDR_RE.match(line2)
if m:
if not m.group(1).startswith('?'):
source_path = m.group(1)
if not m.group(2).startswith('?'):
source_line = int(m.group(2))
else:
logging.warning('Got invalid symbol path from addr2line: %s', line2)
# In case disambiguation is on, and needed
was_ambiguous = False
disambiguated = False
if self._symbolizer.disambiguate:
if source_path and not posixpath.isabs(source_path):
path = self._symbolizer.disambiguation_table.get(source_path)
was_ambiguous = True
disambiguated = path is not None
source_path = path if disambiguated else source_path
# Use absolute paths (so that paths are consistent, as disambiguation
# uses absolute paths)
if source_path and not was_ambiguous:
source_path = os.path.abspath(source_path)
if source_path and self._symbolizer.strip_base_path:
# Strip the base path
source_path = re.sub('^' + self._symbolizer.strip_base_path,
self._symbolizer.source_root_path or '', source_path)
sym_info = ELFSymbolInfo(name, source_path, source_line, was_ambiguous,
disambiguated)
if prev_sym_info:
prev_sym_info.inlined_by = sym_info
if not innermost_sym_info:
innermost_sym_info = sym_info
self._processed_symbols_count += 1
self._symbolizer.callback(innermost_sym_info, callback_arg)