我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用stat.S_IFMT。
def assertS_IS(self, name, mode): # test format, lstrip is for S_IFIFO fmt = getattr(stat, "S_IF" + name.lstrip("F")) self.assertEqual(stat.S_IFMT(mode), fmt) # test that just one function returns true testname = "S_IS" + name for funcname in self.format_funcs: func = getattr(stat, funcname, None) if func is None: if funcname == testname: raise ValueError(funcname) continue if funcname == testname: self.assertTrue(func(mode)) else: self.assertFalse(func(mode))
def _ftypelet(mode): if stat.S_ISREG(mode) or not stat.S_IFMT(mode): return '-' if stat.S_ISBLK(mode): return 'b' if stat.S_ISCHR(mode): return 'c' if stat.S_ISDIR(mode): return 'd' if stat.S_ISFIFO(mode): return 'p' if stat.S_ISLNK(mode): return 'l' if stat.S_ISSOCK(mode): return 's' return '?'
def file_type(): def get_nan_file_type(): def file_type(path): return 'nan' return file_type def get_unix_file_type(): file_type_map = {} for t in ('reg', 'dir', 'chr', 'blk', 'fifo', 'lnk', 'sock'): mode_name = 'S_IF%s' % (t.upper(),) mode = getattr(stat, mode_name, None) if mode is not None: file_type_map[mode] = t def file_type(path): return file_type_map.get(stat.S_IFMT(os.stat(path).st_mode), 'nan') return file_type S_IFMT = getattr(stat, 'S_IFMT', None) if S_IFMT is None: return get_nan_file_type() else: return get_unix_file_type()
def _join_modifies(self): if self._rewrite_threshold is None: return modifies = {} delete_map = dict((d.old.path, d) for d in self._deletes) for add in self._adds: path = add.new.path delete = delete_map.get(path) if (delete is not None and stat.S_IFMT(delete.old.mode) == stat.S_IFMT(add.new.mode)): modifies[path] = TreeChange(CHANGE_MODIFY, delete.old, add.new) self._adds = [a for a in self._adds if a.new.path not in modifies] self._deletes = [a for a in self._deletes if a.new.path not in modifies] self._changes += modifies.values()
def _sig(st): return (stat.S_IFMT(st.st_mode), st.st_size, st.st_mtime)
def phase2(self): # Distinguish files, directories, funnies self.common_dirs = [] self.common_files = [] self.common_funny = [] for x in self.common: a_path = os.path.join(self.left, x) b_path = os.path.join(self.right, x) ok = 1 try: a_stat = os.stat(a_path) except os.error, why: # print 'Can\'t stat', a_path, ':', why[1] ok = 0 try: b_stat = os.stat(b_path) except os.error, why: # print 'Can\'t stat', b_path, ':', why[1] ok = 0 if ok: a_type = stat.S_IFMT(a_stat.st_mode) b_type = stat.S_IFMT(b_stat.st_mode) if a_type != b_type: self.common_funny.append(x) elif stat.S_ISDIR(a_type): self.common_dirs.append(x) elif stat.S_ISREG(a_type): self.common_files.append(x) else: self.common_funny.append(x) else: self.common_funny.append(x)
def get_file_info(system, path, verbose=False): fullpath = get_fullpath(system, path) if fullpath == IO_ERROR: return IO_ERROR result = {} if system.startswith('/'): # local or sub if can_read(system, path): # TODO platform-specific stats = os.stat(fullpath) stm = stats.st_mode result['type'] = get_file_type_char(stat.S_IFMT(stm)) result['permissions'] = '%o' % (stat.S_IMODE(stm)) result['UID'] = stats[stat.ST_UID] result['GID'] = stats[stat.ST_GID] result['ATIME'] = stats[stat.ST_ATIME] result['MTIME'] = stats[stat.ST_MTIME] result['CTIME'] = stats[stat.ST_CTIME] # actually mtime on UNIX, TODO else: if verbose: log.info('File \'%s\' is not accessible.' % (fullpath)) result['type'] = None result['permissions'] = None result['UID'] = None result['GID'] = None result['ATIME'] = None result['MTIME'] = None result['CTIME'] = None return result else: # SSH/FTP/TFTP/HTTP # TODO NOT IMPLEMENTED return IO_ERROR
def phase2(self): # Distinguish files, directories, funnies self.common_dirs = [] self.common_files = [] self.common_funny = [] for x in self.common: a_path = os.path.join(self.left, x) b_path = os.path.join(self.right, x) ok = 1 try: a_stat = os.stat(a_path) except os.error as why: # print('Can\'t stat', a_path, ':', why.args[1]) ok = 0 try: b_stat = os.stat(b_path) except os.error as why: # print('Can\'t stat', b_path, ':', why.args[1]) ok = 0 if ok: a_type = stat.S_IFMT(a_stat.st_mode) b_type = stat.S_IFMT(b_stat.st_mode) if a_type != b_type: self.common_funny.append(x) elif stat.S_ISDIR(a_type): self.common_dirs.append(x) elif stat.S_ISREG(a_type): self.common_files.append(x) else: self.common_funny.append(x) else: self.common_funny.append(x)
def test_mode(self): with open(TESTFN, 'w'): pass if os.name == 'posix': os.chmod(TESTFN, 0o700) st_mode = self.get_mode() self.assertS_IS("REG", st_mode) self.assertEqual(stat.S_IMODE(st_mode), stat.S_IRWXU) os.chmod(TESTFN, 0o070) st_mode = self.get_mode() self.assertS_IS("REG", st_mode) self.assertEqual(stat.S_IMODE(st_mode), stat.S_IRWXG) os.chmod(TESTFN, 0o007) st_mode = self.get_mode() self.assertS_IS("REG", st_mode) self.assertEqual(stat.S_IMODE(st_mode), stat.S_IRWXO) os.chmod(TESTFN, 0o444) st_mode = self.get_mode() self.assertS_IS("REG", st_mode) self.assertEqual(stat.S_IMODE(st_mode), 0o444) else: os.chmod(TESTFN, 0o700) st_mode = self.get_mode() self.assertS_IS("REG", st_mode) self.assertEqual(stat.S_IFMT(st_mode), stat.S_IFREG)
def test_mode(self): with open(TESTFN, 'w'): pass if os.name == 'posix': os.chmod(TESTFN, 0o700) st_mode, modestr = self.get_mode() self.assertEqual(modestr, '-rwx------') self.assertS_IS("REG", st_mode) self.assertEqual(stat.S_IMODE(st_mode), stat.S_IRWXU) os.chmod(TESTFN, 0o070) st_mode, modestr = self.get_mode() self.assertEqual(modestr, '----rwx---') self.assertS_IS("REG", st_mode) self.assertEqual(stat.S_IMODE(st_mode), stat.S_IRWXG) os.chmod(TESTFN, 0o007) st_mode, modestr = self.get_mode() self.assertEqual(modestr, '-------rwx') self.assertS_IS("REG", st_mode) self.assertEqual(stat.S_IMODE(st_mode), stat.S_IRWXO) os.chmod(TESTFN, 0o444) st_mode, modestr = self.get_mode() self.assertS_IS("REG", st_mode) self.assertEqual(modestr, '-r--r--r--') self.assertEqual(stat.S_IMODE(st_mode), 0o444) else: os.chmod(TESTFN, 0o700) st_mode, modestr = self.get_mode() self.assertEqual(modestr[:3], '-rw') self.assertS_IS("REG", st_mode) self.assertEqual(stat.S_IFMT(st_mode), stat.S_IFREG)
def _get_type_from_stat(cls, _stat): """Get the resource type from an `os.stat_result` object. """ st_mode = _stat.st_mode st_type = stat.S_IFMT(st_mode) return cls.STAT_TO_RESOURCE_TYPE.get(st_type, ResourceType.unknown) # -------------------------------------------------------- # Required Methods # --------------------------------------------------------
def create_dir(self, dir_inode, name, *, mode=0): if not stat.S_IFMT(mode): mode |= stat.S_IFDIR dir_inode.change_times() _debug('create_dir %s 0x%x', name, mode) return self._create(mode, dir_inode, name)
def create_file(self, dir_inode, name, *, mode=0): if not stat.S_IFMT(mode): mode |= stat.S_IFREG dir_inode.change_times() _debug('create_file %s 0x%x', name, mode) return self._create(mode, dir_inode, name)
def phase2(self): # Distinguish files, directories, funnies self.common_dirs = [] self.common_files = [] self.common_funny = [] for x in self.common: a_path = os.path.join(self.left, x) b_path = os.path.join(self.right, x) ok = 1 try: a_stat = os.stat(a_path) except OSError as why: # print('Can\'t stat', a_path, ':', why.args[1]) ok = 0 try: b_stat = os.stat(b_path) except OSError as why: # print('Can\'t stat', b_path, ':', why.args[1]) ok = 0 if ok: a_type = stat.S_IFMT(a_stat.st_mode) b_type = stat.S_IFMT(b_stat.st_mode) if a_type != b_type: self.common_funny.append(x) elif stat.S_ISDIR(a_type): self.common_dirs.append(x) elif stat.S_ISREG(a_type): self.common_files.append(x) else: self.common_funny.append(x) else: self.common_funny.append(x)
def path_mode(self, path): if not self.path_mod.exists(path): return 0 return stat.S_IFMT(self.stat(path).st_mode)
def path_mode(self, path): try: with self.ftp_handle() as ftp: return stat.S_IFMT(ftp.stat(path).st_mode) except IOError as error: if error.errno in (errno.ENOENT, errno.EACCES): return 0 raise
def S_ISGITLINK(m): """Check if a mode indicates a submodule. :param m: Mode to check :return: a ``boolean`` """ return (stat.S_IFMT(m) == S_IFGITLINK)
def _find_exact_renames(self): add_map = defaultdict(list) for add in self._adds: add_map[add.new.sha].append(add.new) delete_map = defaultdict(list) for delete in self._deletes: # Keep track of whether the delete was actually marked as a delete. # If not, it needs to be marked as a copy. is_delete = delete.type == CHANGE_DELETE delete_map[delete.old.sha].append((delete.old, is_delete)) add_paths = set() delete_paths = set() for sha, sha_deletes in delete_map.items(): sha_adds = add_map[sha] for (old, is_delete), new in zip(sha_deletes, sha_adds): if stat.S_IFMT(old.mode) != stat.S_IFMT(new.mode): continue if is_delete: delete_paths.add(old.path) add_paths.add(new.path) new_type = is_delete and CHANGE_RENAME or CHANGE_COPY self._changes.append(TreeChange(new_type, old, new)) num_extra_adds = len(sha_adds) - len(sha_deletes) # TODO(dborowitz): Less arbitrary way of dealing with extra copies. old = sha_deletes[0][0] if num_extra_adds > 0: for new in sha_adds[-num_extra_adds:]: add_paths.add(new.path) self._changes.append(TreeChange(CHANGE_COPY, old, new)) self._prune(add_paths, delete_paths)
def _find_content_rename_candidates(self): candidates = self._candidates = [] # TODO: Optimizations: # - Compare object sizes before counting blocks. # - Skip if delete's S_IFMT differs from all adds. # - Skip if adds or deletes is empty. # Match C git's behavior of not attempting to find content renames if # the matrix size exceeds the threshold. if not self._should_find_content_renames(): return block_cache = {} check_paths = self._rename_threshold is not None for delete in self._deletes: if S_ISGITLINK(delete.old.mode): continue # Git links don't exist in this repo. old_sha = delete.old.sha old_obj = self._store[old_sha] block_cache[old_sha] = _count_blocks(old_obj) for add in self._adds: if stat.S_IFMT(delete.old.mode) != stat.S_IFMT(add.new.mode): continue new_obj = self._store[add.new.sha] score = _similarity_score(old_obj, new_obj, block_cache=block_cache) if score > self._rename_threshold: new_type = self._rename_type(check_paths, delete, add) rename = TreeChange(new_type, delete.old, add.new) candidates.append((-score, rename))
def lsLine(name, s): mode = s.st_mode perms = array.array('c', '-'*10) ft = stat.S_IFMT(mode) if stat.S_ISDIR(ft): perms[0] = 'd' elif stat.S_ISCHR(ft): perms[0] = 'c' elif stat.S_ISBLK(ft): perms[0] = 'b' elif stat.S_ISREG(ft): perms[0] = '-' elif stat.S_ISFIFO(ft): perms[0] = 'f' elif stat.S_ISLNK(ft): perms[0] = 'l' elif stat.S_ISSOCK(ft): perms[0] = 's' else: perms[0] = '!' # user if mode&stat.S_IRUSR:perms[1] = 'r' if mode&stat.S_IWUSR:perms[2] = 'w' if mode&stat.S_IXUSR:perms[3] = 'x' # group if mode&stat.S_IRGRP:perms[4] = 'r' if mode&stat.S_IWGRP:perms[5] = 'w' if mode&stat.S_IXGRP:perms[6] = 'x' # other if mode&stat.S_IROTH:perms[7] = 'r' if mode&stat.S_IWOTH:perms[8] = 'w' if mode&stat.S_IXOTH:perms[9] = 'x' # suid/sgid if mode&stat.S_ISUID: if perms[3] == 'x': perms[3] = 's' else: perms[3] = 'S' if mode&stat.S_ISGID: if perms[6] == 'x': perms[6] = 's' else: perms[6] = 'S' l = perms.tostring() l += str(s.st_nlink).rjust(5) + ' ' un = str(s.st_uid) l += un.ljust(9) gr = str(s.st_gid) l += gr.ljust(9) sz = str(s.st_size) l += sz.rjust(8) l += ' ' sixmo = 60 * 60 * 24 * 7 * 26 if s.st_mtime + sixmo < time.time(): # last edited more than 6mo ago l += time.strftime("%b %2d %Y ", time.localtime(s.st_mtime)) else: l += time.strftime("%b %2d %H:%S ", time.localtime(s.st_mtime)) l += name return l
def __str__(self): "create a unix-style long description of the file (like ls -l)" if self.st_mode is not None: kind = stat.S_IFMT(self.st_mode) if kind == stat.S_IFIFO: ks = 'p' elif kind == stat.S_IFCHR: ks = 'c' elif kind == stat.S_IFDIR: ks = 'd' elif kind == stat.S_IFBLK: ks = 'b' elif kind == stat.S_IFREG: ks = '-' elif kind == stat.S_IFLNK: ks = 'l' elif kind == stat.S_IFSOCK: ks = 's' else: ks = '?' ks += self._rwx((self.st_mode & 0700) >> 6, self.st_mode & stat.S_ISUID) ks += self._rwx((self.st_mode & 070) >> 3, self.st_mode & stat.S_ISGID) ks += self._rwx(self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) else: ks = '?---------' # compute display date if (self.st_mtime is None) or (self.st_mtime == 0xffffffffL): # shouldn't really happen datestr = '(unknown date)' else: if abs(time.time() - self.st_mtime) > 15552000: # (15552000 = 6 months) datestr = time.strftime('%d %b %Y', time.localtime(self.st_mtime)) else: datestr = time.strftime('%d %b %H:%M', time.localtime(self.st_mtime)) filename = getattr(self, 'filename', '?') # not all servers support uid/gid uid = self.st_uid gid = self.st_gid if uid is None: uid = 0 if gid is None: gid = 0 return '%s 1 %-8d %-8d %8d %-12s %s' % (ks, uid, gid, self.st_size, datestr, filename)
def __str__(self): """create a unix-style long description of the file (like ls -l)""" if self.st_mode is not None: kind = stat.S_IFMT(self.st_mode) if kind == stat.S_IFIFO: ks = 'p' elif kind == stat.S_IFCHR: ks = 'c' elif kind == stat.S_IFDIR: ks = 'd' elif kind == stat.S_IFBLK: ks = 'b' elif kind == stat.S_IFREG: ks = '-' elif kind == stat.S_IFLNK: ks = 'l' elif kind == stat.S_IFSOCK: ks = 's' else: ks = '?' ks += self._rwx((self.st_mode & o700) >> 6, self.st_mode & stat.S_ISUID) ks += self._rwx((self.st_mode & o70) >> 3, self.st_mode & stat.S_ISGID) ks += self._rwx(self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) else: ks = '?---------' # compute display date if (self.st_mtime is None) or (self.st_mtime == xffffffff): # shouldn't really happen datestr = '(unknown date)' else: if abs(time.time() - self.st_mtime) > 15552000: # (15552000 = 6 months) datestr = time.strftime('%d %b %Y', time.localtime(self.st_mtime)) else: datestr = time.strftime('%d %b %H:%M', time.localtime(self.st_mtime)) filename = getattr(self, 'filename', '?') # not all servers support uid/gid uid = self.st_uid gid = self.st_gid if uid is None: uid = 0 if gid is None: gid = 0 return '%s 1 %-8d %-8d %8d %-12s %s' % (ks, uid, gid, self.st_size, datestr, filename)
def __str__(self): """create a unix-style long description of the file (like ls -l)""" if self.st_mode is not None: kind = stat.S_IFMT(self.st_mode) if kind == stat.S_IFIFO: ks = 'p' elif kind == stat.S_IFCHR: ks = 'c' elif kind == stat.S_IFDIR: ks = 'd' elif kind == stat.S_IFBLK: ks = 'b' elif kind == stat.S_IFREG: ks = '-' elif kind == stat.S_IFLNK: ks = 'l' elif kind == stat.S_IFSOCK: ks = 's' else: ks = '?' ks += self._rwx((self.st_mode & o700) >> 6, self.st_mode & stat.S_ISUID) ks += self._rwx((self.st_mode & o70) >> 3, self.st_mode & stat.S_ISGID) ks += self._rwx(self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) else: ks = '?---------' # compute display date if (self.st_mtime is None) or (self.st_mtime == xffffffff): # shouldn't really happen datestr = '(unknown date)' else: if abs(time.time() - self.st_mtime) > 15552000: # (15552000 = 6 months) datestr = time.strftime('%d %b %Y', time.localtime(self.st_mtime)) else: datestr = time.strftime('%d %b %H:%M', time.localtime(self.st_mtime)) filename = getattr(self, 'filename', '?') # not all servers support uid/gid uid = self.st_uid gid = self.st_gid size = self.st_size if uid is None: uid = 0 if gid is None: gid = 0 if size is None: size = 0 return '%s 1 %-8d %-8d %8d %-12s %s' % (ks, uid, gid, size, datestr, filename)
def __str__(self): """create a unix-style long description of the file (like ls -l)""" if self.st_mode is not None: kind = stat.S_IFMT(self.st_mode) if kind == stat.S_IFIFO: ks = 'p' elif kind == stat.S_IFCHR: ks = 'c' elif kind == stat.S_IFDIR: ks = 'd' elif kind == stat.S_IFBLK: ks = 'b' elif kind == stat.S_IFREG: ks = '-' elif kind == stat.S_IFLNK: ks = 'l' elif kind == stat.S_IFSOCK: ks = 's' else: ks = '?' ks += self._rwx( (self.st_mode & o700) >> 6, self.st_mode & stat.S_ISUID) ks += self._rwx( (self.st_mode & o70) >> 3, self.st_mode & stat.S_ISGID) ks += self._rwx( self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) else: ks = '?---------' # compute display date if (self.st_mtime is None) or (self.st_mtime == xffffffff): # shouldn't really happen datestr = '(unknown date)' else: if abs(time.time() - self.st_mtime) > 15552000: # (15552000 = 6 months) datestr = time.strftime( '%d %b %Y', time.localtime(self.st_mtime)) else: datestr = time.strftime( '%d %b %H:%M', time.localtime(self.st_mtime)) filename = getattr(self, 'filename', '?') # not all servers support uid/gid uid = self.st_uid gid = self.st_gid size = self.st_size if uid is None: uid = 0 if gid is None: gid = 0 if size is None: size = 0 return '%s 1 %-8d %-8d %8d %-12s %s' % ( ks, uid, gid, size, datestr, filename)