2

current running code is:

for y in checkdirlist:
    if (y in no_exec or y.endswith(".ebuild")) and \
        stat.S_IMODE(os.stat(os.path.join(checkdir, y)).st_mode) & 0o111:

and stat fails with symlink files.

I want to add some check if y is symlink or not.

lstat solves the problem with system error but it's not over in general

Traceback (most recent call last):
  File "/usr/bin/repoman", line 1385, in <module>
    myaux = dict(zip(allvars, portdb.aux_get(cpv, allvars)))
  File "/usr/lib/portage/pym/portage/dbapi/porttree.py", line 435, in aux_get
    myebuild, mylocation = self.findname2(mycpv, mytree)
  File "/usr/lib/portage/pym/portage/dbapi/porttree.py", line 308, in findname2
    raise InvalidPackageName(mycpv)
portage.exception.InvalidPackageName: dev-haskell/.#hakyll-9999

I want to filter those files so I need to know which ones are symlinks.

cnd
  • 32,616
  • 62
  • 183
  • 313
  • 1
    Have you considered using `os.lstat` instead of `os.stat`? – Amber Oct 15 '12 at 07:12
  • No, I'm very new with python. And just want to patch existing project. What is core difference between stat and lstat? – cnd Oct 15 '12 at 07:20
  • 2
    `stat` follows symbolic links and returns the stat of the file pointed to via links (or fails if any of them refer to a non-existent path), while `lstat` returns the stat of the link and does not follow links. – Dan D. Oct 15 '12 at 07:34
  • 5
    Is there some reason than `os.path.islink(y)` won't work? [Non-rhetorical, by the way -- there are often corner cases I don't think of.] – DSM Oct 15 '12 at 08:05
  • 1
    by the way, you don't need the backslash there, inside parentheses you can break lines freely – unddoch Oct 15 '12 at 08:34
  • so question is solved now, must I delete it? @DSM islink works just fine – cnd Oct 15 '12 at 08:46
  • @pythonm my IDE highlights red without blackslashes (kdevelop) – cnd Oct 15 '12 at 08:47
  • @nCdy: I'll make my comment an answer. – DSM Oct 15 '12 at 14:04

1 Answers1

4

From the docs, you probably want os.path.islink:

os.path.islink(path)

Return True if path refers to a directory entry that is a symbolic link. 
Always False if symbolic links are not supported.
DSM
  • 342,061
  • 65
  • 592
  • 494