aboutsummaryrefslogtreecommitdiff
blob: 5d1765ced6dfc8cf805fd3eb6b76af12119dbd6e (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright 2010-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

__all__ = ["cacheddir", "listdir"]

import errno
import stat


from portage import os
from portage.const import VCS_DIRS
from portage.exception import DirectoryNotFound, PermissionDenied, PortageException
from portage.util import normalize_path

# The global dircache is no longer supported, since it could
# be a memory leak for API consumers. Any cacheddir callers
# should use higher-level caches instead, when necessary.
# TODO: Remove dircache variable after stable portage does
# not use is (keep it for now, in case API consumers clear
# it manually).
dircache = {}


def cacheddir(
    my_original_path, ignorecvs, ignorelist, EmptyOnError, followSymlinks=True
):
    mypath = normalize_path(my_original_path)
    try:
        pathstat = os.stat(mypath)
        if not stat.S_ISDIR(pathstat.st_mode):
            raise DirectoryNotFound(mypath)
    except OSError as e:
        if e.errno == PermissionDenied.errno:
            raise PermissionDenied(mypath)
        del e
        return [], []
    except PortageException:
        return [], []
    else:
        try:
            fpaths = os.listdir(mypath)
        except OSError as e:
            if e.errno != errno.EACCES:
                raise
            del e
            raise PermissionDenied(mypath)
        ftype = []
        for x in fpaths:
            try:
                if followSymlinks:
                    pathstat = os.stat(mypath + "/" + x)
                else:
                    pathstat = os.lstat(mypath + "/" + x)

                if stat.S_ISREG(pathstat[stat.ST_MODE]):
                    ftype.append(0)
                elif stat.S_ISDIR(pathstat[stat.ST_MODE]):
                    ftype.append(1)
                elif stat.S_ISLNK(pathstat[stat.ST_MODE]):
                    ftype.append(2)
                else:
                    ftype.append(3)
            except OSError:
                ftype.append(3)

    if ignorelist or ignorecvs:
        ret_list = []
        ret_ftype = []
        for file_path, file_type in zip(fpaths, ftype):
            if file_path in ignorelist:
                pass
            elif ignorecvs:
                if file_path[:2] != ".#" and not (
                    file_type == 1 and file_path in VCS_DIRS
                ):
                    ret_list.append(file_path)
                    ret_ftype.append(file_type)
    else:
        ret_list = fpaths
        ret_ftype = ftype

    return ret_list, ret_ftype


def listdir(
    mypath,
    recursive=False,
    filesonly=False,
    ignorecvs=False,
    ignorelist=[],
    followSymlinks=True,
    EmptyOnError=False,
    dirsonly=False,
):
    """
    Portage-specific implementation of os.listdir

    @param mypath: Path whose contents you wish to list
    @type mypath: String
    @param recursive: Recursively scan directories contained within mypath
    @type recursive: Boolean
    @param filesonly; Only return files, not more directories
    @type filesonly: Boolean
    @param ignorecvs: Ignore VCS directories
    @type ignorecvs: Boolean
    @param ignorelist: List of filenames/directories to exclude
    @type ignorelist: List
    @param followSymlinks: Follow Symlink'd files and directories
    @type followSymlinks: Boolean
    @param EmptyOnError: Return [] if an error occurs (deprecated, always True)
    @type EmptyOnError: Boolean
    @param dirsonly: Only return directories.
    @type dirsonly: Boolean
    @rtype: List
    @return: A list of files and directories (or just files or just directories) or an empty list.
    """

    fpaths, ftype = cacheddir(
        mypath, ignorecvs, ignorelist, EmptyOnError, followSymlinks
    )

    if fpaths is None:
        fpaths = []
    if ftype is None:
        ftype = []

    if not (filesonly or dirsonly or recursive):
        return fpaths

    if recursive:
        stack = list(zip(fpaths, ftype))
        fpaths = []
        ftype = []
        while stack:
            file_path, file_type = stack.pop()
            fpaths.append(file_path)
            ftype.append(file_type)
            if file_type == 1:
                subdir_list, subdir_types = cacheddir(
                    os.path.join(mypath, file_path),
                    ignorecvs,
                    ignorelist,
                    EmptyOnError,
                    followSymlinks,
                )
                stack.extend(
                    (os.path.join(file_path, x), x_type)
                    for x, x_type in zip(subdir_list, subdir_types)
                )

    if filesonly:
        fpaths = [x for x, x_type in zip(fpaths, ftype) if x_type == 0]

    elif dirsonly:
        fpaths = [x for x, x_type in zip(fpaths, ftype) if x_type == 1]

    return fpaths