osutils.py (3181B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\brennivin\osutils.py 2 import binascii as _binascii 3 import contextlib as _contextlib 4 import errno as _errno 5 import fnmatch as _fnmatch 6 import ntpath 7 import os as _os 8 import shutil as _shutil 9 import stat as _stat 10 import tempfile as _tempfile 11 import threading as _threading 12 altsep = _os.altsep 13 if altsep is None: 14 altsep = _os.sep 15 16 def abspathex(path, relative_to, _ignore_this = False): 17 absRelativeTo = _os.path.abspath(relative_to) 18 with change_cwd(absRelativeTo): 19 if _ignore_this: 20 raise ArithmeticError 21 return _os.path.abspath(path) 22 23 24 _changecwd_lock = _threading.Lock() 25 26 @_contextlib.contextmanager 27 def change_cwd(cwd): 28 orig = None 29 _changecwd_lock.acquire() 30 try: 31 orig = _os.getcwd() 32 _os.chdir(cwd) 33 yield 34 finally: 35 if orig is not None: 36 _os.chdir(orig) 37 _changecwd_lock.release() 38 39 40 @_contextlib.contextmanager 41 def change_environ(key, newvalue): 42 oldvalue = _os.environ.get(key, None) 43 if newvalue is None: 44 _os.environ.pop(key, None) 45 else: 46 _os.environ[key] = newvalue 47 try: 48 yield 49 finally: 50 if oldvalue is None: 51 _os.environ.pop(key, None) 52 else: 53 _os.environ[key] = oldvalue 54 55 56 def change_ext(path, ext): 57 root, ext_ = _os.path.splitext(path) 58 ext_ = ext 59 return root + ext_ 60 61 62 def copy(src, dst): 63 dirname = _os.path.dirname(dst) 64 if not _os.path.isdir(dirname): 65 _os.makedirs(dirname) 66 _shutil.copy(src, dst) 67 68 69 def crc_from_filename(filename): 70 with open(filename, 'rb') as f: 71 data = f.read() 72 return _binascii.crc32(data) & 4294967295L 73 74 75 def iter_files(directory, pattern = '*'): 76 for root, dirs, files in _os.walk(directory): 77 for basename in files: 78 if _fnmatch.fnmatch(basename, pattern): 79 filename = _os.path.join(root, basename) 80 yield filename 81 82 83 def listdirex(path, pattern = '*.*'): 84 return [ _os.path.join(path, fn) for fn in _os.listdir(path) if _fnmatch.fnmatch(fn, pattern) ] 85 86 87 def makedirs(path, mode = 511): 88 try: 89 _os.makedirs(path, mode) 90 except OSError as err: 91 if err.errno != _errno.EEXIST: 92 raise 93 94 return path 95 96 97 def mktemp(*args, **kwargs): 98 handle, filename = _tempfile.mkstemp(*args, **kwargs) 99 _os.close(handle) 100 return filename 101 102 103 def path_components(path): 104 parts = [] 105 while True: 106 newpath, tail = ntpath.split(path) 107 if newpath == path: 108 if path: 109 parts.append(path) 110 break 111 parts.append(tail) 112 path = newpath 113 114 parts.reverse() 115 return parts 116 117 118 def purename(filename): 119 if filename is None: 120 raise TypeError('filename cannot be None.') 121 f = _os.path.basename(filename) 122 return _os.path.splitext(f)[0] 123 124 125 def set_readonly(path, state): 126 mode = _stat.S_IREAD if state else _stat.S_IWRITE 127 _os.chmod(path, mode) 128 129 130 def split3(path): 131 dirname, filename = _os.path.split(path) 132 filename, ext = _os.path.splitext(filename) 133 return (dirname, filename, ext)