_counterfeit.py
1 # Copyright (C) 2025 Armin "Era" Ramezani <e@4d2.org> 2 # 3 # This file is a part of patchman. 4 # 5 # patchman is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public 6 # License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 7 # version. 8 # 9 # patchman is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 10 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 11 # details. 12 # 13 # You should have received a copy of the GNU Lesser General Public License along with patchman. If not, see 14 # <https://www.gnu.org/licenses/>. 15 # 16 """Stuff for compatibility.""" 17 18 import os 19 import errno 20 21 try: 22 from typing import TYPE_CHECKING 23 except ImportError: 24 TYPE_CHECKING = False 25 26 if TYPE_CHECKING: 27 from typing import List 28 29 30 def makedirs(path, mode = 0o777, exist_ok=False): # type: (str, int, bool) -> None 31 if exist_ok: 32 if not os.path.exists(path): 33 try: 34 os.makedirs(path, mode) 35 except (IOError, OSError) as e: 36 if e.errno != errno.EEXIST: 37 raise e 38 else: 39 os.makedirs(path, mode) 40 41 42 def scandir(path): # type: (str) -> List[PseudoFile] 43 return [PseudoFile(path, file_name) for file_name in os.listdir(path)] 44 45 46 class PseudoFile(object): 47 def __init__(self, parent, name): # type: (str, str) -> None 48 self._name = name 49 self._path = os.path.join(parent, name) 50 51 @property 52 def name(self): # type: () -> str 53 return self._name 54 55 @property 56 def path(self): # type: () -> str 57 return self._path 58 59 def is_dir(self): # type: () -> bool 60 return os.path.isdir(self._path) 61 62 def is_file(self): # type: () -> bool 63 return os.path.isfile(self._path)