aboutsummaryrefslogtreecommitdiff
path: root/pyfakefs/helpers.py
blob: ba75d2a300c8747937e779452fcb6997c8d22da9 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Helper classes use for fake file system implementation."""
import io
import locale
import os
import platform
import stat
import sys
import time
from copy import copy
from stat import S_IFLNK
from typing import Union, Optional, Any, AnyStr, overload, cast

IS_PYPY = platform.python_implementation() == 'PyPy'
IS_WIN = sys.platform == 'win32'
IN_DOCKER = os.path.exists('/.dockerenv')

AnyPath = Union[AnyStr, os.PathLike]


def is_int_type(val: Any) -> bool:
    """Return True if `val` is of integer type."""
    return isinstance(val, int)


def is_byte_string(val: Any) -> bool:
    """Return True if `val` is a bytes-like object, False for a unicode
    string."""
    return not hasattr(val, 'encode')


def is_unicode_string(val: Any) -> bool:
    """Return True if `val` is a unicode string, False for a bytes-like
    object."""
    return hasattr(val, 'encode')


@overload
def make_string_path(dir_name: AnyStr) -> AnyStr: ...


@overload
def make_string_path(dir_name: os.PathLike) -> str: ...


def make_string_path(dir_name: AnyPath) -> AnyStr:
    return cast(AnyStr, os.fspath(dir_name))


def to_string(path: Union[AnyStr, Union[str, bytes]]) -> str:
    """Return the string representation of a byte string using the preferred
     encoding, or the string itself if path is a str."""
    if isinstance(path, bytes):
        return path.decode(locale.getpreferredencoding(False))
    return path


def to_bytes(path: Union[AnyStr, Union[str, bytes]]) -> bytes:
    """Return the bytes representation of a string using the preferred
     encoding, or the byte string itself if path is a byte string."""
    if isinstance(path, str):
        return bytes(path, locale.getpreferredencoding(False))
    return path


def join_strings(s1: AnyStr, s2: AnyStr) -> AnyStr:
    """This is a bit of a hack to satisfy mypy - may be refactored."""
    return s1 + s2


def real_encoding(encoding: Optional[str]) -> Optional[str]:
    """Since Python 3.10, the new function ``io.text_encoding`` returns
    "locale" as the encoding if None is defined. This will be handled
    as no encoding in pyfakefs."""
    if sys.version_info >= (3, 10):
        return encoding if encoding != "locale" else None
    return encoding


def now():
    return time.time()


@overload
def matching_string(matched: bytes, string: AnyStr) -> bytes: ...


@overload
def matching_string(matched: str, string: AnyStr) -> str: ...


@overload
def matching_string(matched: AnyStr, string: None) -> None: ...


def matching_string(  # type: ignore[misc]
        matched: AnyStr, string: Optional[AnyStr]) -> Optional[AnyStr]:
    """Return the string as byte or unicode depending
    on the type of matched, assuming string is an ASCII string.
    """
    if string is None:
        return string
    if isinstance(matched, bytes) and isinstance(string, str):
        return string.encode(locale.getpreferredencoding(False))
    return string


class FakeStatResult:
    """Mimics os.stat_result for use as return type of `stat()` and similar.
    This is needed as `os.stat_result` has no possibility to set
    nanosecond times directly.
    """
    _stat_float_times: bool = True

    def __init__(self, is_windows: bool, user_id: int, group_id: int,
                 initial_time: Optional[float] = None):
        self._use_float: Optional[bool] = None
        self.st_mode: int = 0
        self.st_ino: Optional[int] = None
        self.st_dev: int = 0
        self.st_nlink: int = 0
        self.st_uid: int = user_id
        self.st_gid: int = group_id
        self._st_size: int = 0
        self.is_windows: bool = is_windows
        self._st_atime_ns: int = int((initial_time or 0) * 1e9)
        self._st_mtime_ns: int = self._st_atime_ns
        self._st_ctime_ns: int = self._st_atime_ns

    @property
    def use_float(self) -> bool:
        if self._use_float is None:
            return self.stat_float_times()
        return self._use_float

    @use_float.setter
    def use_float(self, val: bool) -> None:
        self._use_float = val

    def __eq__(self, other: Any) -> bool:
        return (
                isinstance(other, FakeStatResult) and
                self._st_atime_ns == other._st_atime_ns and
                self._st_ctime_ns == other._st_ctime_ns and
                self._st_mtime_ns == other._st_mtime_ns and
                self.st_size == other.st_size and
                self.st_gid == other.st_gid and
                self.st_uid == other.st_uid and
                self.st_nlink == other.st_nlink and
                self.st_dev == other.st_dev and
                self.st_ino == other.st_ino and
                self.st_mode == other.st_mode
        )

    def __ne__(self, other: Any) -> bool:
        return not self == other

    def copy(self) -> "FakeStatResult":
        """Return a copy where the float usage is hard-coded to mimic the
        behavior of the real os.stat_result.
        """
        stat_result = copy(self)
        stat_result.use_float = self.use_float
        return stat_result

    def set_from_stat_result(self, stat_result: os.stat_result) -> None:
        """Set values from a real os.stat_result.
        Note: values that are controlled by the fake filesystem are not set.
        This includes st_ino, st_dev and st_nlink.
        """
        self.st_mode = stat_result.st_mode
        self.st_uid = stat_result.st_uid
        self.st_gid = stat_result.st_gid
        self._st_size = stat_result.st_size
        self._st_atime_ns = stat_result.st_atime_ns
        self._st_mtime_ns = stat_result.st_mtime_ns
        self._st_ctime_ns = stat_result.st_ctime_ns

    @classmethod
    def stat_float_times(cls, newvalue: Optional[bool] = None) -> bool:
        """Determine whether a file's time stamps are reported as floats
        or ints.

        Calling without arguments returns the current value.
        The value is shared by all instances of FakeOsModule.

        Args:
            newvalue: If `True`, mtime, ctime, atime are reported as floats.
                Otherwise, they are returned as ints (rounding down).
        """
        if newvalue is not None:
            cls._stat_float_times = bool(newvalue)
        return cls._stat_float_times

    @property
    def st_ctime(self) -> Union[int, float]:
        """Return the creation time in seconds."""
        ctime = self._st_ctime_ns / 1e9
        return ctime if self.use_float else int(ctime)

    @st_ctime.setter
    def st_ctime(self, val: Union[int, float]) -> None:
        """Set the creation time in seconds."""
        self._st_ctime_ns = int(val * 1e9)

    @property
    def st_atime(self) -> Union[int, float]:
        """Return the access time in seconds."""
        atime = self._st_atime_ns / 1e9
        return atime if self.use_float else int(atime)

    @st_atime.setter
    def st_atime(self, val: Union[int, float]) -> None:
        """Set the access time in seconds."""
        self._st_atime_ns = int(val * 1e9)

    @property
    def st_mtime(self) -> Union[int, float]:
        """Return the modification time in seconds."""
        mtime = self._st_mtime_ns / 1e9
        return mtime if self.use_float else int(mtime)

    @st_mtime.setter
    def st_mtime(self, val: Union[int, float]) -> None:
        """Set the modification time in seconds."""
        self._st_mtime_ns = int(val * 1e9)

    @property
    def st_size(self) -> int:
        if self.st_mode & S_IFLNK == S_IFLNK and self.is_windows:
            return 0
        return self._st_size

    @st_size.setter
    def st_size(self, val: int) -> None:
        self._st_size = val

    @property
    def st_file_attributes(self) -> int:
        if not self.is_windows:
            raise AttributeError("module 'os.stat_result' "
                                 "has no attribute 'st_file_attributes'")
        mode = 0
        st_mode = self.st_mode
        if st_mode & stat.S_IFDIR:
            mode |= stat.FILE_ATTRIBUTE_DIRECTORY
        if st_mode & stat.S_IFREG:
            mode |= stat.FILE_ATTRIBUTE_NORMAL
        if st_mode & (stat.S_IFCHR | stat.S_IFBLK):
            mode |= stat.FILE_ATTRIBUTE_DEVICE
        if st_mode & stat.S_IFLNK:
            mode |= stat.FILE_ATTRIBUTE_REPARSE_POINT
        return mode

    @property
    def st_reparse_tag(self) -> int:
        if not self.is_windows or sys.version_info < (3, 8):
            raise AttributeError("module 'os.stat_result' "
                                 "has no attribute 'st_reparse_tag'")
        if self.st_mode & stat.S_IFLNK:
            return stat.IO_REPARSE_TAG_SYMLINK  # type: ignore[attr-defined]
        return 0

    def __getitem__(self, item: int) -> Optional[int]:
        """Implement item access to mimic `os.stat_result` behavior."""
        import stat

        if item == stat.ST_MODE:
            return self.st_mode
        if item == stat.ST_INO:
            return self.st_ino
        if item == stat.ST_DEV:
            return self.st_dev
        if item == stat.ST_NLINK:
            return self.st_nlink
        if item == stat.ST_UID:
            return self.st_uid
        if item == stat.ST_GID:
            return self.st_gid
        if item == stat.ST_SIZE:
            return self.st_size
        if item == stat.ST_ATIME:
            # item access always returns int for backward compatibility
            return int(self.st_atime)
        if item == stat.ST_MTIME:
            return int(self.st_mtime)
        if item == stat.ST_CTIME:
            return int(self.st_ctime)
        raise ValueError('Invalid item')

    @property
    def st_atime_ns(self) -> int:
        """Return the access time in nanoseconds."""
        return self._st_atime_ns

    @st_atime_ns.setter
    def st_atime_ns(self, val: int) -> None:
        """Set the access time in nanoseconds."""
        self._st_atime_ns = val

    @property
    def st_mtime_ns(self) -> int:
        """Return the modification time in nanoseconds."""
        return self._st_mtime_ns

    @st_mtime_ns.setter
    def st_mtime_ns(self, val: int) -> None:
        """Set the modification time of the fake file in nanoseconds."""
        self._st_mtime_ns = val

    @property
    def st_ctime_ns(self) -> int:
        """Return the creation time in nanoseconds."""
        return self._st_ctime_ns

    @st_ctime_ns.setter
    def st_ctime_ns(self, val: int) -> None:
        """Set the creation time of the fake file in nanoseconds."""
        self._st_ctime_ns = val


class BinaryBufferIO(io.BytesIO):
    """Stream class that handles byte contents for files."""

    def __init__(self, contents: Optional[bytes]):
        super().__init__(contents or b'')

    def putvalue(self, value: bytes) -> None:
        self.write(value)


class TextBufferIO(io.TextIOWrapper):
    """Stream class that handles Python string contents for files.
    """

    def __init__(self, contents: Optional[bytes] = None,
                 newline: Optional[str] = None,
                 encoding: Optional[str] = None,
                 errors: str = 'strict'):
        self._bytestream = io.BytesIO(contents or b'')
        super().__init__(self._bytestream, encoding, errors, newline)

    def getvalue(self) -> bytes:
        return self._bytestream.getvalue()

    def putvalue(self, value: bytes) -> None:
        self._bytestream.write(value)