本文整理汇总了Python中posixpath.islink函数的典型用法代码示例。如果您正苦于以下问题:Python islink函数的具体用法?Python islink怎么用?Python islink使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了islink函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_islink
def test_islink(self):
self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
f = open(test_support.TESTFN + "1", "wb")
try:
f.write("foo")
f.close()
self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
if hasattr(os, "symlink"):
os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
os.remove(test_support.TESTFN + "1")
self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False)
self.assertIs(posixpath.lexists(test_support.TESTFN + "2"), True)
finally:
if not f.close():
f.close()
try:
os.remove(test_support.TESTFN + "1")
except os.error:
pass
try:
os.remove(test_support.TESTFN + "2")
except os.error:
pass
self.assertRaises(TypeError, posixpath.islink)
示例2: test_islink
def test_islink(self):
self.assertIs(posixpath.islink(support.TESTFN + "1"), False)
self.assertIs(posixpath.lexists(support.TESTFN + "2"), False)
with open(support.TESTFN + "1", "wb") as f:
f.write(b"foo")
self.assertIs(posixpath.islink(support.TESTFN + "1"), False)
if support.can_symlink():
os.symlink(support.TESTFN + "1", support.TESTFN + "2")
self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
os.remove(support.TESTFN + "1")
self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
self.assertIs(posixpath.exists(support.TESTFN + "2"), False)
self.assertIs(posixpath.lexists(support.TESTFN + "2"), True)
示例3: getAttributes
def getAttributes(self, path):
attributes = {}
sys_path = "/sys%s" % path
try:
names = os.listdir(sys_path)
except OSError:
return attributes
for name in names:
name_path = posixpath.join(sys_path, name)
if name[0] == "." \
or name in ["dev", "uevent"] \
or posixpath.isdir(name_path) \
or posixpath.islink(name_path):
continue
try:
value = open(name_path, "r").read().strip()
except IOError:
continue
value = value.split("\n")[0]
if [c for c in value if not isprint(c)]:
continue
attributes[name] = value
return attributes
示例4: generate
def generate(filename, str):
if type(str) != type(''):
raise GenerateOnlyTakesStrings, type(str)
happy = 0
if posixpath.islink(filename):
print '(Blowing away symlink '+filename+' to make way for autogenerated file)'
os.remove(filename)
try:
o = open(filename, 'r')
snow = o.read()
o.close()
if snow == str:
happy = 1
except:
# it is not there; pass
pass
if not happy:
try:
o = open(filename, 'w')
o.write(str)
o.close()
except:
print 'Error: Unable to generate '+filename
sys.exit(1)
else:
#print '(No changes to '+filename+')'
pass
示例5: swap_dir
def swap_dir(rootpath, path):
"""Swap a symlink with its target directory.
Args:
rootpath: Rootpath for tag conversions.
path: Path of target symlink.
"""
target = path
if posixpath.islink(target) and posixpath.isdir(target):
here = target
there = pathlib.readlink(target)
# here is the symlink
# there is the dir
here_tag = tagnames.path2tag(rootpath, here)
there_tag = tagnames.path2tag(rootpath, there)
dtags.remove_tag(here, here_tag)
dtags.add_tag(here, there_tag)
os.unlink(here)
# here is now nothing
# there is now the dir
os.rename(there, here)
# here is now the dir
# there is now nothing
os.symlink(here, there)
else:
raise ValueError('{} is not a symlink to a directory'.format(target))
示例6: test_swap_dir
def test_swap_dir(self):
with patch('dantalian.dtags.add_tag', autospec=True) as mock_add, \
patch('dantalian.dtags.remove_tag', autospec=True) as mock_rm:
base.swap_dir(self.root, 'bag/apple')
self.assertTrue(posixpath.islink('apple'))
self.assertTrue(posixpath.isdir('bag/apple'))
mock_rm.assert_called_with('bag/apple', '//bag/apple')
mock_add.assert_called_with('bag/apple', '//apple')
示例7: test_islink
def test_islink(self):
self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
f = open(test_support.TESTFN + "1", "wb")
try:
f.write("foo")
f.close()
self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
if hasattr(os, 'symlink'):
os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
os.remove(test_support.TESTFN + "1")
self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False)
self.assertIs(posixpath.lexists(test_support.TESTFN + "2"), True)
finally:
if not f.close():
f.close()
示例8: test_islink
def test_islink(self):
self.assertIs(posixpath.islink(support.TESTFN + "1"), False)
f = open(support.TESTFN + "1", "wb")
try:
f.write(b"foo")
f.close()
self.assertIs(posixpath.islink(support.TESTFN + "1"), False)
if support.can_symlink():
os.symlink(support.TESTFN + "1", support.TESTFN + "2")
self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
os.remove(support.TESTFN + "1")
self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
self.assertIs(posixpath.exists(support.TESTFN + "2"), False)
self.assertIs(posixpath.lexists(support.TESTFN + "2"), True)
finally:
if not f.close():
f.close()
示例9: clean_symlinks
def clean_symlinks(dirpath):
"""Remove all broken symlinks under the given directory."""
# Broken symlinks appear as files, so we skip directories.
for dirpath, _, filenames in os.walk(dirpath):
for filename in filenames:
path = posixpath.join(dirpath, filename)
if posixpath.islink(path) and not posixpath.exists(path):
os.unlink(path)
示例10: bus
def bus(self):
sys_path = posixpath.join(
"/sys%s" % self._environment["DEVPATH"], "subsystem")
if posixpath.islink(sys_path):
link = os.readlink(sys_path)
if "/" in link:
return posixpath.basename(link)
return None
示例11: unlink
def unlink(rootpath, path):
"""Unlink given path.
If the target is a directory without any other links, raise OSError.
"""
target = path
# We unlink the target. However, if it is a directory, we want to swap it
# out for one of its symlinks, then unlink the symlink. If the directory
# doesn't have any tags, then we fail.
if posixpath.isdir(target):
if not posixpath.islink(target):
tags = dtags.list_tags(target)
if not tags:
raise oserrors.is_a_directory(target)
swap_candidate = tagnames.tag2path(rootpath, tags[0])
swap_dir(rootpath, swap_candidate)
assert posixpath.islink(target)
dtags.remove_tag(target, tagnames.path2tag(rootpath, target))
os.unlink(target)
示例12: file_exists
def file_exists(file_name, type = 'file'):
if len(file_name) == 0:
return 0
else:
if exists(file_name):
if type == 'file' and isfile(file_name):
return 1
elif type == 'dir' and isdir(file_name):
return 1
else:
if islink(file_name):
print "INFO: using '%s' which is a link" % file_name
return 1
else:
return 0
else:
return 0
示例13: canonicalize
def canonicalize(canon, subpath, linkcache = {}, verbose=0):
if verbose: print canon, ' -> [',subpath,']'
# split the subpath in to components
pathsplit = string.split(subpath, '/')
# if we haven't got any, then we get returned ['']
if len(pathsplit) == 1 and pathsplit[0] == '':
return canon
# make linksrc the thing under consideration; ie the top element of subpath
linksrc = canon + '/' + pathsplit[0]
# define the variable linkdest to be None if linksrc is not a link,
# else make it be the place that linksrc is pointing to
if linkcache.has_key(linksrc):
linkdest = linkcache[linksrc]
else:
linkdest = None
if posixpath.islink(linksrc):
linkdest = os.readlink(linksrc)
linkcache[linksrc] = linkdest
if linkdest:
# linksrc is a link, deal with it
if linkdest[0] == '/':
# we have found an absolute link
# so restart the resolution, using linkdest as our subpath
if verbose: print 'abslink',linkdest
return canonicalize('', linkdest[1:] + '/' + string.join(pathsplit[1:], '/'), linkcache, verbose)
else:
# we have found a relative link
# peel off any .. by stripping away canon
while linkdest[0:2] == '../':
if verbose: print 'uplink',linkdest
canon = string.join(string.split(canon, '/')[:-1], '/')
linkdest = linkdest[3:]
if verbose: print 'rellink',linkdest
# recurse with the new (possible reduced) canon and the stripped down path
return canonicalize(canon, linkdest + '/' + string.join(pathsplit[1:], '/'), linkcache, verbose)
else:
# we take this branch if the linksrc was absolute, or it did not exist
if verbose: print 'abs ',pathsplit[0]
return canonicalize(canon + '/' + pathsplit[0], string.join(pathsplit[1:], '/'), linkcache, verbose)
示例14: is_symlink
def is_symlink(self):
return path.islink(self.path)
示例15: and
if item.get_name() == 'boot_images_'+treeinfo.target_name:
instname = 'image'
if item.get_class() in ['program', 'posixprogram', 'module']:
instname = item.get_binary_object_name()
if item.get_make_flag('donotinstall'):
instname = ''
if instname != '':
if item.options['value'] == 2:
mapping[instname] = item.get_path()
else:
forbidden.append(instname)
if item.get_make_flag('install'):
for instname in item.get_make_flag('install'):
mapping[instname] = item.get_path()
print 'Omitting ',forbidden
print 'Linking ',len(mapping.keys())
files = os.listdir('.')
for file in files:
if posixpath.islink(file) and file != 'Makefile' and (file in forbidden or file in mapping.keys()):
os.unlink(file)
for file in mapping.keys():
install(buildpath, mapping[file], file)
print